From a3d73ee6ce7cb159afb368c076f0016b8c1348da Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 15 Jan 2025 10:40:41 +0100 Subject: [PATCH] Smarter datasource --- .../src/components/app/DataProvider.svelte | 5 ++- packages/frontend-core/src/fetch/index.ts | 37 ++++++++++++++++--- packages/frontend-core/src/index.ts | 6 ++- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/packages/client/src/components/app/DataProvider.svelte b/packages/client/src/components/app/DataProvider.svelte index da7eceeb5c..7b0dcb123b 100644 --- a/packages/client/src/components/app/DataProvider.svelte +++ b/packages/client/src/components/app/DataProvider.svelte @@ -5,6 +5,7 @@ fetchData, QueryUtils, DataFetchOptions, + ProviderDatasource, } from "@budibase/frontend-core" import { LogicalOperator, @@ -15,7 +16,7 @@ } from "@budibase/types" import { SDK, Component } from "../../index" - export let dataSource + export let dataSource: ProviderDatasource export let filter export let sortColumn export let sortOrder @@ -98,7 +99,7 @@ primaryDisplay: ($fetch.definition as any)?.primaryDisplay, } - const createFetch = (datasource: any) => { + const createFetch = (datasource: ProviderDatasource) => { return fetchData({ API, datasource, diff --git a/packages/frontend-core/src/fetch/index.ts b/packages/frontend-core/src/fetch/index.ts index bacea080cb..32c5aa5b21 100644 --- a/packages/frontend-core/src/fetch/index.ts +++ b/packages/frontend-core/src/fetch/index.ts @@ -35,6 +35,23 @@ export const DataFetchMap = { 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 = | TableFetch | ViewFetch @@ -60,8 +77,11 @@ export type DataFetchDatasource = | CustomDatasource | NestedProviderDatasource | FieldDatasource<"field" | "queryarray" | "jsonarray"> -// | FieldDatasource<"field" | "queryarray" | "jsonarray"> -// | FieldDatasource<"field" | "queryarray" | "jsonarray"> + +export type ProviderDatasource = Exclude< + DataFetchDatasource, + UserDatasource | GroupUserDatasource +> export type DataFetchDefinition = | Table @@ -72,22 +92,27 @@ export type DataFetchDefinition = } // 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, datasource, options, }: { API: APIClient - datasource: DataFetchDatasource + datasource: T options: any -}) => { +}): Type extends keyof DataFetchClassMap + ? DataFetchClassMap[Type] + : TableFetch => { const Fetch = DataFetchMap[datasource?.type] || TableFetch const fetch = new Fetch({ API, datasource, ...options }) // Initially fetch data but don't bother waiting for the result fetch.getInitialData() - return fetch + return fetch as any } // Creates an empty fetch instance with no datasource configured, so no data diff --git a/packages/frontend-core/src/index.ts b/packages/frontend-core/src/index.ts index 11354be49d..32eb532503 100644 --- a/packages/frontend-core/src/index.ts +++ b/packages/frontend-core/src/index.ts @@ -1,7 +1,11 @@ export { createAPIClient } from "./api" export type { APIClient } from "./api" export { fetchData, DataFetchMap } from "./fetch" -export type { DataFetchType, DataFetchOptions } from "./fetch" +export type { + DataFetchType, + DataFetchOptions, + ProviderDatasource, +} from "./fetch" export * as Constants from "./constants" export * from "./stores" export * from "./utils"