From a56f0c91dd60fde7f3b8ac8fa7e0ef6ff8f12b3e Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 21 Jul 2023 12:15:33 +0100 Subject: [PATCH 1/2] Quick improvement to table types, before now the field schema was quite difficult to parse/work out what components of the schema were used for what, this at least separates them into particularly grouped bits of metadata, so it is obvious which parts are used for which. In future we should really flip this a bit, so that FieldSchema is the base implementation, and then each of the types has its own schema extending that base - but that would be a more serious refactor (need to cast to the correct type when using based on the 'type' property. --- packages/types/src/documents/app/table.ts | 97 ------------------ .../src/documents/app/table/constants.ts | 9 ++ .../types/src/documents/app/table/index.ts | 3 + .../types/src/documents/app/table/schema.ts | 98 +++++++++++++++++++ .../types/src/documents/app/table/table.ts | 30 ++++++ 5 files changed, 140 insertions(+), 97 deletions(-) delete mode 100644 packages/types/src/documents/app/table.ts create mode 100644 packages/types/src/documents/app/table/constants.ts create mode 100644 packages/types/src/documents/app/table/index.ts create mode 100644 packages/types/src/documents/app/table/schema.ts create mode 100644 packages/types/src/documents/app/table/table.ts diff --git a/packages/types/src/documents/app/table.ts b/packages/types/src/documents/app/table.ts deleted file mode 100644 index 18b415da5f..0000000000 --- a/packages/types/src/documents/app/table.ts +++ /dev/null @@ -1,97 +0,0 @@ -import { Document } from "../document" -import { View } from "./view" -import { RenameColumn } from "../../sdk" -import { FieldType } from "./row" - -export enum RelationshipTypes { - ONE_TO_MANY = "one-to-many", - MANY_TO_ONE = "many-to-one", - MANY_TO_MANY = "many-to-many", -} - -export enum AutoReason { - FOREIGN_KEY = "foreign_key", -} - -export interface FieldSchema { - type: FieldType - externalType?: string - fieldName?: string - name: string - sortable?: boolean - tableId?: string - relationshipType?: RelationshipTypes - through?: string - foreignKey?: string - icon?: string - autocolumn?: boolean - autoReason?: AutoReason - subtype?: string - throughFrom?: string - throughTo?: string - formula?: string - formulaType?: string - main?: boolean - ignoreTimezones?: boolean - timeOnly?: boolean - lastID?: number - useRichText?: boolean | null - order?: number - width?: number - meta?: { - toTable: string - toKey: string - } - constraints?: { - type?: string - email?: boolean - inclusion?: string[] - length?: { - minimum?: string | number | null - maximum?: string | number | null - } - numericality?: { - greaterThanOrEqualTo: string | null - lessThanOrEqualTo: string | null - } - presence?: - | boolean - | { - allowEmpty?: boolean - } - datetime?: { - latest: string - earliest: string - } - } -} - -export interface TableSchema { - [key: string]: FieldSchema -} - -export interface Table extends Document { - type?: string - views?: { [key: string]: View } - name: string - primary?: string[] - schema: TableSchema - primaryDisplay?: string - sourceId?: string - relatedFormula?: string[] - constrained?: string[] - sql?: boolean - indexes?: { [key: string]: any } - rows?: { [key: string]: any } - created?: boolean - rowHeight?: number -} - -export interface ExternalTable extends Table { - sourceId: string -} - -export interface TableRequest extends Table { - _rename?: RenameColumn - created?: boolean -} diff --git a/packages/types/src/documents/app/table/constants.ts b/packages/types/src/documents/app/table/constants.ts new file mode 100644 index 0000000000..12a347c715 --- /dev/null +++ b/packages/types/src/documents/app/table/constants.ts @@ -0,0 +1,9 @@ +export enum RelationshipTypes { + ONE_TO_MANY = "one-to-many", + MANY_TO_ONE = "many-to-one", + MANY_TO_MANY = "many-to-many", +} + +export enum AutoReason { + FOREIGN_KEY = "foreign_key", +} diff --git a/packages/types/src/documents/app/table/index.ts b/packages/types/src/documents/app/table/index.ts new file mode 100644 index 0000000000..c22788d269 --- /dev/null +++ b/packages/types/src/documents/app/table/index.ts @@ -0,0 +1,3 @@ +export * from "./table" +export * from "./schema" +export * from "./constants" diff --git a/packages/types/src/documents/app/table/schema.ts b/packages/types/src/documents/app/table/schema.ts new file mode 100644 index 0000000000..188a2fddd3 --- /dev/null +++ b/packages/types/src/documents/app/table/schema.ts @@ -0,0 +1,98 @@ +// all added by grid/table when defining the +// column size, position and whether it can be viewed +import { FieldType } from "../row" +import { AutoReason, RelationshipTypes } from "./constants" + +export interface UIFieldMetadata { + order?: number + width?: number + visible?: boolean + icon?: string +} + +export interface RelationshipFieldMetadata { + main?: boolean + fieldName?: string + tableId?: string + // below is used for SQL relationships, needed to define the foreign keys + // or the tables used for many-to-many relationships (through) + relationshipType?: RelationshipTypes + through?: string + foreignKey?: string + throughFrom?: string + throughTo?: string +} + +export interface AutoColumnFieldMetadata { + autocolumn?: boolean + subtype?: string + lastID?: number + // if the column was turned to an auto-column for SQL, explains why (primary, foreign etc) + autoReason?: AutoReason +} + +export interface NumberFieldMetadata { + // used specifically when Budibase generates external tables, this denotes if a number field + // is a foreign key used for a many-to-many relationship + meta?: { + toTable: string + toKey: string + } +} + +export interface DateFieldMetadata { + ignoreTimezones?: boolean + timeOnly?: boolean +} + +export interface StringFieldMetadata { + useRichText?: boolean | null +} + +export interface FormulaFieldMetadata { + formula?: string + formulaType?: string +} + +export interface FieldConstraints { + type?: string + email?: boolean + inclusion?: string[] + length?: { + minimum?: string | number | null + maximum?: string | number | null + } + numericality?: { + greaterThanOrEqualTo: string | null + lessThanOrEqualTo: string | null + } + presence?: + | boolean + | { + allowEmpty?: boolean + } + datetime?: { + latest: string + earliest: string + } +} + +export interface FieldSchema + extends UIFieldMetadata, + DateFieldMetadata, + RelationshipFieldMetadata, + AutoColumnFieldMetadata, + StringFieldMetadata, + FormulaFieldMetadata, + NumberFieldMetadata { + type: FieldType + name: string + sortable?: boolean + // only used by external databases, to denote the real type + externalType?: string + constraints?: FieldConstraints +} + +export interface TableSchema { + [key: string]: FieldSchema +} diff --git a/packages/types/src/documents/app/table/table.ts b/packages/types/src/documents/app/table/table.ts new file mode 100644 index 0000000000..f4dc790267 --- /dev/null +++ b/packages/types/src/documents/app/table/table.ts @@ -0,0 +1,30 @@ +import { Document } from "../../document" +import { View } from "../view" +import { RenameColumn } from "../../../sdk" +import { TableSchema } from "./schema" + +export interface Table extends Document { + type?: string + views?: { [key: string]: View } + name: string + primary?: string[] + schema: TableSchema + primaryDisplay?: string + sourceId?: string + relatedFormula?: string[] + constrained?: string[] + sql?: boolean + indexes?: { [key: string]: any } + rows?: { [key: string]: any } + created?: boolean + rowHeight?: number +} + +export interface ExternalTable extends Table { + sourceId: string +} + +export interface TableRequest extends Table { + _rename?: RenameColumn + created?: boolean +} From 7673673db26d77640ef33e54f6df99d1143901e4 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 21 Jul 2023 12:57:47 +0100 Subject: [PATCH 2/2] Updating RelationshipTypes -> RelationshipType. --- .../DataTable/modals/CreateEditColumn.svelte | 12 +++---- .../Datasources/CreateEditRelationship.svelte | 30 ++++++++-------- .../backend/Datasources/relationshipErrors.js | 4 +-- .../builder/src/constants/backend/index.js | 2 +- packages/server/specs/resources/table.ts | 8 ++--- .../api/controllers/row/ExternalRequest.ts | 4 +-- .../src/api/controllers/table/external.ts | 20 +++++------ packages/server/src/constants/index.ts | 2 +- .../db/defaultData/datasource_bb_default.ts | 6 ++-- .../src/db/linkedRows/LinkController.ts | 20 +++++------ .../src/db/tests/linkController.spec.js | 30 ++++++++-------- .../src/integration-test/postgres.spec.ts | 36 +++++++++---------- .../server/src/integrations/base/sqlTable.ts | 6 ++-- .../server/src/sdk/app/tables/validation.ts | 6 ++-- .../src/documents/app/table/constants.ts | 2 +- .../types/src/documents/app/table/schema.ts | 4 +-- 16 files changed, 93 insertions(+), 99 deletions(-) diff --git a/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte b/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte index 208739a540..dfb028d38d 100644 --- a/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte +++ b/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte @@ -18,7 +18,7 @@ import { TableNames, UNEDITABLE_USER_FIELDS } from "constants" import { FIELDS, - RelationshipTypes, + RelationshipType, ALLOWABLE_STRING_OPTIONS, ALLOWABLE_NUMBER_OPTIONS, ALLOWABLE_STRING_TYPES, @@ -183,7 +183,7 @@ dispatch("updatecolumns") if ( saveColumn.type === LINK_TYPE && - saveColumn.relationshipType === RelationshipTypes.MANY_TO_MANY + saveColumn.relationshipType === RelationshipType.MANY_TO_MANY ) { // Fetching the new tables tables.fetch() @@ -237,7 +237,7 @@ // Default relationships many to many if (editableColumn.type === LINK_TYPE) { - editableColumn.relationshipType = RelationshipTypes.MANY_TO_MANY + editableColumn.relationshipType = RelationshipType.MANY_TO_MANY } if (editableColumn.type === FORMULA_TYPE) { editableColumn.formulaType = "dynamic" @@ -285,17 +285,17 @@ { name: `Many ${thisName} rows → many ${linkName} rows`, alt: `Many ${table.name} rows → many ${linkTable.name} rows`, - value: RelationshipTypes.MANY_TO_MANY, + value: RelationshipType.MANY_TO_MANY, }, { name: `One ${linkName} row → many ${thisName} rows`, alt: `One ${linkTable.name} rows → many ${table.name} rows`, - value: RelationshipTypes.ONE_TO_MANY, + value: RelationshipType.ONE_TO_MANY, }, { name: `One ${thisName} row → many ${linkName} rows`, alt: `One ${table.name} rows → many ${linkTable.name} rows`, - value: RelationshipTypes.MANY_TO_ONE, + value: RelationshipType.MANY_TO_ONE, }, ] } diff --git a/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte b/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte index 53fcf56e7f..36c6a32801 100644 --- a/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte +++ b/packages/builder/src/components/backend/Datasources/CreateEditRelationship.svelte @@ -1,5 +1,5 @@