diff --git a/packages/frontend-core/src/components/grid/stores/datasource.js b/packages/frontend-core/src/components/grid/stores/datasource.ts similarity index 80% rename from packages/frontend-core/src/components/grid/stores/datasource.js rename to packages/frontend-core/src/components/grid/stores/datasource.ts index 6aa607f7ed..8fda936d04 100644 --- a/packages/frontend-core/src/components/grid/stores/datasource.js +++ b/packages/frontend-core/src/components/grid/stores/datasource.ts @@ -1,10 +1,47 @@ -import { derived, get } from "svelte/store" +import { derived, get, Readable, Writable } from "svelte/store" import { getDatasourceDefinition, getDatasourceSchema } from "../../../fetch" import { enrichSchemaWithRelColumns, memo } from "../../../utils" import { cloneDeep } from "lodash" -import { ViewV2Type } from "@budibase/types" +import { + FieldSchema, + SaveTableRequest, + UIDatasource, + UpdateViewRequest, + ViewV2Type, +} from "@budibase/types" +import { Store as StoreContext } from "." +import { DatasourceActions } from "./datasources" -export const createStores = () => { +interface DatasourceStore { + definition: Writable + schemaMutations: Writable> + subSchemaMutations: Writable> +} + +interface DerivedDatasourceStore { + schema: Readable> + enrichedSchema: Readable> + hasBudibaseIdentifiers: Readable +} + +interface ActionDatasourceStore { + datasource: DatasourceStore["definition"] & { + actions: DatasourceActions & { + refreshDefinition: () => Promise + changePrimaryDisplay: any + addSchemaMutation: any + addSubSchemaMutation: any + saveSchemaMutations: any + resetSchemaMutations: any + } + } +} + +export type Store = DatasourceStore & + DerivedDatasourceStore & + ActionDatasourceStore + +export const createStores = (): DatasourceStore => { const definition = memo(null) const schemaMutations = memo({}) const subSchemaMutations = memo({}) @@ -16,7 +53,7 @@ export const createStores = () => { } } -export const deriveStores = context => { +export const deriveStores = (context: StoreContext): DerivedDatasourceStore => { const { API, definition, @@ -27,7 +64,7 @@ export const deriveStores = context => { } = context const schema = derived(definition, $definition => { - let schema = getDatasourceSchema({ + let schema: Record = getDatasourceSchema({ API, datasource: get(datasource), definition: $definition, @@ -40,7 +77,7 @@ export const deriveStores = context => { // Certain datasources like queries use primitives. Object.keys(schema || {}).forEach(key => { if (typeof schema[key] !== "object") { - schema[key] = { type: schema[key] } + schema[key] = { type: schema[key] } as any // TODO } }) @@ -68,9 +105,8 @@ export const deriveStores = context => { if ($subSchemaMutations[field]) { enrichedSchema[field].columns ??= {} - for (const [fieldName, mutation] of Object.entries( - $subSchemaMutations[field] - )) { + for (const fieldName of Object.keys($subSchemaMutations[field])) { + const mutation = $subSchemaMutations[field][fieldName] enrichedSchema[field].columns[fieldName] = { ...enrichedSchema[field].columns[fieldName], ...mutation, @@ -104,7 +140,7 @@ export const deriveStores = context => { } } -export const createActions = context => { +export const createActions = (context: StoreContext): ActionDatasourceStore => { const { API, datasource, @@ -147,7 +183,9 @@ export const createActions = context => { } // Saves the datasource definition - const saveDefinition = async newDefinition => { + const saveDefinition = async ( + newDefinition: UpdateViewRequest | SaveTableRequest + ) => { // Update local state const originalDefinition = get(definition) definition.set(newDefinition) @@ -155,7 +193,7 @@ export const createActions = context => { // Update server if (get(config).canSaveSchema) { try { - await getAPI()?.actions.saveDefinition(newDefinition) + await getAPI()?.actions.saveDefinition(newDefinition as any) // Broadcast change so external state can be updated, as this change // will not be received by the builder websocket because we caused it @@ -242,9 +280,8 @@ export const createActions = context => { } if ($subSchemaMutations[column]) { newSchema[column].columns ??= {} - for (const [fieldName, mutation] of Object.entries( - $subSchemaMutations[column] - )) { + for (const fieldName of Object.keys($subSchemaMutations[column])) { + const mutation = $subSchemaMutations[column][fieldName] newSchema[column].columns[fieldName] = { ...newSchema[column].columns[fieldName], ...mutation, diff --git a/packages/frontend-core/src/components/grid/stores/index.ts b/packages/frontend-core/src/components/grid/stores/index.ts index 1ef5da03b6..d7d922f793 100644 --- a/packages/frontend-core/src/components/grid/stores/index.ts +++ b/packages/frontend-core/src/components/grid/stores/index.ts @@ -59,7 +59,8 @@ export type Store = BaseStore & Columns.Store & Table.Store & ViewV2.Store & - NonPlus.Store & { + NonPlus.Store & + Datasource.Store & { // TODO while typing the rest of stores datasource: Writable & { actions: any } definition: Writable @@ -75,6 +76,9 @@ export type Store = BaseStore & rows: Writable & { actions: any } subscribe: any config: Writable + dispatch: (event: string, data: any) => any + notifications: Writable + schemaOverrides: Writable } export const attachStores = (context: Store): Store => { @@ -106,5 +110,5 @@ export const attachStores = (context: Store): Store => { } } - return context + return context as Store } diff --git a/packages/types/src/ui/stores/grid/datasource.ts b/packages/types/src/ui/stores/grid/datasource.ts index d7367352d5..b1ed806b35 100644 --- a/packages/types/src/ui/stores/grid/datasource.ts +++ b/packages/types/src/ui/stores/grid/datasource.ts @@ -1,5 +1,12 @@ +import { SortOrder } from "@budibase/types" + export interface UIDatasource { type: string id: string tableId: string + sort?: { + field: string + order?: SortOrder + } + queryUI: any // TODO }