2021-12-17 09:22:04 +01:00
|
|
|
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"
|
2022-08-05 16:57:21 +02:00
|
|
|
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"
|
2024-02-19 10:13:03 +01:00
|
|
|
import QueryArrayFetch from "./QueryArrayFetch.js"
|
2025-01-08 14:17:10 +01:00
|
|
|
import { UIDatasource } from "@budibase/types"
|
2025-01-07 11:22:22 +01:00
|
|
|
import { APIClient } from "../api/types.js"
|
2021-12-17 09:22:04 +01:00
|
|
|
|
|
|
|
const DataFetchMap = {
|
|
|
|
table: TableFetch,
|
|
|
|
view: ViewFetch,
|
2023-07-19 10:16:12 +02:00
|
|
|
viewV2: ViewV2Fetch,
|
2021-12-17 09:22:04 +01:00
|
|
|
query: QueryFetch,
|
|
|
|
link: RelationshipFetch,
|
2022-08-05 16:57:21 +02:00
|
|
|
user: UserFetch,
|
2023-05-04 18:38:04 +02:00
|
|
|
groupUser: GroupUserFetch,
|
2023-10-05 15:42:26 +02:00
|
|
|
custom: CustomFetch,
|
2022-01-18 10:39:19 +01:00
|
|
|
|
|
|
|
// Client specific datasource types
|
2022-01-04 16:02:43 +01:00
|
|
|
provider: NestedProviderFetch,
|
|
|
|
field: FieldFetch,
|
|
|
|
jsonarray: JSONArrayFetch,
|
2024-02-19 10:13:03 +01:00
|
|
|
queryarray: QueryArrayFetch,
|
2021-12-17 09:22:04 +01:00
|
|
|
}
|
|
|
|
|
2023-10-03 18:35:00 +02:00
|
|
|
// 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
|
2022-01-20 10:40:53 +01:00
|
|
|
return new Fetch({ API, datasource, ...options })
|
2021-12-17 09:22:04 +01:00
|
|
|
}
|
2023-10-03 18:35:00 +02:00
|
|
|
|
2023-10-24 09:26:57 +02:00
|
|
|
// 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]
|
2023-10-03 18:35:00 +02:00
|
|
|
if (!handler) {
|
|
|
|
return null
|
|
|
|
}
|
2025-01-07 11:22:22 +01:00
|
|
|
return new handler({ API, datasource: null as any, query: null as any })
|
2023-10-24 09:26:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2025-01-08 14:17:10 +01:00
|
|
|
datasource: any
|
2024-12-31 13:24:26 +01:00
|
|
|
}) => {
|
2023-10-24 09:26:57 +02:00
|
|
|
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-08 14:17:10 +01:00
|
|
|
definition?: any
|
2024-12-31 13:24:26 +01:00
|
|
|
}) => {
|
2023-10-24 09:26:57 +02:00
|
|
|
const instance = createEmptyFetchInstance({ API, datasource })
|
2025-01-08 12:57:25 +01:00
|
|
|
return instance?.getSchema(definition)
|
2023-10-03 18:35:00 +02:00
|
|
|
}
|