1
0
Fork 0
mirror of synced 2024-09-20 19:33:10 +12:00
budibase/packages/client/src/api/datasources.js

44 lines
1.3 KiB
JavaScript
Raw Normal View History

2021-01-12 04:34:43 +13:00
import { get } from "svelte/store"
import { fetchTableData } from "./tables"
import { fetchViewData } from "./views"
import { fetchRelationshipData } from "./relationships"
2021-01-09 07:22:03 +13:00
import { executeQuery } from "./queries"
import { enrichRows } from "./rows"
2021-01-09 07:22:03 +13:00
import { enrichDataBindings } from "../utils/enrichDataBinding"
2021-01-12 04:34:43 +13:00
import { bindingStore } from "../store/binding"
/**
* Fetches all rows for a particular Budibase data source.
*/
export const fetchDatasource = async (datasource, dataContext) => {
if (!datasource || !datasource.type) {
return []
}
// Fetch all rows in data source
const { type, tableId, fieldName } = datasource
let rows = []
if (type === "table") {
rows = await fetchTableData(tableId)
} else if (type === "view") {
rows = await fetchViewData(datasource)
2020-12-19 07:19:43 +13:00
} else if (type === "query") {
2021-01-12 04:34:43 +13:00
const bindings = get(bindingStore)
const fullContext = {
...bindings,
...dataContext,
}
const parameters = enrichDataBindings(datasource.queryParams, fullContext)
return await executeQuery({ queryId: datasource._id, parameters })
} else if (type === "link") {
const row = dataContext[datasource.providerId]
rows = await fetchRelationshipData({
rowId: row?._id,
tableId: row?.tableId,
fieldName,
})
}
// Enrich rows
return await enrichRows(rows, tableId)
}