From 9371f9913e7bded6e870d3b272bb63045a83027c Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 30 Jul 2024 18:14:54 +0200 Subject: [PATCH] Types --- .../src/api/routes/tests/datasource.spec.ts | 10 +++++- .../server/src/api/routes/tests/row.spec.ts | 4 ++- .../src/api/routes/tests/search.spec.ts | 6 +++- .../server/src/api/routes/tests/table.spec.ts | 1 + .../src/api/routes/tests/viewV2.spec.ts | 4 +++ .../db/defaultData/datasource_bb_default.ts | 9 ++--- .../server/src/integrations/utils/utils.ts | 36 +++++++++++-------- .../sdk/app/rows/search/tests/search.spec.ts | 9 ++++- 8 files changed, 56 insertions(+), 23 deletions(-) diff --git a/packages/server/src/api/routes/tests/datasource.spec.ts b/packages/server/src/api/routes/tests/datasource.spec.ts index 6f249f5016..255e46167f 100644 --- a/packages/server/src/api/routes/tests/datasource.spec.ts +++ b/packages/server/src/api/routes/tests/datasource.spec.ts @@ -15,6 +15,7 @@ import { Table, TableSchema, SupportedSqlTypes, + JsonFieldSubType, } from "@budibase/types" import { DatabaseName, getDatasource } from "../../../integrations/tests/utils" import { tableForDatasource } from "../../../tests/utilities/structures" @@ -288,7 +289,10 @@ describe("/datasources", () => { name: "options", type: FieldType.OPTIONS, constraints: { - presence: { allowEmpty: false }, + presence: { + allowEmpty: false, + }, + inclusion: [], }, }, [FieldType.NUMBER]: { @@ -302,6 +306,10 @@ describe("/datasources", () => { [FieldType.ARRAY]: { name: "array", type: FieldType.ARRAY, + constraints: { + type: JsonFieldSubType.ARRAY, + inclusion: [], + }, }, [FieldType.DATETIME]: { name: "datetime", diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index 9cc53168d0..904ab93e13 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -32,6 +32,7 @@ import { TableSourceType, UpdatedRowEventEmitter, TableSchema, + JsonFieldSubType, } from "@budibase/types" import { generator, mocks } from "@budibase/backend-core/tests" import _, { merge } from "lodash" @@ -103,6 +104,7 @@ describe.each([ const defaultSchema: TableSchema = { id: { type: FieldType.AUTO, + subtype: AutoFieldSubType.AUTO_ID, name: "id", autocolumn: true, constraints: { @@ -384,7 +386,7 @@ describe.each([ const arrayField: FieldSchema = { type: FieldType.ARRAY, constraints: { - type: "array", + type: JsonFieldSubType.ARRAY, presence: false, inclusion: ["One", "Two", "Three"], }, diff --git a/packages/server/src/api/routes/tests/search.spec.ts b/packages/server/src/api/routes/tests/search.spec.ts index bad512e5fd..2c5756efe4 100644 --- a/packages/server/src/api/routes/tests/search.spec.ts +++ b/packages/server/src/api/routes/tests/search.spec.ts @@ -20,6 +20,7 @@ import { Datasource, EmptyFilterOption, FieldType, + JsonFieldSubType, RelationshipType, Row, RowSearchParams, @@ -1494,7 +1495,10 @@ describe.each([ numbers: { name: "numbers", type: FieldType.ARRAY, - constraints: { inclusion: ["one", "two", "three"] }, + constraints: { + type: JsonFieldSubType.ARRAY, + inclusion: ["one", "two", "three"], + }, }, }) await createRows([{ numbers: ["one", "two"] }, { numbers: ["three"] }]) diff --git a/packages/server/src/api/routes/tests/table.spec.ts b/packages/server/src/api/routes/tests/table.spec.ts index 20c83549d2..a2cff7b395 100644 --- a/packages/server/src/api/routes/tests/table.spec.ts +++ b/packages/server/src/api/routes/tests/table.spec.ts @@ -398,6 +398,7 @@ describe.each([ name: "auto", autocolumn: true, type: FieldType.AUTO, + subtype: AutoFieldSubType.AUTO_ID, }, }, }, diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index 3edbc24365..d97ded626d 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -19,6 +19,7 @@ import { ViewV2, SearchResponse, BasicOperator, + AutoFieldSubType, } from "@budibase/types" import { generator, mocks } from "@budibase/backend-core/tests" import { DatabaseName, getDatasource } from "../../../integrations/tests/utils" @@ -57,6 +58,7 @@ describe.each([ schema: { id: { type: FieldType.AUTO, + subtype: AutoFieldSubType.AUTO_ID, name: "id", autocolumn: true, constraints: { @@ -242,6 +244,7 @@ describe.each([ id: { name: "id", type: FieldType.AUTO, + subtype: AutoFieldSubType.AUTO_ID, autocolumn: true, visible: true, }, @@ -1556,6 +1559,7 @@ describe.each([ id: { name: "id", type: FieldType.AUTO, + subtype: AutoFieldSubType.AUTO_ID, autocolumn: true, }, name: { diff --git a/packages/server/src/db/defaultData/datasource_bb_default.ts b/packages/server/src/db/defaultData/datasource_bb_default.ts index fc79b90c00..a393888e51 100644 --- a/packages/server/src/db/defaultData/datasource_bb_default.ts +++ b/packages/server/src/db/defaultData/datasource_bb_default.ts @@ -17,6 +17,7 @@ import { AutoFieldSubType, Datasource, FieldType, + JsonFieldSubType, RelationshipType, Row, SourceName, @@ -131,7 +132,7 @@ export const DEFAULT_INVENTORY_TABLE_SCHEMA: Table = { "Item Tags": { type: FieldType.ARRAY, constraints: { - type: FieldType.ARRAY, + type: JsonFieldSubType.ARRAY, presence: { allowEmpty: false, }, @@ -153,7 +154,7 @@ export const DEFAULT_INVENTORY_TABLE_SCHEMA: Table = { Status: { type: FieldType.ARRAY, constraints: { - type: FieldType.ARRAY, + type: JsonFieldSubType.ARRAY, presence: { allowEmpty: false, }, @@ -291,7 +292,7 @@ export const DEFAULT_EMPLOYEE_TABLE_SCHEMA: Table = { "Employee Level": { type: FieldType.ARRAY, constraints: { - type: FieldType.ARRAY, + type: JsonFieldSubType.ARRAY, presence: false, inclusion: ["Manager", "Junior", "Senior", "Apprentice", "Contractor"], }, @@ -535,7 +536,7 @@ export const DEFAULT_EXPENSES_TABLE_SCHEMA: Table = { "Expense Tags": { type: FieldType.ARRAY, constraints: { - type: FieldType.ARRAY, + type: JsonFieldSubType.ARRAY, presence: { allowEmpty: false, }, diff --git a/packages/server/src/integrations/utils/utils.ts b/packages/server/src/integrations/utils/utils.ts index b97782ce7e..84742626c1 100644 --- a/packages/server/src/integrations/utils/utils.ts +++ b/packages/server/src/integrations/utils/utils.ts @@ -150,22 +150,28 @@ export function generateColumnDefinition(config: { }).internal } - const constraints: { - presence: boolean - inclusion?: string[] - } = { - presence, - } + let schema: FieldSchema if (foundType === FieldType.OPTIONS) { - constraints.inclusion = options - } - - const schema: FieldSchema = { - type: foundType, - externalType, - autocolumn, - name, - constraints, + schema = { + type: foundType, + externalType, + autocolumn, + name, + constraints: { + presence, + inclusion: options!, + }, + } + } else { + schema = { + type: foundType, + externalType, + autocolumn, + name, + constraints: { + presence, + }, + } } if (schema.type === FieldType.DATETIME) { schema.dateOnly = SQL_DATE_ONLY_TYPES.includes(lowerCaseType) diff --git a/packages/server/src/sdk/app/rows/search/tests/search.spec.ts b/packages/server/src/sdk/app/rows/search/tests/search.spec.ts index 4b80d5bcd3..782b580d68 100644 --- a/packages/server/src/sdk/app/rows/search/tests/search.spec.ts +++ b/packages/server/src/sdk/app/rows/search/tests/search.spec.ts @@ -1,4 +1,10 @@ -import { Datasource, FieldType, Row, Table } from "@budibase/types" +import { + AutoFieldSubType, + Datasource, + FieldType, + Row, + Table, +} from "@budibase/types" import TestConfiguration from "../../../../../tests/utilities/TestConfiguration" import { search } from "../../../../../sdk/app/rows/search" @@ -56,6 +62,7 @@ describe.each([ id: { name: "id", type: FieldType.AUTO, + subtype: AutoFieldSubType.AUTO_ID, autocolumn: true, }, name: {