2021-01-21 11:42:14 +01:00
|
|
|
import { cloneDeep } from "lodash/fp"
|
2021-11-03 12:57:47 +01:00
|
|
|
import { fetchTableData, fetchTableDefinition } from "./tables"
|
2020-11-12 10:07:09 +01:00
|
|
|
import { fetchViewData } from "./views"
|
|
|
|
import { fetchRelationshipData } from "./relationships"
|
2021-11-08 18:25:05 +01:00
|
|
|
import { FieldTypes } from "../constants"
|
2021-11-03 12:57:47 +01:00
|
|
|
import { executeQuery, fetchQueryDefinition } from "./queries"
|
2020-11-12 10:07:09 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches all rows for a particular Budibase data source.
|
|
|
|
*/
|
2021-05-04 12:32:22 +02:00
|
|
|
export const fetchDatasource = async dataSource => {
|
2021-03-16 14:54:34 +01:00
|
|
|
if (!dataSource || !dataSource.type) {
|
2020-11-12 10:07:09 +01:00
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch all rows in data source
|
2021-03-16 14:54:34 +01:00
|
|
|
const { type, tableId, fieldName } = dataSource
|
2020-11-12 10:07:09 +01:00
|
|
|
let rows = []
|
|
|
|
if (type === "table") {
|
2020-11-12 13:24:45 +01:00
|
|
|
rows = await fetchTableData(tableId)
|
2020-11-12 10:07:09 +01:00
|
|
|
} else if (type === "view") {
|
2021-03-16 14:54:34 +01:00
|
|
|
rows = await fetchViewData(dataSource)
|
2020-12-18 19:19:43 +01:00
|
|
|
} else if (type === "query") {
|
2021-01-18 16:37:32 +01:00
|
|
|
// Set the default query params
|
2021-03-16 14:54:34 +01:00
|
|
|
let parameters = cloneDeep(dataSource.queryParams || {})
|
|
|
|
for (let param of dataSource.parameters) {
|
2021-01-21 11:42:14 +01:00
|
|
|
if (!parameters[param.name]) {
|
|
|
|
parameters[param.name] = param.default
|
2021-01-11 21:17:56 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-16 14:54:34 +01:00
|
|
|
rows = await executeQuery({ queryId: dataSource._id, parameters })
|
2021-11-08 18:25:05 +01:00
|
|
|
} else if (type === FieldTypes.LINK) {
|
2020-11-19 14:50:20 +01:00
|
|
|
rows = await fetchRelationshipData({
|
2021-03-16 14:54:34 +01:00
|
|
|
rowId: dataSource.rowId,
|
|
|
|
tableId: dataSource.rowTableId,
|
2020-11-19 14:50:20 +01:00
|
|
|
fieldName,
|
|
|
|
})
|
2020-11-12 10:07:09 +01:00
|
|
|
}
|
2021-01-21 11:42:14 +01:00
|
|
|
|
2021-02-02 11:35:00 +01:00
|
|
|
// Enrich the result is always an array
|
|
|
|
return Array.isArray(rows) ? rows : []
|
2020-11-12 10:07:09 +01:00
|
|
|
}
|
2021-11-03 12:57:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches the schema of any kind of datasource.
|
|
|
|
*/
|
|
|
|
export const fetchDatasourceSchema = async dataSource => {
|
|
|
|
if (!dataSource) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
const { type } = dataSource
|
|
|
|
|
|
|
|
// Nested providers should already have exposed their own schema
|
|
|
|
if (type === "provider") {
|
|
|
|
return dataSource.value?.schema
|
|
|
|
}
|
|
|
|
|
2021-11-12 16:27:42 +01:00
|
|
|
// Field sources have their schema statically defined
|
2021-11-12 14:42:55 +01:00
|
|
|
if (type === "field") {
|
|
|
|
if (dataSource.fieldType === "attachment") {
|
|
|
|
return {
|
|
|
|
url: {
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
name: {
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else if (dataSource.fieldType === "array") {
|
|
|
|
return {
|
|
|
|
value: {
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-03 12:57:47 +01:00
|
|
|
// Tables, views and links can be fetched by table ID
|
|
|
|
if (
|
|
|
|
(type === "table" || type === "view" || type === "link") &&
|
|
|
|
dataSource.tableId
|
|
|
|
) {
|
|
|
|
const table = await fetchTableDefinition(dataSource.tableId)
|
|
|
|
return table?.schema
|
|
|
|
}
|
|
|
|
|
|
|
|
// Queries can be fetched by query ID
|
|
|
|
if (type === "query" && dataSource._id) {
|
|
|
|
const definition = await fetchQueryDefinition(dataSource._id)
|
|
|
|
return definition?.schema
|
|
|
|
}
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|