Smarter datasource

This commit is contained in:
Adria Navarro 2025-01-15 10:40:41 +01:00
parent cf4ce587c4
commit a3d73ee6ce
3 changed files with 39 additions and 9 deletions

View File

@ -5,6 +5,7 @@
fetchData,
QueryUtils,
DataFetchOptions,
ProviderDatasource,
} from "@budibase/frontend-core"
import {
LogicalOperator,
@ -15,7 +16,7 @@
} from "@budibase/types"
import { SDK, Component } from "../../index"
export let dataSource
export let dataSource: ProviderDatasource
export let filter
export let sortColumn
export let sortOrder
@ -98,7 +99,7 @@
primaryDisplay: ($fetch.definition as any)?.primaryDisplay,
}
const createFetch = (datasource: any) => {
const createFetch = (datasource: ProviderDatasource) => {
return fetchData({
API,
datasource,

View File

@ -35,6 +35,23 @@ export const DataFetchMap = {
queryarray: QueryArrayFetch,
}
export interface DataFetchClassMap {
table: TableFetch
view: ViewFetch
viewV2: ViewV2Fetch
query: QueryFetch
link: RelationshipFetch
user: UserFetch
groupUser: GroupUserFetch
custom: CustomFetch
// Client specific datasource types
provider: NestedProviderFetch
field: FieldFetch<"field">
jsonarray: JSONArrayFetch
queryarray: QueryArrayFetch
}
export type DataFetch =
| TableFetch
| ViewFetch
@ -60,8 +77,11 @@ export type DataFetchDatasource =
| CustomDatasource
| NestedProviderDatasource
| FieldDatasource<"field" | "queryarray" | "jsonarray">
// | FieldDatasource<"field" | "queryarray" | "jsonarray">
// | FieldDatasource<"field" | "queryarray" | "jsonarray">
export type ProviderDatasource = Exclude<
DataFetchDatasource,
UserDatasource | GroupUserDatasource
>
export type DataFetchDefinition =
| Table
@ -72,22 +92,27 @@ export type DataFetchDefinition =
}
// Constructs a new fetch model for a certain datasource
export const fetchData = ({
export const fetchData = <
T extends DataFetchDatasource,
Type extends T["type"] = T["type"]
>({
API,
datasource,
options,
}: {
API: APIClient
datasource: DataFetchDatasource
datasource: T
options: any
}) => {
}): Type extends keyof DataFetchClassMap
? DataFetchClassMap[Type]
: TableFetch => {
const Fetch = DataFetchMap[datasource?.type] || TableFetch
const fetch = new Fetch({ API, datasource, ...options })
// Initially fetch data but don't bother waiting for the result
fetch.getInitialData()
return fetch
return fetch as any
}
// Creates an empty fetch instance with no datasource configured, so no data

View File

@ -1,7 +1,11 @@
export { createAPIClient } from "./api"
export type { APIClient } from "./api"
export { fetchData, DataFetchMap } from "./fetch"
export type { DataFetchType, DataFetchOptions } from "./fetch"
export type {
DataFetchType,
DataFetchOptions,
ProviderDatasource,
} from "./fetch"
export * as Constants from "./constants"
export * from "./stores"
export * from "./utils"