diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index 482f80a587..27c363e223 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -207,23 +207,6 @@ describe.each([ await assertRowUsage(isInternal ? rowUsage + 1 : rowUsage) }) - it.only("creates a new row with a default value successfully", async () => { - const table = await config.api.table.save( - saveTableRequest({ - schema: { - description: { - name: "description", - type: FieldType.STRING, - default: "default description", - }, - }, - }) - ) - - const row = await config.api.row.save(table._id!, {}) - expect(row.description).toEqual("default description") - }) - it("fails to create a row for a table that does not exist", async () => { const rowUsage = await getRowUsage() await config.api.row.save("1234567", {}, { status: 404 }) @@ -567,6 +550,44 @@ describe.each([ expect(row.name).toEqual(`{ "foo": "2023-01-26T11:48:57.000Z" }`) }) + + describe.only("default values", () => { + it("creates a new row with a default value successfully", async () => { + const table = await config.api.table.save( + saveTableRequest({ + schema: { + description: { + name: "description", + type: FieldType.STRING, + default: "default description", + }, + }, + }) + ) + + const row = await config.api.row.save(table._id!, {}) + expect(row.description).toEqual("default description") + }) + + it("can use bindings in default values", async () => { + const table = await config.api.table.save( + saveTableRequest({ + schema: { + description: { + name: "description", + type: FieldType.STRING, + default: `{{ date now "YYYY-MM-DDTHH:mm:ss" }}`, + }, + }, + }) + ) + + await tk.withFreeze(new Date("2023-01-26T11:48:57.000Z"), async () => { + const row = await config.api.row.save(table._id!, {}) + expect(row.description).toEqual("2023-01-26T11:48:57") + }) + }) + }) }) describe("get", () => { diff --git a/packages/server/src/utilities/rowProcessor/index.ts b/packages/server/src/utilities/rowProcessor/index.ts index 402295ba96..0765873389 100644 --- a/packages/server/src/utilities/rowProcessor/index.ts +++ b/packages/server/src/utilities/rowProcessor/index.ts @@ -19,6 +19,7 @@ import { } from "./bbReferenceProcessor" import { isExternalTableID } from "../../integrations/utils" import { helpers } from "@budibase/shared-core" +import { processString } from "@budibase/string-templates" export * from "./utils" export * from "./attachments" @@ -92,8 +93,9 @@ export async function processAutoColumn( async function processDeafultValues(table: Table, row: Row) { for (let [key, schema] of Object.entries(table.schema)) { - if ("default" in schema && row[key] == null) { - row[key] = schema.default + if ("default" in schema && schema.default != null && row[key] == null) { + const processed = await processString(schema.default, {}) + row[key] = coerce(processed, schema.type) } } }