Support bindings in default values.
This commit is contained in:
parent
12911db06e
commit
297e9003ca
|
@ -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", () => {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue