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

81 lines
2.3 KiB
TypeScript
Raw Normal View History

import TableFetch from "./TableFetch.js"
import ViewFetch from "./ViewFetch.js"
2023-07-19 10:16:12 +02:00
import ViewV2Fetch from "./ViewV2Fetch.js"
2025-01-07 11:47:10 +01:00
import QueryFetch from "./QueryFetch"
2025-01-07 10:14:18 +01:00
import RelationshipFetch from "./RelationshipFetch"
2025-01-07 13:34:42 +01:00
import NestedProviderFetch from "./NestedProviderFetch"
2025-01-03 12:34:36 +01:00
import FieldFetch from "./FieldFetch"
2025-01-07 12:41:37 +01:00
import JSONArrayFetch from "./JSONArrayFetch"
import UserFetch from "./UserFetch.js"
2025-01-07 11:02:09 +01:00
import GroupUserFetch from "./GroupUserFetch"
2025-01-07 13:46:00 +01:00
import CustomFetch from "./CustomFetch"
import QueryArrayFetch from "./QueryArrayFetch.js"
2025-01-07 15:33:05 +01:00
import { TableSchema, UIDatasource } from "@budibase/types"
2025-01-07 11:22:22 +01:00
import { APIClient } from "../api/types.js"
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,
field: FieldFetch,
jsonarray: JSONArrayFetch,
queryarray: QueryArrayFetch,
}
// Constructs a new fetch model for a certain datasource
2025-01-07 11:22:22 +01:00
export const fetchData = ({ API, datasource, options }: any) => {
2024-12-31 13:24:26 +01:00
const Fetch =
DataFetchMap[datasource?.type as keyof typeof DataFetchMap] || TableFetch
return new Fetch({ API, datasource, ...options })
}
// Creates an empty fetch instance with no datasource configured, so no data
// will initially be loaded
2024-12-31 13:24:26 +01:00
const createEmptyFetchInstance = ({
API,
datasource,
}: {
2025-01-07 11:22:22 +01:00
API: APIClient
datasource: any
2024-12-31 13:24:26 +01:00
}) => {
const handler = DataFetchMap[datasource?.type as keyof typeof DataFetchMap]
if (!handler) {
return null
}
2025-01-07 11:22:22 +01:00
return new handler({ API, datasource: null as any, query: null as any })
}
// Fetches the definition of any type of datasource
2024-12-31 13:24:26 +01:00
export const getDatasourceDefinition = async ({
API,
datasource,
}: {
2025-01-07 11:22:22 +01:00
API: APIClient
2024-12-31 13:24:26 +01:00
datasource: UIDatasource
}) => {
const instance = createEmptyFetchInstance({ API, datasource })
return await instance?.getDefinition(datasource)
}
// Fetches the schema of any type of datasource
2024-12-31 13:24:26 +01:00
export const getDatasourceSchema = ({
API,
datasource,
definition,
}: {
2025-01-07 11:22:22 +01:00
API: APIClient
2024-12-31 13:24:26 +01:00
datasource: UIDatasource
2025-01-07 15:33:05 +01:00
definition?: { schema?: TableSchema }
2024-12-31 13:24:26 +01:00
}) => {
const instance = createEmptyFetchInstance({ API, datasource })
return instance?.getSchema(datasource, definition)
}