From fe6535a65f301d1c7fa4f8b6c001e42c4877184f Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 5 Oct 2023 12:06:31 +0200 Subject: [PATCH] Type schema fields --- packages/server/src/constants/index.ts | 20 ++---- .../src/documents/app/table/constants.ts | 13 ++++ .../types/src/documents/app/table/schema.ts | 72 ++++++++++++------- 3 files changed, 66 insertions(+), 39 deletions(-) diff --git a/packages/server/src/constants/index.ts b/packages/server/src/constants/index.ts index c8c17e1d32..326389996d 100644 --- a/packages/server/src/constants/index.ts +++ b/packages/server/src/constants/index.ts @@ -1,6 +1,11 @@ import { objectStore, roles, constants } from "@budibase/backend-core" import { FieldType as FieldTypes } from "@budibase/types" -export { FieldType as FieldTypes, RelationshipType } from "@budibase/types" +export { + FieldType as FieldTypes, + RelationshipType, + AutoFieldSubTypes, + FormulaTypes, +} from "@budibase/types" export enum FilterTypes { STRING = "string", @@ -39,11 +44,6 @@ export const SwitchableTypes = CanSwitchTypes.reduce((prev, current) => prev ? prev.concat(current) : current ) -export enum FormulaTypes { - STATIC = "static", - DYNAMIC = "dynamic", -} - export enum AuthTypes { APP = "app", BUILDER = "builder", @@ -132,14 +132,6 @@ export const USERS_TABLE_SCHEMA = { primaryDisplay: "email", } -export enum AutoFieldSubTypes { - CREATED_BY = "createdBy", - CREATED_AT = "createdAt", - UPDATED_BY = "updatedBy", - UPDATED_AT = "updatedAt", - AUTO_ID = "autoID", -} - export enum AutoFieldDefaultNames { CREATED_BY = "Created By", CREATED_AT = "Created At", diff --git a/packages/types/src/documents/app/table/constants.ts b/packages/types/src/documents/app/table/constants.ts index 9a0ea4d135..783eae0671 100644 --- a/packages/types/src/documents/app/table/constants.ts +++ b/packages/types/src/documents/app/table/constants.ts @@ -7,3 +7,16 @@ export enum RelationshipType { export enum AutoReason { FOREIGN_KEY = "foreign_key", } + +export enum AutoFieldSubTypes { + CREATED_BY = "createdBy", + CREATED_AT = "createdAt", + UPDATED_BY = "updatedBy", + UPDATED_AT = "updatedAt", + AUTO_ID = "autoID", +} + +export enum FormulaTypes { + STATIC = "static", + DYNAMIC = "dynamic", +} diff --git a/packages/types/src/documents/app/table/schema.ts b/packages/types/src/documents/app/table/schema.ts index 42a0838231..5e7fba25b9 100644 --- a/packages/types/src/documents/app/table/schema.ts +++ b/packages/types/src/documents/app/table/schema.ts @@ -1,7 +1,12 @@ // all added by grid/table when defining the // column size, position and whether it can be viewed import { FieldType } from "../row" -import { AutoReason, RelationshipType } from "./constants" +import { + AutoFieldSubTypes, + AutoReason, + FormulaTypes, + RelationshipType, +} from "./constants" export interface UIFieldMetadata { order?: number @@ -10,20 +15,25 @@ export interface UIFieldMetadata { icon?: string } -export interface RelationshipFieldMetadata { +interface ManyToManyRelationshipFieldMetadata { + relationshipType: RelationshipType.MANY_TO_MANY + through: string + throughFrom: string + throughTo: string +} +interface OneSidedRelationshipFieldMetadata { + relationshipType: RelationshipType.ONE_TO_MANY | RelationshipType.MANY_TO_ONE + foreignKey: string +} +export type RelationshipFieldMetadata = BaseFieldSchema & { + type: FieldType.LINK 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?: RelationshipType - through?: string - foreignKey?: string - throughFrom?: string - throughTo?: string -} + tableId: string +} & (ManyToManyRelationshipFieldMetadata | OneSidedRelationshipFieldMetadata) -export interface AutoColumnFieldMetadata { +export interface AutoColumnFieldMetadata extends BaseFieldSchema { + type: FieldType.AUTO autocolumn?: boolean subtype?: string lastID?: number @@ -31,7 +41,10 @@ export interface AutoColumnFieldMetadata { autoReason?: AutoReason } -export interface NumberFieldMetadata { +interface NumberForeignKeyMetadata { + subtype: AutoFieldSubTypes.AUTO_ID + autoReason: AutoReason.FOREIGN_KEY + autocolumn: true // 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?: { @@ -40,18 +53,26 @@ export interface NumberFieldMetadata { } } -export interface DateFieldMetadata { +export type NumberFieldMetadata = BaseFieldSchema & { + type: FieldType.NUMBER + autocolumn: boolean +} & (NumberForeignKeyMetadata | {}) + +export interface DateFieldMetadata extends BaseFieldSchema { + type: FieldType.DATETIME ignoreTimezones?: boolean timeOnly?: boolean } -export interface StringFieldMetadata { +export interface StringFieldMetadata extends BaseFieldSchema { + type: FieldType.STRING useRichText?: boolean | null } -export interface FormulaFieldMetadata { +export interface FormulaFieldMetadata extends BaseFieldSchema { + type: FieldType.FORMULA formula?: string - formulaType?: string + formulaType?: FormulaTypes } export interface FieldConstraints { @@ -77,14 +98,7 @@ export interface FieldConstraints { } } -export interface FieldSchema - extends UIFieldMetadata, - DateFieldMetadata, - RelationshipFieldMetadata, - AutoColumnFieldMetadata, - StringFieldMetadata, - FormulaFieldMetadata, - NumberFieldMetadata { +interface BaseFieldSchema extends UIFieldMetadata { type: FieldType name: string sortable?: boolean @@ -93,6 +107,14 @@ export interface FieldSchema constraints?: FieldConstraints } +export type FieldSchema = + | DateFieldMetadata + | RelationshipFieldMetadata + | AutoColumnFieldMetadata + | StringFieldMetadata + | FormulaFieldMetadata + | NumberFieldMetadata + export interface TableSchema { [key: string]: FieldSchema }