Typing defititions

This commit is contained in:
Adria Navarro 2025-01-14 11:25:26 +01:00
parent bd1a04ff1b
commit bcbf16a69b
2 changed files with 32 additions and 11 deletions

View File

@ -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. // 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 { derived, get, Readable, Writable } from "svelte/store"
import { getDatasourceDefinition, getDatasourceSchema } from "../../../fetch" import {
DataFetchDefinition,
getDatasourceDefinition,
getDatasourceSchema,
} from "../../../fetch"
import { enrichSchemaWithRelColumns, memo } from "../../../utils" import { enrichSchemaWithRelColumns, memo } from "../../../utils"
import { cloneDeep } from "lodash" import { cloneDeep } from "lodash"
import { import {
@ -18,7 +22,7 @@ import { Store as StoreContext, BaseStoreProps } from "."
import { DatasourceActions } from "./datasources" import { DatasourceActions } from "./datasources"
interface DatasourceStore { interface DatasourceStore {
definition: Writable<UIDatasource | null> definition: Writable<DataFetchDefinition | null>
schemaMutations: Writable<Record<string, UIFieldMutation>> schemaMutations: Writable<Record<string, UIFieldMutation>>
subSchemaMutations: Writable<Record<string, Record<string, UIFieldMutation>>> subSchemaMutations: Writable<Record<string, Record<string, UIFieldMutation>>>
} }
@ -131,11 +135,17 @@ export const deriveStores = (context: StoreContext): DerivedDatasourceStore => {
[datasource, definition], [datasource, definition],
([$datasource, $definition]) => { ([$datasource, $definition]) => {
let type = $datasource?.type let type = $datasource?.type
// @ts-expect-error
if (type === "provider") { if (type === "provider") {
type = ($datasource as any).value?.datasource?.type // TODO: see line 1 type = ($datasource as any).value?.datasource?.type // TODO: see line 1
} }
// Handle calculation views // Handle calculation views
if (type === "viewV2" && $definition?.type === ViewV2Type.CALCULATION) { if (
type === "viewV2" &&
$definition &&
"type" in $definition &&
$definition.type === ViewV2Type.CALCULATION
) {
return false return false
} }
return !!type && ["table", "viewV2", "link"].includes(type) return !!type && ["table", "viewV2", "link"].includes(type)
@ -197,7 +207,7 @@ export const createActions = (context: StoreContext): ActionDatasourceStore => {
) => { ) => {
// Update local state // Update local state
const originalDefinition = get(definition) const originalDefinition = get(definition)
definition.set(newDefinition as UIDatasource) definition.set(newDefinition)
// Update server // Update server
if (get(config).canSaveSchema) { if (get(config).canSaveSchema) {
@ -225,6 +235,7 @@ export const createActions = (context: StoreContext): ActionDatasourceStore => {
// Update primary display // Update primary display
newDefinition.primaryDisplay = column newDefinition.primaryDisplay = column
if (newDefinition.schema) {
// Sanitise schema to ensure field is required and has no default value // Sanitise schema to ensure field is required and has no default value
if (!newDefinition.schema[column].constraints) { if (!newDefinition.schema[column].constraints) {
newDefinition.schema[column].constraints = {} newDefinition.schema[column].constraints = {}
@ -233,6 +244,7 @@ export const createActions = (context: StoreContext): ActionDatasourceStore => {
if ("default" in newDefinition.schema[column]) { if ("default" in newDefinition.schema[column]) {
delete newDefinition.schema[column].default delete newDefinition.schema[column].default
} }
}
return await saveDefinition(newDefinition as any) // TODO: see line 1 return await saveDefinition(newDefinition as any) // TODO: see line 1
} }

View File

@ -11,6 +11,7 @@ import GroupUserFetch from "./GroupUserFetch"
import CustomFetch from "./CustomFetch" import CustomFetch from "./CustomFetch"
import QueryArrayFetch from "./QueryArrayFetch" import QueryArrayFetch from "./QueryArrayFetch"
import { APIClient } from "../api/types" import { APIClient } from "../api/types"
import { Table, ViewV2Enriched } from "@budibase/types"
export type DataFetchType = keyof typeof DataFetchMap export type DataFetchType = keyof typeof DataFetchMap
@ -45,6 +46,14 @@ export type DataFetch =
| JSONArrayFetch | JSONArrayFetch
| QueryArrayFetch | QueryArrayFetch
export type DataFetchDefinition =
| Table
| ViewV2Enriched
| {
schema?: Record<string, any> | null
primaryDisplay?: string
}
// Constructs a new fetch model for a certain datasource // Constructs a new fetch model for a certain datasource
export const fetchData = <TDatasource extends { type: DataFetchType }>({ export const fetchData = <TDatasource extends { type: DataFetchType }>({
API, API,