From da6136a1085c7e1e2d70e5b1d3c9b1ff5954a962 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 31 Jul 2023 17:15:51 +0200 Subject: [PATCH] Enrich schema using the new data --- packages/server/src/sdk/app/views/index.ts | 65 ++++++++++--------- .../src/sdk/app/views/tests/views.spec.ts | 46 +++++++++---- 2 files changed, 66 insertions(+), 45 deletions(-) diff --git a/packages/server/src/sdk/app/views/index.ts b/packages/server/src/sdk/app/views/index.ts index 244ac899c1..2cda79b470 100644 --- a/packages/server/src/sdk/app/views/index.ts +++ b/packages/server/src/sdk/app/views/index.ts @@ -1,5 +1,11 @@ import { HTTPError, context } from "@budibase/backend-core" -import { TableSchema, UIFieldMetadata, View, ViewV2 } from "@budibase/types" +import { + FieldSchema, + TableSchema, + UIFieldMetadata, + View, + ViewV2, +} from "@budibase/types" import sdk from "../../../sdk" import * as utils from "../../../db/utils" @@ -73,37 +79,34 @@ export function enrichSchema(view: View | ViewV2, tableSchema: TableSchema) { return view } + let schema = { ...tableSchema } + if (view.schemaUI) { + const viewOverridesEntries = Object.entries(view.schemaUI) + const viewSetsOrder = viewOverridesEntries.some(([_, v]) => v.order) + for (const [fieldName, schemaUI] of viewOverridesEntries) { + schema[fieldName] = { + ...schema[fieldName], + ...schemaUI, + order: viewSetsOrder + ? schemaUI.order || undefined + : schema[fieldName].order, + } + } + } + + if (view?.columns?.length) { + const pickedSchema: Record = {} + for (const fieldName of view.columns) { + if (!schema[fieldName]) { + continue + } + pickedSchema[fieldName] = { ...schema[fieldName] } + } + schema = pickedSchema + } + return { ...view, - schema: - !view?.columns || !Object.entries(view?.columns).length - ? tableSchema - : enrichViewV2Schema(tableSchema, view.columns), + schema: schema, } } - -function enrichViewV2Schema( - tableSchema: TableSchema, - viewOverrides: Record -) { - const result: TableSchema = {} - const viewOverridesEntries = Object.entries(viewOverrides) - const viewSetsOrder = viewOverridesEntries.some(([_, v]) => v.order) - for (const [columnName, columnUIMetadata] of viewOverridesEntries) { - if (!columnUIMetadata.visible) { - continue - } - - if (!tableSchema[columnName]) { - continue - } - - const tableFieldSchema = tableSchema[columnName] - if (viewSetsOrder) { - delete tableFieldSchema.order - } - - result[columnName] = merge(tableFieldSchema, columnUIMetadata) - } - return result -} diff --git a/packages/server/src/sdk/app/views/tests/views.spec.ts b/packages/server/src/sdk/app/views/tests/views.spec.ts index cbcd98eb91..3b1cb84a42 100644 --- a/packages/server/src/sdk/app/views/tests/views.spec.ts +++ b/packages/server/src/sdk/app/views/tests/views.spec.ts @@ -102,18 +102,14 @@ describe("table sdk", () => { }) }) - it("if view schema only defines visiblility, should only fetch the selected fields", async () => { + it("if view schema only defines columns, should only fetch the selected fields", async () => { const tableId = basicTable._id! const view: ViewV2 = { version: 2, id: generator.guid(), name: generator.guid(), tableId, - columns: { - name: { visible: true }, - id: { visible: true }, - description: { visible: false }, - }, + columns: ["name", "id"], } const res = enrichSchema(view, basicTable.schema) @@ -151,7 +147,7 @@ describe("table sdk", () => { id: generator.guid(), name: generator.guid(), tableId, - columns: { unnexisting: { visible: true }, name: { visible: true } }, + columns: ["unnexisting", "name"], } const res = enrichSchema(view, basicTable.schema) @@ -175,16 +171,17 @@ describe("table sdk", () => { ) }) - it("if view schema only defines visiblility, should only fetch the selected fields", async () => { + it("if the view schema overrides the schema UI, the table schema should be overridden", async () => { const tableId = basicTable._id! const view: ViewV2 = { version: 2, id: generator.guid(), name: generator.guid(), tableId, - columns: { - name: { visible: true }, - id: { visible: true }, + columns: ["name", "id", "description"], + schemaUI: { + name: { visible: true, width: 100 }, + id: { visible: true, width: 20 }, description: { visible: false }, }, } @@ -200,7 +197,7 @@ describe("table sdk", () => { name: "name", order: 2, visible: true, - width: 80, + width: 100, constraints: { type: "string", }, @@ -210,23 +207,34 @@ describe("table sdk", () => { name: "id", order: 1, visible: true, + width: 20, constraints: { type: "number", }, }, + description: { + type: "string", + name: "description", + visible: false, + width: 200, + constraints: { + type: "string", + }, + }, }, }) ) }) - it("if view defines order, the table schema order should be ignored", async () => { + it("if the view defines order, the table schema order should be ignored", async () => { const tableId = basicTable._id! const view: ViewV2 = { version: 2, id: generator.guid(), name: generator.guid(), tableId, - columns: { + columns: ["name", "id", "description"], + schemaUI: { name: { visible: true, order: 1 }, id: { visible: true }, description: { visible: false, order: 2 }, @@ -257,6 +265,16 @@ describe("table sdk", () => { type: "number", }, }, + description: { + type: "string", + name: "description", + order: 2, + visible: false, + width: 200, + constraints: { + type: "string", + }, + }, }, }) )