2021-01-21 11:42:14 +01:00
|
|
|
import { cloneDeep } from "lodash/fp"
|
2020-11-12 13:24:45 +01:00
|
|
|
import { fetchTableData } from "./tables"
|
2020-11-12 10:07:09 +01:00
|
|
|
import { fetchViewData } from "./views"
|
|
|
|
import { fetchRelationshipData } from "./relationships"
|
2021-01-08 19:22:03 +01:00
|
|
|
import { executeQuery } 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 })
|
2020-11-12 10:07:09 +01:00
|
|
|
} else if (type === "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
|
|
|
}
|