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

147 lines
3.5 KiB
TypeScript
Raw Normal View History

2025-01-16 10:28:06 +01:00
import TableFetch from "./TableFetch"
import ViewFetch from "./ViewFetch"
import ViewV2Fetch from "./ViewV2Fetch"
import QueryFetch from "./QueryFetch"
import RelationshipFetch from "./RelationshipFetch"
import NestedProviderFetch from "./NestedProviderFetch"
import FieldFetch from "./FieldFetch"
2025-01-07 12:41:37 +01:00
import JSONArrayFetch from "./JSONArrayFetch"
2025-01-16 10:28:06 +01:00
import UserFetch from "./UserFetch"
import GroupUserFetch from "./GroupUserFetch"
import CustomFetch from "./CustomFetch"
2025-01-09 14:49:07 +01:00
import QueryArrayFetch from "./QueryArrayFetch"
import { APIClient } from "../api/types"
2025-01-16 10:28:06 +01:00
import { DataFetchDatasource, Table, ViewV2Enriched } from "@budibase/types"
2025-01-09 15:04:56 +01:00
export type DataFetchType = keyof typeof DataFetchMap
2025-01-15 12:27:10 +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-16 10:28:06 +01:00
field: FieldFetch,
jsonarray: JSONArrayFetch,
queryarray: QueryArrayFetch,
}
2025-01-15 10:40:41 +01:00
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
2025-01-16 10:28:06 +01:00
field: FieldFetch
2025-01-15 10:40:41 +01:00
jsonarray: JSONArrayFetch
queryarray: QueryArrayFetch
}
2025-01-14 10:52:08 +01:00
export type DataFetch =
| TableFetch
| ViewFetch
| ViewV2Fetch
| QueryFetch
| RelationshipFetch
| UserFetch
| GroupUserFetch
| CustomFetch
| NestedProviderFetch
2025-01-16 10:28:06 +01:00
| FieldFetch
2025-01-14 10:52:08 +01:00
| JSONArrayFetch
| QueryArrayFetch
2025-01-14 11:25:26 +01:00
export type DataFetchDefinition =
| Table
| ViewV2Enriched
| {
2025-01-15 12:35:36 +01:00
// These fields are added to allow checking these fields on definition usages without requiring constant castings
2025-01-14 11:25:26 +01:00
schema?: Record<string, any> | null
primaryDisplay?: string
2025-01-15 12:27:10 +01:00
rowHeight?: number
type?: string
2025-01-15 12:35:36 +01:00
name?: string
2025-01-14 11:25:26 +01:00
}
// Constructs a new fetch model for a certain datasource
2025-01-15 10:40:41 +01:00
export const fetchData = <
T extends DataFetchDatasource,
Type extends T["type"] = T["type"]
>({
2025-01-14 10:45:08 +01:00
API,
datasource,
options,
}: {
API: APIClient
2025-01-15 10:40:41 +01:00
datasource: T
2025-01-14 10:45:08 +01:00
options: any
2025-01-15 10:40:41 +01:00
}): Type extends keyof DataFetchClassMap
? DataFetchClassMap[Type]
: TableFetch => {
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()
2025-01-15 10:40:41 +01:00
return fetch as any
}
// Creates an empty fetch instance with no datasource configured, so no data
// will initially be loaded
2025-01-23 12:25:10 +01:00
const createEmptyFetchInstance = <T extends DataFetchDatasource>({
2024-12-31 13:24:26 +01:00
API,
datasource,
}: {
2025-01-07 11:22:22 +01:00
API: APIClient
2025-01-23 12:25:10 +01:00
datasource: T
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,
2025-01-23 12:25:10 +01:00
datasource: datasource as any,
2025-01-09 15:23:20 +01:00
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)
}