budibase/packages/frontend-core/src/fetch/index.ts

138 lines
3.5 KiB
TypeScript
Raw Normal View History

2025-01-14 12:51:27 +01:00
import TableFetch, { TableDatasource } from "./TableFetch"
import ViewFetch, { ViewV1Datasource } from "./ViewFetch"
import ViewV2Fetch, { ViewDatasource } from "./ViewV2Fetch"
import QueryFetch, { QueryDatasource } from "./QueryFetch"
import RelationshipFetch, { RelationshipDatasource } from "./RelationshipFetch"
import NestedProviderFetch, {
NestedProviderDatasource,
} from "./NestedProviderFetch"
import FieldFetch, { FieldDatasource } from "./FieldFetch"
2025-01-07 12:41:37 +01:00
import JSONArrayFetch from "./JSONArrayFetch"
2025-01-14 12:51:27 +01:00
import UserFetch, { UserDatasource } from "./UserFetch"
import GroupUserFetch, { GroupUserDatasource } from "./GroupUserFetch"
import CustomFetch, { CustomDatasource } from "./CustomFetch"
2025-01-09 14:49:07 +01:00
import QueryArrayFetch from "./QueryArrayFetch"
import { APIClient } from "../api/types"
2025-01-14 11:25:26 +01:00
import { Table, ViewV2Enriched } from "@budibase/types"
2025-01-09 15:04:56 +01:00
export type DataFetchType = keyof typeof DataFetchMap
2025-01-14 11:51:19 +01:00
export type { DataFetchOptions } from "./DataFetch"
2025-01-09 15:04:56 +01:00
2025-01-09 14:49:07 +01:00
export const DataFetchMap = {
table: TableFetch,
view: ViewFetch,
2023-07-19 10:16:12 +02:00
viewV2: ViewV2Fetch,
query: QueryFetch,
link: RelationshipFetch,
user: UserFetch,
2023-05-04 18:38:04 +02:00
groupUser: GroupUserFetch,
2023-10-05 15:42:26 +02:00
custom: CustomFetch,
// Client specific datasource types
provider: NestedProviderFetch,
2025-01-09 15:23:20 +01:00
field: FieldFetch<"field">,
jsonarray: JSONArrayFetch,
queryarray: QueryArrayFetch,
}
2025-01-14 10:52:08 +01:00
export type DataFetch =
| TableFetch
| ViewFetch
| ViewV2Fetch
| QueryFetch
| RelationshipFetch
| UserFetch
| GroupUserFetch
| CustomFetch
| NestedProviderFetch
| FieldFetch<"field">
| JSONArrayFetch
| QueryArrayFetch
2025-01-14 12:51:27 +01:00
export type DataFetchDatasource =
| TableDatasource
| ViewV1Datasource
| ViewDatasource
| QueryDatasource
| RelationshipDatasource
| UserDatasource
| GroupUserDatasource
| CustomDatasource
| NestedProviderDatasource
| FieldDatasource<"field" | "queryarray" | "jsonarray">
// | FieldDatasource<"field" | "queryarray" | "jsonarray">
// | FieldDatasource<"field" | "queryarray" | "jsonarray">
2025-01-14 11:25:26 +01:00
export type DataFetchDefinition =
| Table
| ViewV2Enriched
| {
schema?: Record<string, any> | null
primaryDisplay?: string
}
// Constructs a new fetch model for a certain datasource
2025-01-14 12:51:27 +01:00
export const fetchData = ({
2025-01-14 10:45:08 +01:00
API,
datasource,
options,
}: {
API: APIClient
2025-01-14 12:51:27 +01:00
datasource: DataFetchDatasource
2025-01-14 10:45:08 +01:00
options: any
}) => {
2025-01-14 12:51:27 +01:00
const Fetch = DataFetchMap[datasource?.type] || TableFetch
2025-01-09 15:58:59 +01:00
const fetch = new Fetch({ API, datasource, ...options })
// Initially fetch data but don't bother waiting for the result
fetch.getInitialData()
return fetch
}
// Creates an empty fetch instance with no datasource configured, so no data
// will initially be loaded
2025-01-14 12:51:27 +01:00
const createEmptyFetchInstance = ({
2024-12-31 13:24:26 +01:00
API,
datasource,
}: {
2025-01-07 11:22:22 +01:00
API: APIClient
2025-01-14 12:51:27 +01:00
datasource: DataFetchDatasource
2024-12-31 13:24:26 +01:00
}) => {
2025-01-14 12:51:27 +01:00
const handler = DataFetchMap[datasource?.type]
if (!handler) {
return null
}
2025-01-09 15:23:20 +01:00
return new handler({
API,
datasource: null as never,
query: null as any,
})
}
// Fetches the definition of any type of datasource
2025-01-14 12:51:27 +01:00
export const getDatasourceDefinition = async ({
2024-12-31 13:24:26 +01:00
API,
datasource,
}: {
2025-01-07 11:22:22 +01:00
API: APIClient
2025-01-14 12:51:27 +01:00
datasource: DataFetchDatasource
2024-12-31 13:24:26 +01:00
}) => {
const instance = createEmptyFetchInstance({ API, datasource })
2025-01-08 14:27:13 +01:00
return await instance?.getDefinition()
}
// Fetches the schema of any type of datasource
2025-01-14 12:51:27 +01:00
export const getDatasourceSchema = ({
2024-12-31 13:24:26 +01:00
API,
datasource,
definition,
}: {
2025-01-07 11:22:22 +01:00
API: APIClient
2025-01-14 12:51:27 +01:00
datasource: DataFetchDatasource
2025-01-08 14:17:10 +01:00
definition?: any
2024-12-31 13:24:26 +01:00
}) => {
const instance = createEmptyFetchInstance({ API, datasource })
2025-01-08 12:57:25 +01:00
return instance?.getSchema(definition)
}