From 3d2954e1d508fb625f4711c429c6880a8705857a Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 2 Aug 2023 13:09:37 +0200 Subject: [PATCH 1/4] Add primary display --- packages/types/src/documents/app/view.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/types/src/documents/app/view.ts b/packages/types/src/documents/app/view.ts index 10fcac2805..3fe8b4a500 100644 --- a/packages/types/src/documents/app/view.ts +++ b/packages/types/src/documents/app/view.ts @@ -18,6 +18,7 @@ export interface ViewV2 { version: 2 id: string name: string + primaryDisplay?: string tableId: string query?: SearchFilters sort?: { From 398cf99b4ff0feeccac421f87b5c5fba311bcb37 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 2 Aug 2023 13:17:20 +0200 Subject: [PATCH 2/4] Handle primaryDisplay on controller --- packages/server/src/api/controllers/view/viewsV2.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/server/src/api/controllers/view/viewsV2.ts b/packages/server/src/api/controllers/view/viewsV2.ts index cd28a1b55f..82e536f62d 100644 --- a/packages/server/src/api/controllers/view/viewsV2.ts +++ b/packages/server/src/api/controllers/view/viewsV2.ts @@ -66,13 +66,14 @@ export async function create(ctx: Ctx) { const schemaUI = await parseSchemaUI(ctx, view) - const parsedView: Omit = { + const parsedView: Omit, "id" | "version"> = { name: view.name, tableId: view.tableId, query: view.query, sort: view.sort, columns: view.schema && Object.keys(view.schema), schemaUI, + primaryDisplay: view.primaryDisplay, } const result = await sdk.views.create(tableId, parsedView) ctx.status = 201 @@ -95,7 +96,7 @@ export async function update(ctx: Ctx) { const { tableId } = view const schemaUI = await parseSchemaUI(ctx, view) - const parsedView: ViewV2 = { + const parsedView: RequiredKeys = { id: view.id, name: view.name, version: view.version, @@ -104,6 +105,7 @@ export async function update(ctx: Ctx) { sort: view.sort, columns: view.schema && Object.keys(view.schema), schemaUI, + primaryDisplay: view.primaryDisplay, } const result = await sdk.views.update(tableId, parsedView) From a3a1e29350edc801e0989557cc258d45dc15e120 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 2 Aug 2023 13:37:36 +0200 Subject: [PATCH 3/4] Handle undefineds --- packages/server/src/api/controllers/view/viewsV2.ts | 8 +++++++- packages/server/src/sdk/app/views/index.ts | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/server/src/api/controllers/view/viewsV2.ts b/packages/server/src/api/controllers/view/viewsV2.ts index 82e536f62d..80a115e365 100644 --- a/packages/server/src/api/controllers/view/viewsV2.ts +++ b/packages/server/src/api/controllers/view/viewsV2.ts @@ -49,12 +49,18 @@ async function parseSchemaUI(ctx: Ctx, view: CreateViewRequest) { const schemaUI = view.schema && Object.entries(view.schema).reduce((p, [fieldName, schemaValue]) => { - p[fieldName] = { + const fieldSchema: RequiredKeys = { order: schemaValue.order, width: schemaValue.width, visible: schemaValue.visible, icon: schemaValue.icon, } + Object.entries(fieldSchema) + .filter(([_, val]) => val === undefined) + .forEach(([key]) => { + delete fieldSchema[key as keyof UIFieldMetadata] + }) + p[fieldName] = fieldSchema return p }, {} as Record>) return schemaUI diff --git a/packages/server/src/sdk/app/views/index.ts b/packages/server/src/sdk/app/views/index.ts index 3dfa82df0d..7e75f22060 100644 --- a/packages/server/src/sdk/app/views/index.ts +++ b/packages/server/src/sdk/app/views/index.ts @@ -86,6 +86,7 @@ export function enrichSchema(view: View | ViewV2, tableSchema: TableSchema) { : schema[fieldName].order, } } + delete view.schemaUI } if (view?.columns?.length) { @@ -97,6 +98,7 @@ export function enrichSchema(view: View | ViewV2, tableSchema: TableSchema) { pickedSchema[fieldName] = { ...schema[fieldName] } } schema = pickedSchema + delete view.columns } return { From e11279311dbc75424492c695d10661260417fc18 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 2 Aug 2023 13:37:58 +0200 Subject: [PATCH 4/4] Add tests --- .../src/api/routes/tests/viewV2.spec.ts | 79 ++++++++++++++----- 1 file changed, 58 insertions(+), 21 deletions(-) diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index 452d15bde0..fc97e657c5 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -6,6 +6,7 @@ import { SortOrder, SortType, Table, + UpdateViewRequest, ViewV2, } from "@budibase/types" import { generator } from "@budibase/backend-core/tests" @@ -34,20 +35,6 @@ function priceTable(): Table { describe("/v2/views", () => { const config = setup.getConfig() - const viewFilters: Omit = { - query: { allOr: false, equal: { field: "value" } }, - sort: { - field: "fieldToSort", - order: SortOrder.DESCENDING, - type: SortType.STRING, - }, - schema: { - name: { - visible: true, - }, - }, - } - afterAll(setup.afterAll) beforeAll(async () => { @@ -70,20 +57,30 @@ describe("/v2/views", () => { }) }) - it("can persist views with queries", async () => { - const newView: CreateViewRequest = { + it("can persist views with all fields", async () => { + const newView: Required = { name: generator.name(), tableId: config.table!._id!, - query: viewFilters.query, - sort: viewFilters.sort, + primaryDisplay: generator.word(), + query: { allOr: false, equal: { field: "value" } }, + sort: { + field: "fieldToSort", + order: SortOrder.DESCENDING, + type: SortType.STRING, + }, + schema: { + name: { + visible: true, + }, + }, } - delete newView.schema const res = await config.api.viewV2.create(newView) expect(res).toEqual({ ...newView, - query: viewFilters.query, - sort: viewFilters.sort, + schema: undefined, + columns: ["name"], + schemaUI: newView.schema, id: expect.any(String), version: 2, }) @@ -210,6 +207,46 @@ describe("/v2/views", () => { }) }) + it("can update all fields", async () => { + const tableId = config.table!._id! + + const updatedData: Required = { + version: view.version, + id: view.id, + tableId, + name: view.name, + primaryDisplay: generator.word(), + query: { equal: { [generator.word()]: generator.word() } }, + sort: { + field: generator.word(), + order: SortOrder.DESCENDING, + type: SortType.STRING, + }, + schema: { + Category: { + visible: false, + }, + }, + } + await config.api.viewV2.update(updatedData) + + expect(await config.api.table.get(tableId)).toEqual({ + ...config.table, + views: { + [view.name]: { + ...updatedData, + schema: { + Category: expect.objectContaining({ + visible: false, + }), + }, + }, + }, + _rev: expect.any(String), + updatedAt: expect.any(String), + }) + }) + it("can update an existing view name", async () => { const tableId = config.table!._id! await config.api.viewV2.update({ ...view, name: "View B" })