diff --git a/packages/frontend-core/src/components/grid/stores/datasource.ts b/packages/frontend-core/src/components/grid/stores/datasource.ts index 805ace5a8f..588f373152 100644 --- a/packages/frontend-core/src/components/grid/stores/datasource.ts +++ b/packages/frontend-core/src/components/grid/stores/datasource.ts @@ -1,7 +1,11 @@ // TODO: datasource and defitions are unions of the different implementations. At this point, the datasource does not know what type is being used, and the assignations will cause TS exceptions. Casting it "as any" for now. This should be fixed improving the type usages. import { derived, get, Readable, Writable } from "svelte/store" -import { getDatasourceDefinition, getDatasourceSchema } from "../../../fetch" +import { + DataFetchDefinition, + getDatasourceDefinition, + getDatasourceSchema, +} from "../../../fetch" import { enrichSchemaWithRelColumns, memo } from "../../../utils" import { cloneDeep } from "lodash" import { @@ -18,7 +22,7 @@ import { Store as StoreContext, BaseStoreProps } from "." import { DatasourceActions } from "./datasources" interface DatasourceStore { - definition: Writable + definition: Writable schemaMutations: Writable> subSchemaMutations: Writable>> } @@ -131,11 +135,17 @@ export const deriveStores = (context: StoreContext): DerivedDatasourceStore => { [datasource, definition], ([$datasource, $definition]) => { let type = $datasource?.type + // @ts-expect-error if (type === "provider") { type = ($datasource as any).value?.datasource?.type // TODO: see line 1 } // Handle calculation views - if (type === "viewV2" && $definition?.type === ViewV2Type.CALCULATION) { + if ( + type === "viewV2" && + $definition && + "type" in $definition && + $definition.type === ViewV2Type.CALCULATION + ) { return false } return !!type && ["table", "viewV2", "link"].includes(type) @@ -197,7 +207,7 @@ export const createActions = (context: StoreContext): ActionDatasourceStore => { ) => { // Update local state const originalDefinition = get(definition) - definition.set(newDefinition as UIDatasource) + definition.set(newDefinition) // Update server if (get(config).canSaveSchema) { @@ -225,13 +235,15 @@ export const createActions = (context: StoreContext): ActionDatasourceStore => { // Update primary display newDefinition.primaryDisplay = column - // Sanitise schema to ensure field is required and has no default value - if (!newDefinition.schema[column].constraints) { - newDefinition.schema[column].constraints = {} - } - newDefinition.schema[column].constraints.presence = { allowEmpty: false } - if ("default" in newDefinition.schema[column]) { - delete newDefinition.schema[column].default + if (newDefinition.schema) { + // Sanitise schema to ensure field is required and has no default value + if (!newDefinition.schema[column].constraints) { + newDefinition.schema[column].constraints = {} + } + newDefinition.schema[column].constraints.presence = { allowEmpty: false } + if ("default" in newDefinition.schema[column]) { + delete newDefinition.schema[column].default + } } return await saveDefinition(newDefinition as any) // TODO: see line 1 } diff --git a/packages/frontend-core/src/fetch/index.ts b/packages/frontend-core/src/fetch/index.ts index 1c1d6671e6..dee0c9dbf2 100644 --- a/packages/frontend-core/src/fetch/index.ts +++ b/packages/frontend-core/src/fetch/index.ts @@ -11,6 +11,7 @@ import GroupUserFetch from "./GroupUserFetch" import CustomFetch from "./CustomFetch" import QueryArrayFetch from "./QueryArrayFetch" import { APIClient } from "../api/types" +import { Table, ViewV2Enriched } from "@budibase/types" export type DataFetchType = keyof typeof DataFetchMap @@ -45,6 +46,14 @@ export type DataFetch = | JSONArrayFetch | QueryArrayFetch +export type DataFetchDefinition = + | Table + | ViewV2Enriched + | { + schema?: Record | null + primaryDisplay?: string + } + // Constructs a new fetch model for a certain datasource export const fetchData = ({ API,