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

View File

@ -35,6 +35,23 @@ export const DataFetchMap = {
queryarray: QueryArrayFetch, 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 = export type DataFetch =
| TableFetch | TableFetch
| ViewFetch | ViewFetch
@ -60,8 +77,11 @@ export type DataFetchDatasource =
| CustomDatasource | CustomDatasource
| NestedProviderDatasource | NestedProviderDatasource
| FieldDatasource<"field" | "queryarray" | "jsonarray"> | FieldDatasource<"field" | "queryarray" | "jsonarray">
// | FieldDatasource<"field" | "queryarray" | "jsonarray">
// | FieldDatasource<"field" | "queryarray" | "jsonarray"> export type ProviderDatasource = Exclude<
DataFetchDatasource,
UserDatasource | GroupUserDatasource
>
export type DataFetchDefinition = export type DataFetchDefinition =
| Table | Table
@ -72,22 +92,27 @@ export type DataFetchDefinition =
} }
// Constructs a new fetch model for a certain datasource // 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, API,
datasource, datasource,
options, options,
}: { }: {
API: APIClient API: APIClient
datasource: DataFetchDatasource datasource: T
options: any options: any
}) => { }): Type extends keyof DataFetchClassMap
? DataFetchClassMap[Type]
: TableFetch => {
const Fetch = DataFetchMap[datasource?.type] || TableFetch const Fetch = DataFetchMap[datasource?.type] || TableFetch
const fetch = new Fetch({ API, datasource, ...options }) const fetch = new Fetch({ API, datasource, ...options })
// Initially fetch data but don't bother waiting for the result // Initially fetch data but don't bother waiting for the result
fetch.getInitialData() fetch.getInitialData()
return fetch return fetch as any
} }
// Creates an empty fetch instance with no datasource configured, so no data // Creates an empty fetch instance with no datasource configured, so no data

View File

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