From e6e25fdf943d69e975f025bec9e51dc26227dec1 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Mon, 7 Oct 2024 14:59:29 +0100 Subject: [PATCH 1/3] Allow calculation views to hide required fields. --- .../src/api/routes/tests/viewV2.spec.ts | 20 +++++++++++++++++++ packages/server/src/sdk/app/views/index.ts | 3 ++- 2 files changed, 22 insertions(+), 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 5238b9b752..22c83419a4 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -1024,6 +1024,26 @@ describe.each([ ) }) + it("can add a new group by field that is invisible, even if required on the table", async () => { + view.schema!.name = { visible: false } + await config.api.viewV2.update(view) + + const { rows } = await config.api.row.search(view.id) + expect(rows).toHaveLength(2) + expect(rows).toEqual( + expect.arrayContaining([ + { + country: "USA", + age: 65, + }, + { + country: "UK", + age: 61, + }, + ]) + ) + }) + it("can add a new calculation field", async () => { view.schema!.count = { visible: true, diff --git a/packages/server/src/sdk/app/views/index.ts b/packages/server/src/sdk/app/views/index.ts index 73785edd98..32fd696f20 100644 --- a/packages/server/src/sdk/app/views/index.ts +++ b/packages/server/src/sdk/app/views/index.ts @@ -178,7 +178,7 @@ function checkRequiredFields( continue } - if (!viewSchemaField?.visible) { + if (!helpers.views.isCalculationView(view) && !viewSchemaField?.visible) { throw new HTTPError( `You can't hide "${field.name}" because it is a required field.`, 400 @@ -186,6 +186,7 @@ function checkRequiredFields( } if ( + viewSchemaField && helpers.views.isBasicViewField(viewSchemaField) && viewSchemaField.readonly ) { From f2e78ec4d500ae03b3bd1e2530c9e0893fdefe91 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Mon, 7 Oct 2024 16:39:44 +0100 Subject: [PATCH 2/3] Don't check required fields at all for calculation views. --- packages/server/src/sdk/app/views/index.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/server/src/sdk/app/views/index.ts b/packages/server/src/sdk/app/views/index.ts index 32fd696f20..fe2360f9d0 100644 --- a/packages/server/src/sdk/app/views/index.ts +++ b/packages/server/src/sdk/app/views/index.ts @@ -114,7 +114,11 @@ async function guardViewSchema( } await checkReadonlyFields(table, view) - checkRequiredFields(table, view) + + if (!helpers.views.isCalculationView(view)) { + checkRequiredFields(table, view) + } + checkDisplayField(view) } @@ -178,7 +182,7 @@ function checkRequiredFields( continue } - if (!helpers.views.isCalculationView(view) && !viewSchemaField?.visible) { + if (!viewSchemaField?.visible) { throw new HTTPError( `You can't hide "${field.name}" because it is a required field.`, 400 @@ -186,7 +190,6 @@ function checkRequiredFields( } if ( - viewSchemaField && helpers.views.isBasicViewField(viewSchemaField) && viewSchemaField.readonly ) { From a4a90d7456e2a1521fe46ed73f3cc3fa6174feaa Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Tue, 8 Oct 2024 09:56:51 +0100 Subject: [PATCH 3/3] Fix tests. --- .../backend-core/src/db/couch/DatabaseImpl.ts | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/packages/backend-core/src/db/couch/DatabaseImpl.ts b/packages/backend-core/src/db/couch/DatabaseImpl.ts index 2ca52a2dce..1213dde8f5 100644 --- a/packages/backend-core/src/db/couch/DatabaseImpl.ts +++ b/packages/backend-core/src/db/couch/DatabaseImpl.ts @@ -371,15 +371,21 @@ export class DatabaseImpl implements Database { return this.performCall(() => { return async () => { const response = await directCouchUrlCall(args) - if (response.status >= 300) { - const text = await response.text() - console.error(`SQS error: ${text}`) - throw new CouchDBError( - "error while running SQS query, please try again later", - { name: "sqs_error", status: response.status } - ) + const text = await response.text() + if (response.status > 300) { + let json + try { + json = JSON.parse(text) + } catch (err) { + console.error(`SQS error: ${text}`) + throw new CouchDBError( + "error while running SQS query, please try again later", + { name: "sqs_error", status: response.status } + ) + } + throw json } - return (await response.json()) as T + return JSON.parse(text) as T } }) }