From da6ac6c1cbc1d3f4923050cc6c1c19d334ae2cbe Mon Sep 17 00:00:00 2001 From: Daniel Olabemiwo Date: Fri, 23 Aug 2024 02:38:16 +0100 Subject: [PATCH 01/21] fix #14408 - Delete Screen modal shows behind builder --- packages/bbui/src/Modal/Modal.svelte | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/bbui/src/Modal/Modal.svelte b/packages/bbui/src/Modal/Modal.svelte index dec1455d0c..4d55a8a266 100644 --- a/packages/bbui/src/Modal/Modal.svelte +++ b/packages/bbui/src/Modal/Modal.svelte @@ -10,7 +10,7 @@ export let inline = false export let disableCancel = false export let autoFocus = true - export let zIndex = 999 + export let zIndex = 99999 const dispatch = createEventDispatcher() let visible = fixed || inline From 458ef9e75461d0009d84e58f926c424a9ef6283c Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 28 Aug 2024 13:16:22 +0200 Subject: [PATCH 02/21] Trim on output --- .../server/src/middleware/trimViewRowInfo.ts | 33 ++++++++++++------- packages/server/src/sdk/app/views/index.ts | 10 ++++-- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/packages/server/src/middleware/trimViewRowInfo.ts b/packages/server/src/middleware/trimViewRowInfo.ts index 55efaee732..2d29a88a6f 100644 --- a/packages/server/src/middleware/trimViewRowInfo.ts +++ b/packages/server/src/middleware/trimViewRowInfo.ts @@ -1,10 +1,10 @@ -import { Ctx, Row } from "@budibase/types" +import { Ctx, Row, ViewV2 } from "@budibase/types" import sdk from "../sdk" import { Next } from "koa" import { getSourceId } from "../api/controllers/row/utils" -export default async (ctx: Ctx, next: Next) => { +export default async (ctx: Ctx, next: Next) => { const { body } = ctx.request const viewId = getSourceId(ctx).viewId ?? body._viewId @@ -14,22 +14,31 @@ export default async (ctx: Ctx, next: Next) => { } // don't need to trim delete requests - if (ctx?.method?.toLowerCase() !== "delete") { - await trimViewFields(ctx.request.body, viewId) + const trimFields = ctx?.method?.toLowerCase() !== "delete" + if (!trimFields) { + return next() } - return next() + const view = await sdk.views.get(viewId) + ctx.request.body = await trimNonViewFields(ctx.request.body, view, "WRITE") + + await next() + + ctx.body = await trimNonViewFields(ctx.body, view, "READ") } // have to mutate the koa context, can't return -export async function trimViewFields(body: Row, viewId: string): Promise { - const view = await sdk.views.get(viewId) - const allowedKeys = sdk.views.allowedFields(view) +export async function trimNonViewFields( + row: Row, + view: ViewV2, + permission: "WRITE" | "READ" +): Promise { + row = { ...row } + const allowedKeys = sdk.views.allowedFields(view, permission) // have to mutate the context, can't update reference - const toBeRemoved = Object.keys(body).filter( - key => !allowedKeys.includes(key) - ) + const toBeRemoved = Object.keys(row).filter(key => !allowedKeys.includes(key)) for (let removeKey of toBeRemoved) { - delete body[removeKey] + delete row[removeKey] } + return row } diff --git a/packages/server/src/sdk/app/views/index.ts b/packages/server/src/sdk/app/views/index.ts index 1c09f710d7..0fc692b108 100644 --- a/packages/server/src/sdk/app/views/index.ts +++ b/packages/server/src/sdk/app/views/index.ts @@ -139,14 +139,20 @@ export async function remove(viewId: string): Promise { return pickApi(tableId).remove(viewId) } -export function allowedFields(view: View | ViewV2) { +export function allowedFields( + view: View | ViewV2, + permission: "WRITE" | "READ" +) { return [ ...Object.keys(view?.schema || {}).filter(key => { if (!isV2(view)) { return true } const fieldSchema = view.schema![key] - return fieldSchema.visible && !fieldSchema.readonly + if (permission === "WRITE") { + return fieldSchema.visible && !fieldSchema.readonly + } + return fieldSchema.visible }), ...PROTECTED_EXTERNAL_COLUMNS, ...PROTECTED_INTERNAL_COLUMNS, From 9b674816611850e114b4b607374c27462b4135fb Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 28 Aug 2024 13:16:42 +0200 Subject: [PATCH 03/21] Add extra tests --- .../src/api/routes/tests/viewV2.spec.ts | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index 4401efc480..476d2f4d60 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -1084,9 +1084,36 @@ describe.each([ expect(row.one).toBeUndefined() expect(row.two).toEqual("bar") }) + + it("should not return non-view view fields for a row", async () => { + const newRow = await config.api.row.save(view.id, { + one: "foo", + two: "bar", + }) + + expect(newRow.one).toBeUndefined() + expect(newRow.two).toEqual("bar") + }) }) describe("patch", () => { + it("should not return non-view view fields for a row", async () => { + const newRow = await config.api.row.save(table._id!, { + one: "foo", + two: "bar", + }) + const row = await config.api.row.patch(view.id, { + tableId: table._id!, + _id: newRow._id!, + _rev: newRow._rev!, + one: "newFoo", + two: "newBar", + }) + + expect(row.one).toBeUndefined() + expect(row.two).toEqual("newBar") + }) + it("should update only the view fields for a row", async () => { const newRow = await config.api.row.save(table._id!, { one: "foo", From 8a9e86852756594285f418bbc5c16ed3864fb78a Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 28 Aug 2024 13:26:23 +0200 Subject: [PATCH 04/21] Move test to right describe --- .../src/api/routes/tests/viewV2.spec.ts | 87 ++++++++++--------- 1 file changed, 44 insertions(+), 43 deletions(-) diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index 476d2f4d60..d6546f8e03 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -978,49 +978,6 @@ describe.each([ }) }) - describe("read", () => { - let view: ViewV2 - - beforeAll(async () => { - table = await config.api.table.save( - saveTableRequest({ - schema: { - Country: { - type: FieldType.STRING, - name: "Country", - }, - Story: { - type: FieldType.STRING, - name: "Story", - }, - }, - }) - ) - - view = await config.api.viewV2.create({ - tableId: table._id!, - name: generator.guid(), - schema: { - id: { visible: true }, - Country: { - visible: true, - }, - }, - }) - }) - - it("views have extra data trimmed", async () => { - let row = await config.api.row.save(view.id, { - Country: "Aussy", - Story: "aaaaa", - }) - - row = await config.api.row.get(table._id!, row._id!) - expect(row.Story).toBeUndefined() - expect(row.Country).toEqual("Aussy") - }) - }) - describe("row operations", () => { let table: Table, view: ViewV2 beforeEach(async () => { @@ -1194,6 +1151,50 @@ describe.each([ }) }) + describe("read", () => { + let view: ViewV2 + let table: Table + + beforeAll(async () => { + table = await config.api.table.save( + saveTableRequest({ + schema: { + Country: { + type: FieldType.STRING, + name: "Country", + }, + Story: { + type: FieldType.STRING, + name: "Story", + }, + }, + }) + ) + + view = await config.api.viewV2.create({ + tableId: table._id!, + name: generator.guid(), + schema: { + id: { visible: true }, + Country: { + visible: true, + }, + }, + }) + }) + + it("views have extra data trimmed", async () => { + let row = await config.api.row.save(view.id, { + Country: "Aussy", + Story: "aaaaa", + }) + + row = await config.api.row.get(table._id!, row._id!) + expect(row.Story).toBeUndefined() + expect(row.Country).toEqual("Aussy") + }) + }) + describe("search", () => { it("returns empty rows from view when no schema is passed", async () => { const rows = await Promise.all( From 3af35e6683c820c1a54b1f107a3fc9195293a88a Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 28 Aug 2024 13:28:45 +0200 Subject: [PATCH 05/21] Add describe --- .../src/api/routes/tests/viewV2.spec.ts | 274 +++++++++--------- 1 file changed, 141 insertions(+), 133 deletions(-) diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index d6546f8e03..c238d85da2 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -125,18 +125,7 @@ describe.each([ mocks.licenses.useCloudFree() }) - const getRowUsage = async () => { - const { total } = await config.doInContext(undefined, () => - quotas.getCurrentUsageValues(QuotaUsageType.STATIC, StaticQuotaName.ROWS) - ) - return total - } - - const assertRowUsage = async (expected: number) => { - const usage = await getRowUsage() - expect(usage).toBe(expected) - } - + describe("view crud", () => { describe("create", () => { it("persist the view when the view is successfully created", async () => { const newView: CreateViewRequest = { @@ -584,7 +573,9 @@ describe.each([ expect((await config.api.table.get(tableId)).views).toEqual({ [view.name]: { ...view, - query: [{ operator: "equal", field: "newField", value: "thatValue" }], + query: [ + { operator: "equal", field: "newField", value: "thatValue" }, + ], schema: expect.anything(), }, }) @@ -886,7 +877,8 @@ describe.each([ // Update the view to an invalid state const tableToUpdate = await config.api.table.get(table._id!) - ;(tableToUpdate.views![view.name] as ViewV2).schema!.id.visible = false + ;(tableToUpdate.views![view.name] as ViewV2).schema!.id.visible = + false await db.getDB(config.appId!).put(tableToUpdate) view = await config.api.viewV2.get(view.id) @@ -975,6 +967,126 @@ describe.each([ expect(view.schema?.Price).toEqual( expect.objectContaining({ visible: true, readonly: true }) ) + }) + }) + + describe("updating table schema", () => { + describe("existing columns changed to required", () => { + beforeEach(async () => { + table = await config.api.table.save( + saveTableRequest({ + schema: { + id: { + name: "id", + type: FieldType.NUMBER, + autocolumn: true, + }, + name: { + name: "name", + type: FieldType.STRING, + }, + }, + }) + ) + }) + + it("allows updating when no views constrains the field", async () => { + await config.api.viewV2.create({ + name: "view a", + tableId: table._id!, + schema: { + id: { visible: true }, + name: { visible: true }, + }, + }) + + table = await config.api.table.get(table._id!) + await config.api.table.save( + { + ...table, + schema: { + ...table.schema, + name: { + name: "name", + type: FieldType.STRING, + constraints: { presence: { allowEmpty: false } }, + }, + }, + }, + { status: 200 } + ) + }) + + it("rejects if field is readonly in any view", async () => { + mocks.licenses.useViewReadonlyColumns() + + await config.api.viewV2.create({ + name: "view a", + tableId: table._id!, + schema: { + id: { visible: true }, + name: { + visible: true, + readonly: true, + }, + }, + }) + + table = await config.api.table.get(table._id!) + await config.api.table.save( + { + ...table, + schema: { + ...table.schema, + name: { + name: "name", + type: FieldType.STRING, + constraints: { presence: true }, + }, + }, + }, + { + status: 400, + body: { + status: 400, + message: + 'To make field "name" required, this field must be present and writable in views: view a.', + }, + } + ) + }) + + it("rejects if field is hidden in any view", async () => { + await config.api.viewV2.create({ + name: "view a", + tableId: table._id!, + schema: { id: { visible: true } }, + }) + + table = await config.api.table.get(table._id!) + await config.api.table.save( + { + ...table, + schema: { + ...table.schema, + name: { + name: "name", + type: FieldType.STRING, + constraints: { presence: true }, + }, + }, + }, + { + status: 400, + body: { + status: 400, + message: + 'To make field "name" required, this field must be present and writable in views: view a.', + }, + } + ) + }) + }) }) }) @@ -1119,6 +1231,21 @@ describe.each([ }) describe("destroy", () => { + const getRowUsage = async () => { + const { total } = await config.doInContext(undefined, () => + quotas.getCurrentUsageValues( + QuotaUsageType.STATIC, + StaticQuotaName.ROWS + ) + ) + return total + } + + const assertRowUsage = async (expected: number) => { + const usage = await getRowUsage() + expect(usage).toBe(expected) + } + it("should be able to delete a row", async () => { const createdRow = await config.api.row.save(table._id!, {}) const rowUsage = await getRowUsage() @@ -1823,123 +1950,4 @@ describe.each([ }) }) }) - - describe("updating table schema", () => { - describe("existing columns changed to required", () => { - beforeEach(async () => { - table = await config.api.table.save( - saveTableRequest({ - schema: { - id: { - name: "id", - type: FieldType.NUMBER, - autocolumn: true, - }, - name: { - name: "name", - type: FieldType.STRING, - }, - }, - }) - ) - }) - - it("allows updating when no views constrains the field", async () => { - await config.api.viewV2.create({ - name: "view a", - tableId: table._id!, - schema: { - id: { visible: true }, - name: { visible: true }, - }, - }) - - table = await config.api.table.get(table._id!) - await config.api.table.save( - { - ...table, - schema: { - ...table.schema, - name: { - name: "name", - type: FieldType.STRING, - constraints: { presence: { allowEmpty: false } }, - }, - }, - }, - { status: 200 } - ) - }) - - it("rejects if field is readonly in any view", async () => { - mocks.licenses.useViewReadonlyColumns() - - await config.api.viewV2.create({ - name: "view a", - tableId: table._id!, - schema: { - id: { visible: true }, - name: { - visible: true, - readonly: true, - }, - }, - }) - - table = await config.api.table.get(table._id!) - await config.api.table.save( - { - ...table, - schema: { - ...table.schema, - name: { - name: "name", - type: FieldType.STRING, - constraints: { presence: true }, - }, - }, - }, - { - status: 400, - body: { - status: 400, - message: - 'To make field "name" required, this field must be present and writable in views: view a.', - }, - } - ) - }) - - it("rejects if field is hidden in any view", async () => { - await config.api.viewV2.create({ - name: "view a", - tableId: table._id!, - schema: { id: { visible: true } }, - }) - - table = await config.api.table.get(table._id!) - await config.api.table.save( - { - ...table, - schema: { - ...table.schema, - name: { - name: "name", - type: FieldType.STRING, - constraints: { presence: true }, - }, - }, - }, - { - status: 400, - body: { - status: 400, - message: - 'To make field "name" required, this field must be present and writable in views: view a.', - }, - } - ) - }) - }) - }) }) From 4bd27bac615f43e06536da71edae647815e548e3 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 28 Aug 2024 13:36:40 +0200 Subject: [PATCH 06/21] Add basic test --- .../src/api/routes/tests/viewV2.spec.ts | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index c238d85da2..84dfc39d43 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -929,6 +929,57 @@ describe.each([ }) }) + describe("read", () => { + let table: Table + let tableId: string + + beforeEach(async () => { + table = await config.api.table.save( + saveTableRequest({ + schema: { + one: { + type: FieldType.STRING, + name: "one", + }, + two: { + type: FieldType.STRING, + name: "two", + }, + three: { + type: FieldType.STRING, + name: "three", + }, + }, + }) + ) + tableId = table._id! + }) + + it("retrieves the view data with the enriched schema", async () => { + const view = await config.api.viewV2.create({ + tableId, + name: generator.guid(), + schema: { + id: { visible: true }, + one: { visible: true }, + two: { visible: true }, + }, + }) + + expect((await config.api.table.get(tableId)).views).toEqual({ + [view.name]: { + ...view, + schema: { + id: { ...table.schema["id"], visible: true }, + one: { ...table.schema["one"], visible: true }, + two: { ...table.schema["two"], visible: true }, + three: { ...table.schema["three"], visible: false }, + }, + }, + }) + }) + }) + describe("fetch view (through table)", () => { it("should be able to fetch a view V2", async () => { const res = await config.api.viewV2.create({ From d404d60c32942e273bbb41026b21055596f831e5 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 28 Aug 2024 14:07:59 +0200 Subject: [PATCH 07/21] Add extra tests --- .../src/api/routes/tests/viewV2.spec.ts | 37 +++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index 84dfc39d43..a8c8d77558 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -929,7 +929,16 @@ describe.each([ }) }) - describe("read", () => { + describe.each([ + ["from view api", (view: ViewV2) => config.api.viewV2.get(view.id)], + [ + "from table", + async (view: ViewV2) => { + const table = await config.api.table.get(view.tableId) + return (table.views || {})[view.name] + }, + ], + ])("read (%s)", (_, getDelegate) => { let table: Table let tableId: string @@ -966,8 +975,7 @@ describe.each([ }, }) - expect((await config.api.table.get(tableId)).views).toEqual({ - [view.name]: { + expect(await getDelegate(view)).toEqual({ ...view, schema: { id: { ...table.schema["id"], visible: true }, @@ -975,6 +983,29 @@ describe.each([ two: { ...table.schema["two"], visible: true }, three: { ...table.schema["three"], visible: false }, }, + }) + }) + + it("does not include columns removed from the table", async () => { + const view = await config.api.viewV2.create({ + tableId, + name: generator.guid(), + schema: { + id: { visible: true }, + one: { visible: true }, + two: { visible: true }, + }, + }) + const table = await config.api.table.get(tableId) + const { one: _, ...newSchema } = table.schema + await config.api.table.save({ ...table, schema: newSchema }) + + expect(await getDelegate(view)).toEqual({ + ...view, + schema: { + id: { ...table.schema["id"], visible: true }, + two: { ...table.schema["two"], visible: true }, + three: { ...table.schema["three"], visible: false }, }, }) }) From 0dba593bdb8fe97fb961c632afe1eec7b8d10066 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 28 Aug 2024 14:11:02 +0200 Subject: [PATCH 08/21] Add extra test (failing) --- .../src/api/routes/tests/viewV2.spec.ts | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index a8c8d77558..cc21dd1b7e 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -1009,6 +1009,35 @@ describe.each([ }, }) }) + + it("does not include columns hidden from the table", async () => { + const view = await config.api.viewV2.create({ + tableId, + name: generator.guid(), + schema: { + id: { visible: true }, + one: { visible: true }, + two: { visible: true }, + }, + }) + const table = await config.api.table.get(tableId) + await config.api.table.save({ + ...table, + schema: { + ...table.schema, + two: { ...table.schema["two"], visible: false }, + }, + }) + + expect(await getDelegate(view)).toEqual({ + ...view, + schema: { + id: { ...table.schema["id"], visible: true }, + one: { ...table.schema["one"], visible: true }, + three: { ...table.schema["three"], visible: false }, + }, + }) + }) }) describe("fetch view (through table)", () => { From e1add8dd6aaaf96a5c6206509247e2fe2ce00579 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 28 Aug 2024 14:20:39 +0200 Subject: [PATCH 09/21] Fix retrieve --- .../src/api/routes/tests/viewV2.spec.ts | 1556 ++++++++--------- packages/server/src/sdk/app/views/index.ts | 11 +- 2 files changed, 784 insertions(+), 783 deletions(-) diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index cc21dd1b7e..8cde673a07 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -126,659 +126,121 @@ describe.each([ }) describe("view crud", () => { - describe("create", () => { - it("persist the view when the view is successfully created", async () => { - const newView: CreateViewRequest = { - name: generator.name(), - tableId: table._id!, - schema: { - id: { visible: true }, - }, - } - const res = await config.api.viewV2.create(newView) - - expect(res).toEqual({ - ...newView, - id: expect.stringMatching(new RegExp(`${table._id!}_`)), - version: 2, - }) - }) - - it("can persist views with all fields", async () => { - const newView: Required = { - name: generator.name(), - tableId: table._id!, - primaryDisplay: "id", - query: [ - { - operator: BasicOperator.EQUAL, - field: "field", - value: "value", - }, - ], - sort: { - field: "fieldToSort", - order: SortOrder.DESCENDING, - type: SortType.STRING, - }, - schema: { - id: { visible: true }, - Price: { - visible: true, - }, - }, - } - const res = await config.api.viewV2.create(newView) - - expect(res).toEqual({ - ...newView, - schema: { - id: { visible: true }, - Price: { - visible: true, - }, - }, - id: expect.any(String), - version: 2, - }) - }) - - it("persist only UI schema overrides", async () => { - const newView: CreateViewRequest = { - name: generator.name(), - tableId: table._id!, - schema: { - id: { - name: "id", - type: FieldType.NUMBER, - visible: true, - }, - Price: { - name: "Price", - type: FieldType.NUMBER, - visible: true, - order: 1, - width: 100, - }, - Category: { - name: "Category", - type: FieldType.STRING, - visible: false, - icon: "ic", - }, - } as Record, - } - - const createdView = await config.api.viewV2.create(newView) - - expect(createdView).toEqual({ - ...newView, - schema: { - id: { visible: true }, - Price: { - visible: true, - order: 1, - width: 100, - }, - Category: { - visible: false, - icon: "ic", - }, - }, - id: createdView.id, - version: 2, - }) - }) - - it("will not throw an exception if the schema is 'deleting' non UI fields", async () => { - const newView: CreateViewRequest = { - name: generator.name(), - tableId: table._id!, - schema: { - id: { - name: "id", - type: FieldType.NUMBER, - autocolumn: true, - visible: true, - }, - Price: { - name: "Price", - type: FieldType.NUMBER, - visible: true, - }, - Category: { - name: "Category", - type: FieldType.STRING, - }, - } as Record, - } - - await config.api.viewV2.create(newView, { - status: 201, - }) - }) - - it("does not persist non-visible fields", async () => { - const newView: CreateViewRequest = { - name: generator.name(), - tableId: table._id!, - primaryDisplay: "id", - schema: { - id: { visible: true }, - Price: { visible: true }, - Category: { visible: false }, - }, - } - const res = await config.api.viewV2.create(newView) - - expect(res).toEqual({ - ...newView, - schema: { - id: { visible: true }, - Price: { visible: true }, - Category: { visible: false }, - }, - id: expect.any(String), - version: 2, - }) - }) - - it("throws bad request when the schema fields are not valid", async () => { - const newView: CreateViewRequest = { - name: generator.name(), - tableId: table._id!, - schema: { - id: { visible: true }, - nonExisting: { - visible: true, - }, - }, - } - await config.api.viewV2.create(newView, { - status: 400, - body: { - message: 'Field "nonExisting" is not valid for the requested table', - }, - }) - }) - - describe("readonly fields", () => { - beforeEach(() => { - mocks.licenses.useViewReadonlyColumns() - }) - - it("readonly fields are persisted", async () => { - const table = await config.api.table.save( - saveTableRequest({ - schema: { - name: { - name: "name", - type: FieldType.STRING, - }, - description: { - name: "description", - type: FieldType.STRING, - }, - }, - }) - ) - + describe("create", () => { + it("persist the view when the view is successfully created", async () => { const newView: CreateViewRequest = { name: generator.name(), tableId: table._id!, schema: { id: { visible: true }, - name: { - visible: true, - readonly: true, - }, - description: { - visible: true, - readonly: true, - }, }, } - const res = await config.api.viewV2.create(newView) - expect(res.schema).toEqual({ - id: { visible: true }, - name: { - visible: true, - readonly: true, - }, - description: { - visible: true, - readonly: true, - }, + + expect(res).toEqual({ + ...newView, + id: expect.stringMatching(new RegExp(`${table._id!}_`)), + version: 2, }) }) - it("required fields cannot be marked as readonly", async () => { - const table = await config.api.table.save( - saveTableRequest({ - schema: { - name: { - name: "name", - type: FieldType.STRING, - constraints: { presence: true }, - }, - description: { - name: "description", - type: FieldType.STRING, - }, - }, - }) - ) - - const newView: CreateViewRequest = { + it("can persist views with all fields", async () => { + const newView: Required = { name: generator.name(), tableId: table._id!, - schema: { - id: { visible: true }, - name: { - visible: true, - readonly: true, - }, - }, - } - - await config.api.viewV2.create(newView, { - status: 400, - body: { - message: - 'You can\'t make "name" readonly because it is a required field.', - status: 400, - }, - }) - }) - - it("readonly fields must be visible", async () => { - const table = await config.api.table.save( - saveTableRequest({ - schema: { - name: { - name: "name", - type: FieldType.STRING, - }, - description: { - name: "description", - type: FieldType.STRING, - }, - }, - }) - ) - - const newView: CreateViewRequest = { - name: generator.name(), - tableId: table._id!, - schema: { - id: { visible: true }, - name: { - visible: false, - readonly: true, - }, - }, - } - - await config.api.viewV2.create(newView, { - status: 400, - body: { - message: - 'Field "name" must be visible if you want to make it readonly', - status: 400, - }, - }) - }) - - it("readonly fields cannot be used on free license", async () => { - mocks.licenses.useCloudFree() - const table = await config.api.table.save( - saveTableRequest({ - schema: { - name: { - name: "name", - type: FieldType.STRING, - }, - description: { - name: "description", - type: FieldType.STRING, - }, - }, - }) - ) - - const newView: CreateViewRequest = { - name: generator.name(), - tableId: table._id!, - schema: { - id: { visible: true }, - name: { - visible: true, - readonly: true, - }, - }, - } - - await config.api.viewV2.create(newView, { - status: 400, - body: { - message: "Readonly fields are not enabled", - status: 400, - }, - }) - }) - }) - - it("display fields must be visible", async () => { - const table = await config.api.table.save( - saveTableRequest({ - schema: { - name: { - name: "name", - type: FieldType.STRING, - }, - description: { - name: "description", - type: FieldType.STRING, - }, - }, - }) - ) - - const newView: CreateViewRequest = { - name: generator.name(), - tableId: table._id!, - primaryDisplay: "name", - schema: { - id: { visible: true }, - name: { - visible: false, - }, - }, - } - - await config.api.viewV2.create(newView, { - status: 400, - body: { - message: 'You can\'t hide "name" because it is the display column.', - status: 400, - }, - }) - }) - - it("display fields can be readonly", async () => { - mocks.licenses.useViewReadonlyColumns() - const table = await config.api.table.save( - saveTableRequest({ - schema: { - name: { - name: "name", - type: FieldType.STRING, - }, - description: { - name: "description", - type: FieldType.STRING, - }, - }, - }) - ) - - const newView: CreateViewRequest = { - name: generator.name(), - tableId: table._id!, - primaryDisplay: "name", - schema: { - id: { visible: true }, - name: { - visible: true, - readonly: true, - }, - }, - } - - await config.api.viewV2.create(newView, { - status: 201, - }) - }) - }) - - describe("update", () => { - let view: ViewV2 - - beforeEach(async () => { - table = await config.api.table.save(priceTable()) - - view = await config.api.viewV2.create({ - tableId: table._id!, - name: generator.guid(), - schema: { - id: { visible: true }, - }, - }) - }) - - it("can update an existing view data", async () => { - const tableId = table._id! - await config.api.viewV2.update({ - ...view, - query: [ - { - operator: BasicOperator.EQUAL, - field: "newField", - value: "thatValue", - }, - ], - }) - - expect((await config.api.table.get(tableId)).views).toEqual({ - [view.name]: { - ...view, - query: [ - { operator: "equal", field: "newField", value: "thatValue" }, - ], - schema: expect.anything(), - }, - }) - }) - - it("can update all fields", async () => { - mocks.licenses.useViewReadonlyColumns() - const tableId = table._id! - - const updatedData: Required = { - version: view.version, - id: view.id, - tableId, - name: view.name, - primaryDisplay: "Price", - query: [ - { - operator: BasicOperator.EQUAL, - field: generator.word(), - value: generator.word(), - }, - ], - sort: { - field: generator.word(), - order: SortOrder.DESCENDING, - type: SortType.STRING, - }, - schema: { - id: { visible: true }, - Category: { - visible: false, - }, - Price: { - visible: true, - readonly: true, - }, - }, - } - await config.api.viewV2.update(updatedData) - - expect((await config.api.table.get(tableId)).views).toEqual({ - [view.name]: { - ...updatedData, - schema: { - ...table.schema, - id: expect.objectContaining({ - visible: true, - }), - Category: expect.objectContaining({ - visible: false, - }), - Price: expect.objectContaining({ - visible: true, - readonly: true, - }), - }, - }, - }) - }) - - it("can update an existing view name", async () => { - const tableId = table._id! - const newName = generator.guid() - await config.api.viewV2.update({ ...view, name: newName }) - - expect(await config.api.table.get(tableId)).toEqual( - expect.objectContaining({ - views: { - [newName]: { ...view, name: newName, schema: expect.anything() }, - }, - }) - ) - }) - - it("cannot update an unexisting views nor edit ids", async () => { - const tableId = table._id! - await config.api.viewV2.update( - { ...view, id: generator.guid() }, - { status: 404 } - ) - - expect(await config.api.table.get(tableId)).toEqual( - expect.objectContaining({ - views: { - [view.name]: { - ...view, - schema: expect.anything(), - }, - }, - }) - ) - }) - - it("cannot update views with the wrong tableId", async () => { - const tableId = table._id! - await config.api.viewV2.update( - { - ...view, - tableId: generator.guid(), + primaryDisplay: "id", query: [ { operator: BasicOperator.EQUAL, - field: "newField", - value: "thatValue", + field: "field", + value: "value", }, ], - }, - { status: 404 } - ) - - expect(await config.api.table.get(tableId)).toEqual( - expect.objectContaining({ - views: { - [view.name]: { - ...view, - schema: expect.anything(), + sort: { + field: "fieldToSort", + order: SortOrder.DESCENDING, + type: SortType.STRING, + }, + schema: { + id: { visible: true }, + Price: { + visible: true, }, }, - }) - ) - }) + } + const res = await config.api.viewV2.create(newView) - it("cannot update views v1", async () => { - const viewV1 = await config.api.legacyView.save({ - tableId: table._id!, - name: generator.guid(), - filters: [], - schema: {}, - }) - - await config.api.viewV2.update(viewV1 as unknown as ViewV2, { - status: 400, - body: { - message: "Only views V2 can be updated", - status: 400, - }, - }) - }) - - it("cannot update the a view with unmatching ids between url and body", async () => { - const anotherView = await config.api.viewV2.create({ - tableId: table._id!, - name: generator.guid(), - schema: { - id: { visible: true }, - }, - }) - const result = await config - .request!.put(`/api/v2/views/${anotherView.id}`) - .send(view) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(400) - - expect(result.body).toEqual({ - message: "View id does not match between the body and the uri path", - status: 400, - }) - }) - - it("updates only UI schema overrides", async () => { - const updatedView = await config.api.viewV2.update({ - ...view, - schema: { - ...view.schema, - Price: { - name: "Price", - type: FieldType.NUMBER, - visible: true, - order: 1, - width: 100, - }, - Category: { - name: "Category", - type: FieldType.STRING, - visible: false, - icon: "ic", - }, - } as Record, - }) - - expect(updatedView).toEqual({ - ...view, - schema: { - id: { visible: true }, - Price: { - visible: true, - order: 1, - width: 100, - }, - Category: { visible: false, icon: "ic" }, - }, - id: view.id, - version: 2, - }) - }) - - it("will not throw an exception if the schema is 'deleting' non UI fields", async () => { - await config.api.viewV2.update( - { - ...view, + expect(res).toEqual({ + ...newView, schema: { - ...view.schema, + id: { visible: true }, + Price: { + visible: true, + }, + }, + id: expect.any(String), + version: 2, + }) + }) + + it("persist only UI schema overrides", async () => { + const newView: CreateViewRequest = { + name: generator.name(), + tableId: table._id!, + schema: { + id: { + name: "id", + type: FieldType.NUMBER, + visible: true, + }, + Price: { + name: "Price", + type: FieldType.NUMBER, + visible: true, + order: 1, + width: 100, + }, + Category: { + name: "Category", + type: FieldType.STRING, + visible: false, + icon: "ic", + }, + } as Record, + } + + const createdView = await config.api.viewV2.create(newView) + + expect(createdView).toEqual({ + ...newView, + schema: { + id: { visible: true }, + Price: { + visible: true, + order: 1, + width: 100, + }, + Category: { + visible: false, + icon: "ic", + }, + }, + id: createdView.id, + version: 2, + }) + }) + + it("will not throw an exception if the schema is 'deleting' non UI fields", async () => { + const newView: CreateViewRequest = { + name: generator.name(), + tableId: table._id!, + schema: { + id: { + name: "id", + type: FieldType.NUMBER, + autocolumn: true, + visible: true, + }, Price: { name: "Price", type: FieldType.NUMBER, @@ -789,66 +251,593 @@ describe.each([ type: FieldType.STRING, }, } as Record, - }, - { - status: 200, } - ) - }) - it("cannot update views with readonly on on free license", async () => { - mocks.licenses.useViewReadonlyColumns() - - view = await config.api.viewV2.update({ - ...view, - schema: { - id: { visible: true }, - Price: { - visible: true, - readonly: true, - }, - }, + await config.api.viewV2.create(newView, { + status: 201, + }) }) - mocks.licenses.useCloudFree() - await config.api.viewV2.update(view, { - status: 400, - body: { - message: "Readonly fields are not enabled", - }, + it("does not persist non-visible fields", async () => { + const newView: CreateViewRequest = { + name: generator.name(), + tableId: table._id!, + primaryDisplay: "id", + schema: { + id: { visible: true }, + Price: { visible: true }, + Category: { visible: false }, + }, + } + const res = await config.api.viewV2.create(newView) + + expect(res).toEqual({ + ...newView, + schema: { + id: { visible: true }, + Price: { visible: true }, + Category: { visible: false }, + }, + id: expect.any(String), + version: 2, + }) + }) + + it("throws bad request when the schema fields are not valid", async () => { + const newView: CreateViewRequest = { + name: generator.name(), + tableId: table._id!, + schema: { + id: { visible: true }, + nonExisting: { + visible: true, + }, + }, + } + await config.api.viewV2.create(newView, { + status: 400, + body: { + message: 'Field "nonExisting" is not valid for the requested table', + }, + }) + }) + + describe("readonly fields", () => { + beforeEach(() => { + mocks.licenses.useViewReadonlyColumns() + }) + + it("readonly fields are persisted", async () => { + const table = await config.api.table.save( + saveTableRequest({ + schema: { + name: { + name: "name", + type: FieldType.STRING, + }, + description: { + name: "description", + type: FieldType.STRING, + }, + }, + }) + ) + + const newView: CreateViewRequest = { + name: generator.name(), + tableId: table._id!, + schema: { + id: { visible: true }, + name: { + visible: true, + readonly: true, + }, + description: { + visible: true, + readonly: true, + }, + }, + } + + const res = await config.api.viewV2.create(newView) + expect(res.schema).toEqual({ + id: { visible: true }, + name: { + visible: true, + readonly: true, + }, + description: { + visible: true, + readonly: true, + }, + }) + }) + + it("required fields cannot be marked as readonly", async () => { + const table = await config.api.table.save( + saveTableRequest({ + schema: { + name: { + name: "name", + type: FieldType.STRING, + constraints: { presence: true }, + }, + description: { + name: "description", + type: FieldType.STRING, + }, + }, + }) + ) + + const newView: CreateViewRequest = { + name: generator.name(), + tableId: table._id!, + schema: { + id: { visible: true }, + name: { + visible: true, + readonly: true, + }, + }, + } + + await config.api.viewV2.create(newView, { + status: 400, + body: { + message: + 'You can\'t make "name" readonly because it is a required field.', + status: 400, + }, + }) + }) + + it("readonly fields must be visible", async () => { + const table = await config.api.table.save( + saveTableRequest({ + schema: { + name: { + name: "name", + type: FieldType.STRING, + }, + description: { + name: "description", + type: FieldType.STRING, + }, + }, + }) + ) + + const newView: CreateViewRequest = { + name: generator.name(), + tableId: table._id!, + schema: { + id: { visible: true }, + name: { + visible: false, + readonly: true, + }, + }, + } + + await config.api.viewV2.create(newView, { + status: 400, + body: { + message: + 'Field "name" must be visible if you want to make it readonly', + status: 400, + }, + }) + }) + + it("readonly fields cannot be used on free license", async () => { + mocks.licenses.useCloudFree() + const table = await config.api.table.save( + saveTableRequest({ + schema: { + name: { + name: "name", + type: FieldType.STRING, + }, + description: { + name: "description", + type: FieldType.STRING, + }, + }, + }) + ) + + const newView: CreateViewRequest = { + name: generator.name(), + tableId: table._id!, + schema: { + id: { visible: true }, + name: { + visible: true, + readonly: true, + }, + }, + } + + await config.api.viewV2.create(newView, { + status: 400, + body: { + message: "Readonly fields are not enabled", + status: 400, + }, + }) + }) + }) + + it("display fields must be visible", async () => { + const table = await config.api.table.save( + saveTableRequest({ + schema: { + name: { + name: "name", + type: FieldType.STRING, + }, + description: { + name: "description", + type: FieldType.STRING, + }, + }, + }) + ) + + const newView: CreateViewRequest = { + name: generator.name(), + tableId: table._id!, + primaryDisplay: "name", + schema: { + id: { visible: true }, + name: { + visible: false, + }, + }, + } + + await config.api.viewV2.create(newView, { + status: 400, + body: { + message: 'You can\'t hide "name" because it is the display column.', + status: 400, + }, + }) + }) + + it("display fields can be readonly", async () => { + mocks.licenses.useViewReadonlyColumns() + const table = await config.api.table.save( + saveTableRequest({ + schema: { + name: { + name: "name", + type: FieldType.STRING, + }, + description: { + name: "description", + type: FieldType.STRING, + }, + }, + }) + ) + + const newView: CreateViewRequest = { + name: generator.name(), + tableId: table._id!, + primaryDisplay: "name", + schema: { + id: { visible: true }, + name: { + visible: true, + readonly: true, + }, + }, + } + + await config.api.viewV2.create(newView, { + status: 201, + }) }) }) - it("can remove readonly config after license downgrade", async () => { - mocks.licenses.useViewReadonlyColumns() + describe("update", () => { + let view: ViewV2 - view = await config.api.viewV2.update({ - ...view, - schema: { - id: { visible: true }, - Price: { - visible: true, - readonly: true, + beforeEach(async () => { + table = await config.api.table.save(priceTable()) + + view = await config.api.viewV2.create({ + tableId: table._id!, + name: generator.guid(), + schema: { + id: { visible: true }, }, - Category: { - visible: true, - readonly: true, - }, - }, + }) }) - mocks.licenses.useCloudFree() - const res = await config.api.viewV2.update({ - ...view, - schema: { - id: { visible: true }, - Price: { - visible: true, - readonly: false, + + it("can update an existing view data", async () => { + const tableId = table._id! + await config.api.viewV2.update({ + ...view, + query: [ + { + operator: BasicOperator.EQUAL, + field: "newField", + value: "thatValue", + }, + ], + }) + + expect((await config.api.table.get(tableId)).views).toEqual({ + [view.name]: { + ...view, + query: [ + { operator: "equal", field: "newField", value: "thatValue" }, + ], + schema: expect.anything(), }, - }, + }) }) - expect(res).toEqual( - expect.objectContaining({ + + it("can update all fields", async () => { + mocks.licenses.useViewReadonlyColumns() + const tableId = table._id! + + const updatedData: Required = { + version: view.version, + id: view.id, + tableId, + name: view.name, + primaryDisplay: "Price", + query: [ + { + operator: BasicOperator.EQUAL, + field: generator.word(), + value: generator.word(), + }, + ], + sort: { + field: generator.word(), + order: SortOrder.DESCENDING, + type: SortType.STRING, + }, + schema: { + id: { visible: true }, + Category: { + visible: false, + }, + Price: { + visible: true, + readonly: true, + }, + }, + } + await config.api.viewV2.update(updatedData) + + expect((await config.api.table.get(tableId)).views).toEqual({ + [view.name]: { + ...updatedData, + schema: { + ...table.schema, + id: expect.objectContaining({ + visible: true, + }), + Category: expect.objectContaining({ + visible: false, + }), + Price: expect.objectContaining({ + visible: true, + readonly: true, + }), + }, + }, + }) + }) + + it("can update an existing view name", async () => { + const tableId = table._id! + const newName = generator.guid() + await config.api.viewV2.update({ ...view, name: newName }) + + expect(await config.api.table.get(tableId)).toEqual( + expect.objectContaining({ + views: { + [newName]: { ...view, name: newName, schema: expect.anything() }, + }, + }) + ) + }) + + it("cannot update an unexisting views nor edit ids", async () => { + const tableId = table._id! + await config.api.viewV2.update( + { ...view, id: generator.guid() }, + { status: 404 } + ) + + expect(await config.api.table.get(tableId)).toEqual( + expect.objectContaining({ + views: { + [view.name]: { + ...view, + schema: expect.anything(), + }, + }, + }) + ) + }) + + it("cannot update views with the wrong tableId", async () => { + const tableId = table._id! + await config.api.viewV2.update( + { + ...view, + tableId: generator.guid(), + query: [ + { + operator: BasicOperator.EQUAL, + field: "newField", + value: "thatValue", + }, + ], + }, + { status: 404 } + ) + + expect(await config.api.table.get(tableId)).toEqual( + expect.objectContaining({ + views: { + [view.name]: { + ...view, + schema: expect.anything(), + }, + }, + }) + ) + }) + + it("cannot update views v1", async () => { + const viewV1 = await config.api.legacyView.save({ + tableId: table._id!, + name: generator.guid(), + filters: [], + schema: {}, + }) + + await config.api.viewV2.update(viewV1 as unknown as ViewV2, { + status: 400, + body: { + message: "Only views V2 can be updated", + status: 400, + }, + }) + }) + + it("cannot update the a view with unmatching ids between url and body", async () => { + const anotherView = await config.api.viewV2.create({ + tableId: table._id!, + name: generator.guid(), + schema: { + id: { visible: true }, + }, + }) + const result = await config + .request!.put(`/api/v2/views/${anotherView.id}`) + .send(view) + .set(config.defaultHeaders()) + .expect("Content-Type", /json/) + .expect(400) + + expect(result.body).toEqual({ + message: "View id does not match between the body and the uri path", + status: 400, + }) + }) + + it("updates only UI schema overrides", async () => { + const updatedView = await config.api.viewV2.update({ + ...view, + schema: { + ...view.schema, + Price: { + name: "Price", + type: FieldType.NUMBER, + visible: true, + order: 1, + width: 100, + }, + Category: { + name: "Category", + type: FieldType.STRING, + visible: false, + icon: "ic", + }, + } as Record, + }) + + expect(updatedView).toEqual({ + ...view, + schema: { + id: { visible: true }, + Price: { + visible: true, + order: 1, + width: 100, + }, + Category: { visible: false, icon: "ic" }, + }, + id: view.id, + version: 2, + }) + }) + + it("will not throw an exception if the schema is 'deleting' non UI fields", async () => { + await config.api.viewV2.update( + { + ...view, + schema: { + ...view.schema, + Price: { + name: "Price", + type: FieldType.NUMBER, + visible: true, + }, + Category: { + name: "Category", + type: FieldType.STRING, + }, + } as Record, + }, + { + status: 200, + } + ) + }) + + it("cannot update views with readonly on on free license", async () => { + mocks.licenses.useViewReadonlyColumns() + + view = await config.api.viewV2.update({ + ...view, + schema: { + id: { visible: true }, + Price: { + visible: true, + readonly: true, + }, + }, + }) + + mocks.licenses.useCloudFree() + await config.api.viewV2.update(view, { + status: 400, + body: { + message: "Readonly fields are not enabled", + }, + }) + }) + + it("can remove readonly config after license downgrade", async () => { + mocks.licenses.useViewReadonlyColumns() + + view = await config.api.viewV2.update({ + ...view, + schema: { + id: { visible: true }, + Price: { + visible: true, + readonly: true, + }, + Category: { + visible: true, + readonly: true, + }, + }, + }) + mocks.licenses.useCloudFree() + const res = await config.api.viewV2.update({ ...view, schema: { id: { visible: true }, @@ -858,77 +847,88 @@ describe.each([ }, }, }) - ) + expect(res).toEqual( + expect.objectContaining({ + ...view, + schema: { + id: { visible: true }, + Price: { + visible: true, + readonly: false, + }, + }, + }) + ) + }) + + isInternal && + it("updating schema will only validate modified field", async () => { + let view = await config.api.viewV2.create({ + tableId: table._id!, + name: generator.guid(), + schema: { + id: { visible: true }, + Price: { + visible: true, + }, + Category: { visible: true }, + }, + }) + + // Update the view to an invalid state + const tableToUpdate = await config.api.table.get(table._id!) + ;(tableToUpdate.views![view.name] as ViewV2).schema!.id.visible = + false + await db.getDB(config.appId!).put(tableToUpdate) + + view = await config.api.viewV2.get(view.id) + await config.api.viewV2.update( + { + ...view, + schema: { + ...view.schema, + Price: { + visible: false, + }, + }, + }, + { + status: 400, + body: { + message: 'You can\'t hide "id" because it is a required field.', + status: 400, + }, + } + ) + }) }) - isInternal && - it("updating schema will only validate modified field", async () => { - let view = await config.api.viewV2.create({ + describe("delete", () => { + let view: ViewV2 + + beforeAll(async () => { + view = await config.api.viewV2.create({ tableId: table._id!, name: generator.guid(), schema: { id: { visible: true }, - Price: { - visible: true, - }, - Category: { visible: true }, }, }) - - // Update the view to an invalid state - const tableToUpdate = await config.api.table.get(table._id!) - ;(tableToUpdate.views![view.name] as ViewV2).schema!.id.visible = - false - await db.getDB(config.appId!).put(tableToUpdate) - - view = await config.api.viewV2.get(view.id) - await config.api.viewV2.update( - { - ...view, - schema: { - ...view.schema, - Price: { - visible: false, - }, - }, - }, - { - status: 400, - body: { - message: 'You can\'t hide "id" because it is a required field.', - status: 400, - }, - } - ) }) - }) - describe("delete", () => { - let view: ViewV2 + it("can delete an existing view", async () => { + const tableId = table._id! + const getPersistedView = async () => + (await config.api.table.get(tableId)).views![view.name] - beforeAll(async () => { - view = await config.api.viewV2.create({ - tableId: table._id!, - name: generator.guid(), - schema: { - id: { visible: true }, - }, + expect(await getPersistedView()).toBeDefined() + + await config.api.viewV2.delete(view.id) + + expect(await getPersistedView()).toBeUndefined() }) }) - it("can delete an existing view", async () => { - const tableId = table._id! - const getPersistedView = async () => - (await config.api.table.get(tableId)).views![view.name] - - expect(await getPersistedView()).toBeDefined() - - await config.api.viewV2.delete(view.id) - - expect(await getPersistedView()).toBeUndefined() - }) - }) - describe.each([ ["from view api", (view: ViewV2) => config.api.viewV2.get(view.id)], [ @@ -976,13 +976,13 @@ describe.each([ }) expect(await getDelegate(view)).toEqual({ - ...view, - schema: { - id: { ...table.schema["id"], visible: true }, - one: { ...table.schema["one"], visible: true }, - two: { ...table.schema["two"], visible: true }, - three: { ...table.schema["three"], visible: false }, - }, + ...view, + schema: { + id: { ...table.schema["id"], visible: true }, + one: { ...table.schema["one"], visible: true }, + two: { ...table.schema["two"], visible: true }, + three: { ...table.schema["three"], visible: false }, + }, }) }) @@ -1040,44 +1040,44 @@ describe.each([ }) }) - describe("fetch view (through table)", () => { - it("should be able to fetch a view V2", async () => { - const res = await config.api.viewV2.create({ - name: generator.name(), - tableId: table._id!, - schema: { - id: { visible: true }, - Price: { visible: false }, - Category: { visible: true }, - }, + describe("fetch view (through table)", () => { + it("should be able to fetch a view V2", async () => { + const res = await config.api.viewV2.create({ + name: generator.name(), + tableId: table._id!, + schema: { + id: { visible: true }, + Price: { visible: false }, + Category: { visible: true }, + }, + }) + + const view = await config.api.viewV2.get(res.id) + const updatedTable = await config.api.table.get(table._id!) + const viewSchema = updatedTable.views![view!.name!].schema as Record< + string, + ViewUIFieldMetadata + > + expect(viewSchema.Price?.visible).toEqual(false) + expect(viewSchema.Category?.visible).toEqual(true) }) - const view = await config.api.viewV2.get(res.id) - const updatedTable = await config.api.table.get(table._id!) - const viewSchema = updatedTable.views![view!.name!].schema as Record< - string, - ViewUIFieldMetadata - > - expect(viewSchema.Price?.visible).toEqual(false) - expect(viewSchema.Category?.visible).toEqual(true) - }) + it("should be able to fetch readonly config after downgrades", async () => { + mocks.licenses.useViewReadonlyColumns() + const res = await config.api.viewV2.create({ + name: generator.name(), + tableId: table._id!, + schema: { + id: { visible: true }, + Price: { visible: true, readonly: true }, + }, + }) - it("should be able to fetch readonly config after downgrades", async () => { - mocks.licenses.useViewReadonlyColumns() - const res = await config.api.viewV2.create({ - name: generator.name(), - tableId: table._id!, - schema: { - id: { visible: true }, - Price: { visible: true, readonly: true }, - }, - }) - - mocks.licenses.useCloudFree() - const view = await config.api.viewV2.get(res.id) - expect(view.schema?.Price).toEqual( - expect.objectContaining({ visible: true, readonly: true }) - ) + mocks.licenses.useCloudFree() + const view = await config.api.viewV2.get(res.id) + expect(view.schema?.Price).toEqual( + expect.objectContaining({ visible: true, readonly: true }) + ) }) }) diff --git a/packages/server/src/sdk/app/views/index.ts b/packages/server/src/sdk/app/views/index.ts index 0fc692b108..ed624c2b5c 100644 --- a/packages/server/src/sdk/app/views/index.ts +++ b/packages/server/src/sdk/app/views/index.ts @@ -13,7 +13,6 @@ import { PROTECTED_EXTERNAL_COLUMNS, PROTECTED_INTERNAL_COLUMNS, } from "@budibase/shared-core" -import { cloneDeep } from "lodash/fp" import * as utils from "../../../db/utils" import { isExternalTableID } from "../../../integrations/utils" @@ -163,17 +162,19 @@ export function enrichSchema( view: ViewV2, tableSchema: TableSchema ): ViewV2Enriched { - let schema = cloneDeep(tableSchema) + let schema: TableSchema = {} const anyViewOrder = Object.values(view.schema || {}).some( ui => ui.order != null ) - for (const key of Object.keys(schema)) { + for (const key of Object.keys(tableSchema).filter( + key => tableSchema[key].visible !== false + )) { // if nothing specified in view, then it is not visible const ui = view.schema?.[key] || { visible: false } schema[key] = { - ...schema[key], + ...tableSchema[key], ...ui, - order: anyViewOrder ? ui?.order ?? undefined : schema[key].order, + order: anyViewOrder ? ui?.order ?? undefined : tableSchema[key].order, } } From 19e97dee502506c9beaf8553c7b3e17e074399c1 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 28 Aug 2024 14:26:00 +0200 Subject: [PATCH 10/21] Unify tests --- .../src/api/routes/tests/viewV2.spec.ts | 29 ++----------------- 1 file changed, 3 insertions(+), 26 deletions(-) diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index 8cde673a07..f64ea79888 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -1038,29 +1038,6 @@ describe.each([ }, }) }) - }) - - describe("fetch view (through table)", () => { - it("should be able to fetch a view V2", async () => { - const res = await config.api.viewV2.create({ - name: generator.name(), - tableId: table._id!, - schema: { - id: { visible: true }, - Price: { visible: false }, - Category: { visible: true }, - }, - }) - - const view = await config.api.viewV2.get(res.id) - const updatedTable = await config.api.table.get(table._id!) - const viewSchema = updatedTable.views![view!.name!].schema as Record< - string, - ViewUIFieldMetadata - > - expect(viewSchema.Price?.visible).toEqual(false) - expect(viewSchema.Category?.visible).toEqual(true) - }) it("should be able to fetch readonly config after downgrades", async () => { mocks.licenses.useViewReadonlyColumns() @@ -1069,13 +1046,13 @@ describe.each([ tableId: table._id!, schema: { id: { visible: true }, - Price: { visible: true, readonly: true }, + one: { visible: true, readonly: true }, }, }) mocks.licenses.useCloudFree() - const view = await config.api.viewV2.get(res.id) - expect(view.schema?.Price).toEqual( + const view = (await getDelegate(res)) as ViewV2 + expect(view.schema?.one).toEqual( expect.objectContaining({ visible: true, readonly: true }) ) }) From f2d9be985dcf471a72e233c3c69e18918bef441a Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 28 Aug 2024 14:36:19 +0200 Subject: [PATCH 11/21] Lint --- packages/server/src/api/routes/tests/viewV2.spec.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index f64ea79888..4727dfc95d 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -15,7 +15,6 @@ import { Table, TableSourceType, UpdateViewRequest, - ViewUIFieldMetadata, ViewV2, SearchResponse, BasicOperator, From d71b18be007ca24d9a8634535a20e892ab9009ff Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 28 Aug 2024 14:44:08 +0200 Subject: [PATCH 12/21] Fix tests --- .../src/sdk/app/views/tests/views.spec.ts | 18 ------------------ 1 file changed, 18 deletions(-) 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 508285651a..9dd4a7fb69 100644 --- a/packages/server/src/sdk/app/views/tests/views.spec.ts +++ b/packages/server/src/sdk/app/views/tests/views.spec.ts @@ -101,14 +101,6 @@ describe("table sdk", () => { type: "number", }, }, - hiddenField: { - type: "string", - name: "hiddenField", - visible: false, - constraints: { - type: "string", - }, - }, }, }) }) @@ -143,10 +135,6 @@ describe("table sdk", () => { ...basicTable.schema.id, visible: true, }, - hiddenField: { - ...basicTable.schema.hiddenField, - visible: false, - }, }, }) }) @@ -181,10 +169,6 @@ describe("table sdk", () => { ...basicTable.schema.id, visible: false, }, - hiddenField: { - ...basicTable.schema.hiddenField, - visible: false, - }, }, }) }) @@ -209,7 +193,6 @@ describe("table sdk", () => { expect.objectContaining({ ...view, schema: { - ...basicTable.schema, name: { type: "string", name: "name", @@ -264,7 +247,6 @@ describe("table sdk", () => { expect.objectContaining({ ...view, schema: { - ...basicTable.schema, name: { type: "string", name: "name", From 2bcc8cdf672b2fe68cb98e589db7d116b70f64c0 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 28 Aug 2024 16:14:43 +0100 Subject: [PATCH 13/21] Update packages/pro refernce to latest master. --- packages/pro | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/pro b/packages/pro index 51a83b791a..11825666de 160000 --- a/packages/pro +++ b/packages/pro @@ -1 +1 @@ -Subproject commit 51a83b791a7a11b1d51c1fdb91f2ac246298279e +Subproject commit 11825666de3516fe084904947be332ed50c21d2a From 06592a0afca522d57f3e30b33d2833d406fa045e Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 28 Aug 2024 16:14:58 +0100 Subject: [PATCH 14/21] Update packages/pro refernce to latest master. --- packages/pro | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/pro b/packages/pro index 11825666de..f2862855dc 160000 --- a/packages/pro +++ b/packages/pro @@ -1 +1 @@ -Subproject commit 11825666de3516fe084904947be332ed50c21d2a +Subproject commit f2862855dcfeb4f4a28d6902daf411ec8f4a28e8 From c1c6c4203e1f1d0fb820ecce24e901f3505c68f4 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 28 Aug 2024 16:28:44 +0100 Subject: [PATCH 15/21] Update packages/account-portal reference. --- packages/account-portal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/account-portal b/packages/account-portal index 516b27b74c..8da7439660 160000 --- a/packages/account-portal +++ b/packages/account-portal @@ -1 +1 @@ -Subproject commit 516b27b74cbcb7069a25f5e738dc91c22d7c4538 +Subproject commit 8da7439660d0a2d759abaf41b31b3ec4ed7c6489 From 64682211e2ead305efdf7d7d6d465d9ea9074938 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 28 Aug 2024 16:45:23 +0100 Subject: [PATCH 16/21] Update packages/account-portal reference. --- packages/account-portal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/account-portal b/packages/account-portal index 8da7439660..9a0ef2714f 160000 --- a/packages/account-portal +++ b/packages/account-portal @@ -1 +1 @@ -Subproject commit 8da7439660d0a2d759abaf41b31b3ec4ed7c6489 +Subproject commit 9a0ef2714fca4f6c99e2fa7f0f28c8a6c97ddf4f From 9ef151c6a68a80c9591606225c3a0155f73e2f69 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 28 Aug 2024 16:47:04 +0100 Subject: [PATCH 17/21] Update packages/account-portal reference. --- packages/account-portal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/account-portal b/packages/account-portal index 9a0ef2714f..da6485c83b 160000 --- a/packages/account-portal +++ b/packages/account-portal @@ -1 +1 @@ -Subproject commit 9a0ef2714fca4f6c99e2fa7f0f28c8a6c97ddf4f +Subproject commit da6485c83b7229185cf3066194d5febb09eb4925 From d507c7d68e2cef63f25671f502621e2e8e3dd194 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 28 Aug 2024 17:21:21 +0100 Subject: [PATCH 18/21] Update packages/account-portal reference. --- packages/account-portal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/account-portal b/packages/account-portal index da6485c83b..c403315c5f 160000 --- a/packages/account-portal +++ b/packages/account-portal @@ -1 +1 @@ -Subproject commit da6485c83b7229185cf3066194d5febb09eb4925 +Subproject commit c403315c5fa09a05dfd8fa4cd1890acfd8de0430 From 1fab11bc1a80475bffd80c34842c44d7f8d08ffb Mon Sep 17 00:00:00 2001 From: Budibase Staging Release Bot <> Date: Thu, 29 Aug 2024 08:54:12 +0000 Subject: [PATCH 19/21] Bump version to 2.31.3 --- lerna.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lerna.json b/lerna.json index 00a472c483..754041d6ef 100644 --- a/lerna.json +++ b/lerna.json @@ -1,6 +1,6 @@ { "$schema": "node_modules/lerna/schemas/lerna-schema.json", - "version": "2.31.2", + "version": "2.31.3", "npmClient": "yarn", "packages": [ "packages/*", From 6028ddec843ccb8fb8955018a69690b4fe6338e9 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 29 Aug 2024 12:34:12 +0200 Subject: [PATCH 20/21] Move cast --- packages/server/src/api/routes/tests/viewV2.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index 4727dfc95d..4829ed4994 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -934,7 +934,7 @@ describe.each([ "from table", async (view: ViewV2) => { const table = await config.api.table.get(view.tableId) - return (table.views || {})[view.name] + return table.views![view.name] as ViewV2 }, ], ])("read (%s)", (_, getDelegate) => { @@ -1050,7 +1050,7 @@ describe.each([ }) mocks.licenses.useCloudFree() - const view = (await getDelegate(res)) as ViewV2 + const view = await getDelegate(res) expect(view.schema?.one).toEqual( expect.objectContaining({ visible: true, readonly: true }) ) From d2b6959b4427daef28d04b26e9ac6d6a3d71b82c Mon Sep 17 00:00:00 2001 From: Dean Date: Thu, 29 Aug 2024 12:58:12 +0100 Subject: [PATCH 21/21] Reduced the z-index to just 1 above the binding drawer. The updated value caused index issues in other areas of budibase. --- packages/bbui/src/Modal/Modal.svelte | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/bbui/src/Modal/Modal.svelte b/packages/bbui/src/Modal/Modal.svelte index 4d55a8a266..c5a6ffc17b 100644 --- a/packages/bbui/src/Modal/Modal.svelte +++ b/packages/bbui/src/Modal/Modal.svelte @@ -10,7 +10,7 @@ export let inline = false export let disableCancel = false export let autoFocus = true - export let zIndex = 99999 + export let zIndex = 1001 const dispatch = createEventDispatcher() let visible = fixed || inline