From 1bcd2590d3285f404e4f84c48bae8e8cf6992589 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Fri, 11 Oct 2024 12:28:20 +0100 Subject: [PATCH 1/2] Allow BB_REFERNCE_SINGLE to have default values. --- .../server/src/api/routes/tests/row.spec.ts | 32 +++++++++++++++++++ packages/shared-core/src/table.ts | 2 +- .../types/src/documents/app/table/schema.ts | 1 + 3 files changed, 34 insertions(+), 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 b86ec38d08..61031a79b4 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -14,6 +14,7 @@ import { InternalTable, tenancy, features, + utils, } from "@budibase/backend-core" import { quotas } from "@budibase/pro" import { @@ -757,6 +758,37 @@ describe.each([ }) }) + describe.only("user column", () => { + beforeAll(async () => { + table = await config.api.table.save( + saveTableRequest({ + schema: { + user: { + name: "user", + type: FieldType.BB_REFERENCE_SINGLE, + subtype: BBReferenceFieldSubType.USER, + default: "{{ [Current User]._id }}", + }, + }, + }) + ) + }) + + it("creates a new row with a default value successfully", async () => { + const row = await config.api.row.save(table._id!, {}) + expect(row.user._id).toEqual(config.getUser()._id) + }) + + it("does not use default value if value specified", async () => { + const id = `us_${utils.newid()}` + await config.createUser({ _id: id }) + const row = await config.api.row.save(table._id!, { + user: id, + }) + expect(row.user._id).toEqual(id) + }) + }) + describe("bindings", () => { describe("string column", () => { beforeAll(async () => { diff --git a/packages/shared-core/src/table.ts b/packages/shared-core/src/table.ts index 57f6854604..677b1e2357 100644 --- a/packages/shared-core/src/table.ts +++ b/packages/shared-core/src/table.ts @@ -67,7 +67,7 @@ const allowDefaultColumnByType: Record = { [FieldType.SIGNATURE_SINGLE]: false, [FieldType.LINK]: false, [FieldType.BB_REFERENCE]: false, - [FieldType.BB_REFERENCE_SINGLE]: false, + [FieldType.BB_REFERENCE_SINGLE]: true, } export function canBeDisplayColumn(type: FieldType): boolean { diff --git a/packages/types/src/documents/app/table/schema.ts b/packages/types/src/documents/app/table/schema.ts index f9d1a4c012..f5bb081fd5 100644 --- a/packages/types/src/documents/app/table/schema.ts +++ b/packages/types/src/documents/app/table/schema.ts @@ -126,6 +126,7 @@ export interface BBReferenceSingleFieldMetadata extends Omit { type: FieldType.BB_REFERENCE_SINGLE subtype: Exclude + default?: string } export interface AttachmentFieldMetadata extends BaseFieldSchema { From 158e0950450fb66742d0819a64460f979f528b9b Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Fri, 11 Oct 2024 16:24:03 +0100 Subject: [PATCH 2/2] Fix lint. --- 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 61031a79b4..6490b4770a 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -758,7 +758,7 @@ describe.each([ }) }) - describe.only("user column", () => { + describe("user column", () => { beforeAll(async () => { table = await config.api.table.save( saveTableRequest({