From 878e09cfb0953b9fb173ad394516ec599bbddac7 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 31 Jul 2023 17:04:32 +0200 Subject: [PATCH 01/13] Change record --- packages/types/src/documents/app/view.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/types/src/documents/app/view.ts b/packages/types/src/documents/app/view.ts index aeef95ed5b..10fcac2805 100644 --- a/packages/types/src/documents/app/view.ts +++ b/packages/types/src/documents/app/view.ts @@ -25,7 +25,8 @@ export interface ViewV2 { order?: SortOrder type?: SortType } - columns?: Record + columns?: string[] + schemaUI?: Record } export type ViewSchema = ViewCountOrSumSchema | ViewStatisticsSchema From da6136a1085c7e1e2d70e5b1d3c9b1ff5954a962 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 31 Jul 2023 17:15:51 +0200 Subject: [PATCH 02/13] 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", + }, + }, }, }) ) From 1b2ce184d9af1c253273c8d52ee2a3a3dc9c8b8d Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 31 Jul 2023 17:22:10 +0200 Subject: [PATCH 03/13] Fix types --- packages/server/src/api/routes/tests/viewV2.spec.ts | 3 ++- packages/server/src/sdk/app/views/index.ts | 8 +------- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index e728af3e40..2660864948 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -40,7 +40,8 @@ describe("/v2/views", () => { order: SortOrder.DESCENDING, type: SortType.STRING, }, - columns: { + columns: ["name"], + schemaUI: { name: { visible: true, }, diff --git a/packages/server/src/sdk/app/views/index.ts b/packages/server/src/sdk/app/views/index.ts index 2cda79b470..3dfa82df0d 100644 --- a/packages/server/src/sdk/app/views/index.ts +++ b/packages/server/src/sdk/app/views/index.ts @@ -1,11 +1,5 @@ import { HTTPError, context } from "@budibase/backend-core" -import { - FieldSchema, - TableSchema, - UIFieldMetadata, - View, - ViewV2, -} from "@budibase/types" +import { FieldSchema, TableSchema, View, ViewV2 } from "@budibase/types" import sdk from "../../../sdk" import * as utils from "../../../db/utils" From 73ded07484b44a3bbd0bf7d3ffb1b91c060c3f12 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 31 Jul 2023 17:23:34 +0200 Subject: [PATCH 04/13] Fix tests --- packages/server/src/api/routes/tests/row.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index dbc417a5b5..baec8ddb85 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -983,7 +983,7 @@ describe("/rows", () => { } const view = await config.api.viewV2.create({ - columns: { name: { visible: true } }, + columns: ["name"], }) const response = await config.api.viewV2.search(view.id) From b528257bbee0a0981c65f0b88560a5028853232b Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 1 Aug 2023 10:45:00 +0200 Subject: [PATCH 05/13] Change viewrequest to accept schema --- .../src/api/controllers/view/viewsV2.ts | 11 +++- .../src/api/routes/tests/viewV2.spec.ts | 53 +++++++++++++++++-- .../server/src/tests/utilities/api/viewV2.ts | 7 +++ packages/types/src/api/web/app/view.ts | 7 ++- 4 files changed, 71 insertions(+), 7 deletions(-) diff --git a/packages/server/src/api/controllers/view/viewsV2.ts b/packages/server/src/api/controllers/view/viewsV2.ts index 9a4949bb43..b2a9e2f223 100644 --- a/packages/server/src/api/controllers/view/viewsV2.ts +++ b/packages/server/src/api/controllers/view/viewsV2.ts @@ -4,13 +4,22 @@ import { Ctx, UpdateViewRequest, ViewResponse, + ViewV2, } from "@budibase/types" export async function create(ctx: Ctx) { const view = ctx.request.body const { tableId } = view - const result = await sdk.views.create(tableId, view) + const parsedView: Omit = { + name: view.name, + tableId: view.tableId, + query: view.query, + sort: view.sort, + columns: view.schema && Object.keys(view.schema), + schemaUI: view.schema, + } + const result = await sdk.views.create(tableId, parsedView) ctx.status = 201 ctx.body = { data: result, diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index 2660864948..e2b81f4cf0 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -40,9 +40,10 @@ describe("/v2/views", () => { order: SortOrder.DESCENDING, type: SortType.STRING, }, - columns: ["name"], - schemaUI: { + schema: { name: { + name: "name", + type: FieldType.STRING, visible: true, }, }, @@ -74,17 +75,61 @@ describe("/v2/views", () => { const newView: CreateViewRequest = { name: generator.name(), tableId: config.table!._id!, - ...viewFilters, + query: viewFilters.query, + sort: viewFilters.sort, } + delete newView.schema const res = await config.api.viewV2.create(newView) expect(res).toEqual({ ...newView, - ...viewFilters, + query: viewFilters.query, + sort: viewFilters.sort, id: expect.any(String), version: 2, }) }) + + it("persist schema overrides", async () => { + const newView: CreateViewRequest = { + name: generator.name(), + tableId: config.table!._id!, + schema: { + name: { + name: "name", + type: FieldType.STRING, + visible: true, + }, + lastname: { + name: "lastname", + type: FieldType.STRING, + visible: false, + }, + }, + } + + const createdView = await config.api.viewV2.create(newView) + + expect(await config.api.viewV2.get(createdView.id)).toEqual({ + ...newView, + schema: undefined, + columns: ["name", "lastname"], + schemaUI: { + name: { + name: "name", + type: FieldType.STRING, + visible: true, + }, + lastname: { + name: "lastname", + type: FieldType.STRING, + visible: false, + }, + }, + id: createdView.id, + version: 2, + }) + }) }) describe("update", () => { diff --git a/packages/server/src/tests/utilities/api/viewV2.ts b/packages/server/src/tests/utilities/api/viewV2.ts index fae0850f79..1f7fe6e2bb 100644 --- a/packages/server/src/tests/utilities/api/viewV2.ts +++ b/packages/server/src/tests/utilities/api/viewV2.ts @@ -3,6 +3,7 @@ import TestConfiguration from "../TestConfiguration" import { TestAPI } from "./base" import { generator } from "@budibase/backend-core/tests" import { Response } from "superagent" +import sdk from "../../../sdk" export class ViewV2API extends TestAPI { constructor(config: TestConfiguration) { @@ -62,6 +63,12 @@ export class ViewV2API extends TestAPI { .expect(expectStatus) } + get = async (viewId: string) => { + return await this.config.doInContext(this.config.appId, () => + sdk.views.get(viewId) + ) + } + search = async ( viewId: string, options?: { diff --git a/packages/types/src/api/web/app/view.ts b/packages/types/src/api/web/app/view.ts index 6b516c0314..b694521678 100644 --- a/packages/types/src/api/web/app/view.ts +++ b/packages/types/src/api/web/app/view.ts @@ -1,9 +1,12 @@ -import { TableSchema, ViewV2 } from "../../../documents" +import { ViewV2, FieldSchema } from "../../../documents" export interface ViewResponse { data: ViewV2 } -export type CreateViewRequest = Omit +export interface CreateViewRequest + extends Omit { + schema?: Record +} export type UpdateViewRequest = ViewV2 From bce75c91a6a83d29c9c826c7992e1b99f9d400f1 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 1 Aug 2023 10:57:03 +0200 Subject: [PATCH 06/13] Persist only UI schema overrides --- .../server/src/api/controllers/view/viewsV2.ts | 16 +++++++++++++++- .../server/src/api/routes/tests/viewV2.spec.ts | 12 +++++++----- packages/types/src/index.ts | 1 + packages/types/src/shared/typeUtils.ts | 4 ++++ 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/packages/server/src/api/controllers/view/viewsV2.ts b/packages/server/src/api/controllers/view/viewsV2.ts index b2a9e2f223..c872abba39 100644 --- a/packages/server/src/api/controllers/view/viewsV2.ts +++ b/packages/server/src/api/controllers/view/viewsV2.ts @@ -2,22 +2,36 @@ import sdk from "../../../sdk" import { CreateViewRequest, Ctx, + UIFieldMetadata, UpdateViewRequest, ViewResponse, ViewV2, + RequiredKeys, } from "@budibase/types" export async function create(ctx: Ctx) { const view = ctx.request.body const { tableId } = view + const schemaUI = + view.schema && + Object.entries(view.schema).reduce((p, [fieldName, schemaValue]) => { + p[fieldName] = { + order: schemaValue.order, + width: schemaValue.width, + visible: schemaValue.visible, + icon: schemaValue.icon, + } + return p + }, {} as Record>) + const parsedView: Omit = { name: view.name, tableId: view.tableId, query: view.query, sort: view.sort, columns: view.schema && Object.keys(view.schema), - schemaUI: view.schema, + schemaUI, } const result = await sdk.views.create(tableId, parsedView) ctx.status = 201 diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index e2b81f4cf0..14fe90b93e 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -90,7 +90,7 @@ describe("/v2/views", () => { }) }) - it("persist schema overrides", async () => { + it("persist only UI schema overrides", async () => { const newView: CreateViewRequest = { name: generator.name(), tableId: config.table!._id!, @@ -99,11 +99,14 @@ describe("/v2/views", () => { name: "name", type: FieldType.STRING, visible: true, + order: 1, + width: 100, }, lastname: { name: "lastname", type: FieldType.STRING, visible: false, + icon: "ic", }, }, } @@ -116,14 +119,13 @@ describe("/v2/views", () => { columns: ["name", "lastname"], schemaUI: { name: { - name: "name", - type: FieldType.STRING, visible: true, + order: 1, + width: 100, }, lastname: { - name: "lastname", - type: FieldType.STRING, visible: false, + icon: "ic", }, }, id: createdView.id, diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index 4adb2fda97..ad1688ef52 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -1,3 +1,4 @@ export * from "./documents" export * from "./sdk" export * from "./api" +export * from "./shared" diff --git a/packages/types/src/shared/typeUtils.ts b/packages/types/src/shared/typeUtils.ts index fbe215fdb9..df0e049455 100644 --- a/packages/types/src/shared/typeUtils.ts +++ b/packages/types/src/shared/typeUtils.ts @@ -3,3 +3,7 @@ export type DeepPartial = { } export type ISO8601 = string + +export type RequiredKeys = { + [K in keyof Required]: T[K] +} From d440291ebc5ddb9b6c439af44db3d6b38c69ef22 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 1 Aug 2023 11:31:58 +0200 Subject: [PATCH 07/13] Throw exception when updating non ui fields --- .../src/api/controllers/view/viewsV2.ts | 45 ++++++++++++- .../src/api/routes/tests/viewV2.spec.ts | 64 ++++++++++++++++--- 2 files changed, 98 insertions(+), 11 deletions(-) diff --git a/packages/server/src/api/controllers/view/viewsV2.ts b/packages/server/src/api/controllers/view/viewsV2.ts index c872abba39..f403d14180 100644 --- a/packages/server/src/api/controllers/view/viewsV2.ts +++ b/packages/server/src/api/controllers/view/viewsV2.ts @@ -9,9 +9,40 @@ import { RequiredKeys, } from "@budibase/types" -export async function create(ctx: Ctx) { - const view = ctx.request.body - const { tableId } = view +async function parseSchemaUI(ctx: Ctx, view: CreateViewRequest) { + if (!view.schema) { + return + } + + function hasOverrides( + newObj: Record, + existingObj: Record + ) { + for (const [key, value] of Object.entries(newObj)) { + if (typeof value === "object") { + if (hasOverrides(value, existingObj[key] || {})) { + return true + } + } else if (value !== existingObj[key]) { + return true + } + } + return false + } + + const table = await sdk.tables.getTable(view.tableId) + for (const [ + fieldName, + { order, width, visible, icon, ...schemaNonUI }, + ] of Object.entries(view.schema)) { + const overrides = hasOverrides(schemaNonUI, table.schema[fieldName]) + if (overrides) { + ctx.throw( + 400, + "This endpoint does not support overriding non UI fields in the schema" + ) + } + } const schemaUI = view.schema && @@ -24,6 +55,14 @@ export async function create(ctx: Ctx) { } return p }, {} as Record>) + return schemaUI +} + +export async function create(ctx: Ctx) { + const view = ctx.request.body + const { tableId } = view + + const schemaUI = await parseSchemaUI(ctx, view) const parsedView: Omit = { name: view.name, diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index 14fe90b93e..e60e9a5126 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -95,15 +95,15 @@ describe("/v2/views", () => { name: generator.name(), tableId: config.table!._id!, schema: { - name: { - name: "name", - type: FieldType.STRING, + Price: { + name: "Price", + type: FieldType.NUMBER, visible: true, order: 1, width: 100, }, - lastname: { - name: "lastname", + Category: { + name: "Category", type: FieldType.STRING, visible: false, icon: "ic", @@ -116,14 +116,14 @@ describe("/v2/views", () => { expect(await config.api.viewV2.get(createdView.id)).toEqual({ ...newView, schema: undefined, - columns: ["name", "lastname"], + columns: ["Price", "Category"], schemaUI: { - name: { + Price: { visible: true, order: 1, width: 100, }, - lastname: { + Category: { visible: false, icon: "ic", }, @@ -132,6 +132,54 @@ describe("/v2/views", () => { version: 2, }) }) + + it("throw an exception if the schema overrides a non UI field", async () => { + const newView: CreateViewRequest = { + name: generator.name(), + tableId: config.table!._id!, + schema: { + Price: { + name: "Price", + type: FieldType.NUMBER, + visible: true, + }, + Category: { + name: "Category", + type: FieldType.STRING, + constraints: { + type: "string", + presence: true, + }, + }, + }, + } + + await config.api.viewV2.create(newView, { + expectStatus: 400, + }) + }) + + it("will not throw an exception if the schema is 'deleting' non UI fields", async () => { + const newView: CreateViewRequest = { + name: generator.name(), + tableId: config.table!._id!, + schema: { + Price: { + name: "Price", + type: FieldType.NUMBER, + visible: true, + }, + Category: { + name: "Category", + type: FieldType.STRING, + }, + }, + } + + await config.api.viewV2.create(newView, { + expectStatus: 201, + }) + }) }) describe("update", () => { From c425194a8561cfb853880f692ab10ae6a38935ac Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 1 Aug 2023 11:38:36 +0200 Subject: [PATCH 08/13] Check schemas on patch --- .../src/api/controllers/view/viewsV2.ts | 14 ++- .../src/api/routes/tests/viewV2.spec.ts | 88 +++++++++++++++++++ .../server/src/tests/utilities/api/viewV2.ts | 10 ++- packages/types/src/api/web/app/view.ts | 5 +- 4 files changed, 113 insertions(+), 4 deletions(-) diff --git a/packages/server/src/api/controllers/view/viewsV2.ts b/packages/server/src/api/controllers/view/viewsV2.ts index f403d14180..804de14ea8 100644 --- a/packages/server/src/api/controllers/view/viewsV2.ts +++ b/packages/server/src/api/controllers/view/viewsV2.ts @@ -92,7 +92,19 @@ export async function update(ctx: Ctx) { const { tableId } = view - const result = await sdk.views.update(tableId, view) + const schemaUI = await parseSchemaUI(ctx, view) + const parsedView: ViewV2 = { + id: view.id, + name: view.name, + version: view.version, + tableId: view.tableId, + query: view.query, + sort: view.sort, + columns: view.schema && Object.keys(view.schema), + schemaUI, + } + + const result = await sdk.views.update(tableId, parsedView) ctx.body = { data: result, } diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index e60e9a5126..b665b261e6 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -298,6 +298,94 @@ describe("/v2/views", () => { status: 400, }) }) + + it("updates only UI schema overrides", async () => { + await config.api.viewV2.update({ + ...view, + schema: { + Price: { + name: "Price", + type: FieldType.NUMBER, + visible: true, + order: 1, + width: 100, + }, + Category: { + name: "Category", + type: FieldType.STRING, + visible: false, + icon: "ic", + }, + }, + }) + + expect(await config.api.viewV2.get(view.id)).toEqual({ + ...view, + schema: undefined, + columns: ["Price", "Category"], + schemaUI: { + Price: { + visible: true, + order: 1, + width: 100, + }, + Category: { + visible: false, + icon: "ic", + }, + }, + id: view.id, + version: 2, + }) + }) + + it("throw an exception if the schema overrides a non UI field", async () => { + await config.api.viewV2.update( + { + ...view, + schema: { + Price: { + name: "Price", + type: FieldType.NUMBER, + visible: true, + }, + Category: { + name: "Category", + type: FieldType.STRING, + constraints: { + type: "string", + presence: true, + }, + }, + }, + }, + { + expectStatus: 400, + } + ) + }) + + it("will not throw an exception if the schema is 'deleting' non UI fields", async () => { + await config.api.viewV2.update( + { + ...view, + schema: { + Price: { + name: "Price", + type: FieldType.NUMBER, + visible: true, + }, + Category: { + name: "Category", + type: FieldType.STRING, + }, + }, + }, + { + expectStatus: 200, + } + ) + }) }) describe("delete", () => { diff --git a/packages/server/src/tests/utilities/api/viewV2.ts b/packages/server/src/tests/utilities/api/viewV2.ts index 1f7fe6e2bb..b404904a28 100644 --- a/packages/server/src/tests/utilities/api/viewV2.ts +++ b/packages/server/src/tests/utilities/api/viewV2.ts @@ -1,4 +1,10 @@ -import { CreateViewRequest, SortOrder, SortType, ViewV2 } from "@budibase/types" +import { + CreateViewRequest, + SortOrder, + SortType, + UpdateViewRequest, + ViewV2, +} from "@budibase/types" import TestConfiguration from "../TestConfiguration" import { TestAPI } from "./base" import { generator } from "@budibase/backend-core/tests" @@ -34,7 +40,7 @@ export class ViewV2API extends TestAPI { } update = async ( - view: ViewV2, + view: UpdateViewRequest, { expectStatus, handleResponse, diff --git a/packages/types/src/api/web/app/view.ts b/packages/types/src/api/web/app/view.ts index b694521678..411bbef44e 100644 --- a/packages/types/src/api/web/app/view.ts +++ b/packages/types/src/api/web/app/view.ts @@ -9,4 +9,7 @@ export interface CreateViewRequest schema?: Record } -export type UpdateViewRequest = ViewV2 +export interface UpdateViewRequest + extends Omit { + schema?: Record +} From 76cacfaff074a487a658484a88df34b2ae362385 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 1 Aug 2023 12:03:43 +0200 Subject: [PATCH 09/13] Clean code --- .../server/src/api/controllers/view/viewsV2.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/server/src/api/controllers/view/viewsV2.ts b/packages/server/src/api/controllers/view/viewsV2.ts index 804de14ea8..cd28a1b55f 100644 --- a/packages/server/src/api/controllers/view/viewsV2.ts +++ b/packages/server/src/api/controllers/view/viewsV2.ts @@ -18,16 +18,18 @@ async function parseSchemaUI(ctx: Ctx, view: CreateViewRequest) { newObj: Record, existingObj: Record ) { - for (const [key, value] of Object.entries(newObj)) { - if (typeof value === "object") { - if (hasOverrides(value, existingObj[key] || {})) { - return true - } - } else if (value !== existingObj[key]) { + const result = Object.entries(newObj).some(([key, value]) => { + const isObject = typeof value === "object" + const existing = existingObj[key] + if (isObject && hasOverrides(value, existing || {})) { return true } - } - return false + if (!isObject && value !== existing) { + return true + } + }) + + return result } const table = await sdk.tables.getTable(view.tableId) From 2cc924f9e717f1aa26969c68459d6fe239546c3e Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 1 Aug 2023 12:08:40 +0200 Subject: [PATCH 10/13] Fix tests --- packages/server/src/api/routes/tests/row.spec.ts | 10 +++++----- .../server/src/api/routes/tests/viewV2.spec.ts | 15 +++++++-------- packages/types/src/api/web/app/view.ts | 6 +++--- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index 479f27d679..b986ffb945 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -1033,7 +1033,7 @@ describe("/rows", () => { } const view = await config.api.viewV2.create({ - columns: ["name"], + schema: { name: {} }, }) const response = await config.api.viewV2.search(view.id) @@ -1102,7 +1102,7 @@ describe("/rows", () => { const table = await config.createTable(userTable()) const view = await config.api.viewV2.create({ tableId: table._id!, - columns: { + schema: { name: { visible: true }, surname: { visible: true }, address: { visible: true }, @@ -1150,7 +1150,7 @@ describe("/rows", () => { const tableId = table._id! const view = await config.api.viewV2.create({ tableId, - columns: { + schema: { name: { visible: true }, address: { visible: true }, }, @@ -1203,7 +1203,7 @@ describe("/rows", () => { const tableId = table._id! const view = await config.api.viewV2.create({ tableId, - columns: { + schema: { name: { visible: true }, address: { visible: true }, }, @@ -1231,7 +1231,7 @@ describe("/rows", () => { const tableId = table._id! const view = await config.api.viewV2.create({ tableId, - columns: { + schema: { name: { visible: true }, address: { visible: true }, }, diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index b665b261e6..452d15bde0 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -1,6 +1,7 @@ import * as setup from "./utilities" import { CreateViewRequest, + FieldSchema, FieldType, SortOrder, SortType, @@ -42,8 +43,6 @@ describe("/v2/views", () => { }, schema: { name: { - name: "name", - type: FieldType.STRING, visible: true, }, }, @@ -108,7 +107,7 @@ describe("/v2/views", () => { visible: false, icon: "ic", }, - }, + } as Record, } const createdView = await config.api.viewV2.create(newView) @@ -151,7 +150,7 @@ describe("/v2/views", () => { presence: true, }, }, - }, + } as Record, } await config.api.viewV2.create(newView, { @@ -173,7 +172,7 @@ describe("/v2/views", () => { name: "Category", type: FieldType.STRING, }, - }, + } as Record, } await config.api.viewV2.create(newView, { @@ -316,7 +315,7 @@ describe("/v2/views", () => { visible: false, icon: "ic", }, - }, + } as Record, }) expect(await config.api.viewV2.get(view.id)).toEqual({ @@ -357,7 +356,7 @@ describe("/v2/views", () => { presence: true, }, }, - }, + } as Record, }, { expectStatus: 400, @@ -379,7 +378,7 @@ describe("/v2/views", () => { name: "Category", type: FieldType.STRING, }, - }, + } as Record, }, { expectStatus: 200, diff --git a/packages/types/src/api/web/app/view.ts b/packages/types/src/api/web/app/view.ts index 411bbef44e..60b5a01b15 100644 --- a/packages/types/src/api/web/app/view.ts +++ b/packages/types/src/api/web/app/view.ts @@ -1,4 +1,4 @@ -import { ViewV2, FieldSchema } from "../../../documents" +import { ViewV2, UIFieldMetadata } from "../../../documents" export interface ViewResponse { data: ViewV2 @@ -6,10 +6,10 @@ export interface ViewResponse { export interface CreateViewRequest extends Omit { - schema?: Record + schema?: Record } export interface UpdateViewRequest extends Omit { - schema?: Record + schema?: Record } From 44e028b574431d3f128a1563d1386c9d520cbf78 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 1 Aug 2023 12:26:37 +0200 Subject: [PATCH 11/13] Fix test --- packages/server/src/middleware/tests/trimViewRowInfo.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/server/src/middleware/tests/trimViewRowInfo.spec.ts b/packages/server/src/middleware/tests/trimViewRowInfo.spec.ts index 89b831b647..427ac9a608 100644 --- a/packages/server/src/middleware/tests/trimViewRowInfo.spec.ts +++ b/packages/server/src/middleware/tests/trimViewRowInfo.spec.ts @@ -129,7 +129,7 @@ describe("trimViewRowInfo middleware", () => { id: viewId, name: generator.guid(), tableId: table._id!, - columns: { name: { visible: true }, address: { visible: true } }, + columns: ["name", "address"], }) const data = getRandomData() From 67baa56cbf5d4b8344a5dc29468021ef648a2c3f Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 1 Aug 2023 14:40:47 +0200 Subject: [PATCH 12/13] Skip flaky test --- packages/server/src/sdk/app/rows/search/tests/external.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/server/src/sdk/app/rows/search/tests/external.spec.ts b/packages/server/src/sdk/app/rows/search/tests/external.spec.ts index e2b71ac81e..b24557fd4f 100644 --- a/packages/server/src/sdk/app/rows/search/tests/external.spec.ts +++ b/packages/server/src/sdk/app/rows/search/tests/external.spec.ts @@ -13,7 +13,7 @@ jest.unmock("mysql2/promise") jest.setTimeout(30000) -describe("external", () => { +describe.skip("external", () => { const config = new TestConfiguration() let externalDatasource: Datasource From f8cbf3c29895896a58f1dcf15906678b953d6f0b Mon Sep 17 00:00:00 2001 From: Budibase Staging Release Bot <> Date: Tue, 1 Aug 2023 12:57:56 +0000 Subject: [PATCH 13/13] Bump version to 2.8.29-alpha.9 --- lerna.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lerna.json b/lerna.json index 2295b6a975..2b5b60e7a9 100644 --- a/lerna.json +++ b/lerna.json @@ -1,5 +1,5 @@ { - "version": "2.8.29-alpha.8", + "version": "2.8.29-alpha.9", "npmClient": "yarn", "packages": [ "packages/*"