Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/templates/entities/entity-col-boolean.eta
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@IsOptional({ groups: [CREATE, UPDATE] })
@IsBoolean({ always: true })

@Column({ type: 'boolean', nullable: <%= Boolean(field.nullable) %>, default: <%= Boolean(field.default) || false %> })
@Column({ type: 'boolean', nullable: <%= Boolean(field.nullable) %>, default: <%= field.default === true || String(field.default).toLowerCase() === 'true' %> })
<% if (field.index ) {%>
@Index()
<% } %>
Expand Down
33 changes: 33 additions & 0 deletions test/lib/generators/typescript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,39 @@ describe("TypeScriptGenerator", () => {
expect(entityContent).toContain('@Column({ "type": "uuid", nullable: true })');
});

test("boolean defaults are false unless the default is true", async () => {
const entity: Entity = {
name: "Flag",
primaryKeyType: "serial",
fields: [
{ name: "noDefault", type: "boolean" },
{ name: "falseString", type: "boolean", default: "false" },
{ name: "falseBool", type: "boolean", default: false },
{ name: "trueString", type: "boolean", default: "true" },
{ name: "trueBool", type: "boolean", default: true },
] as unknown as Field[],
};

const files = await generator.generateEntity({
entity,
relationships: [],
allEntities: [entity],
apiType: "rest",
});

const content = findFileContent(files, "Flag.entity")!;
const defaultOf = (name: string) =>
content
.split("@Column(")
.find((c) => c.includes(`${name}!:`))
?.match(/default: (\w+)/)?.[1];
expect(defaultOf("noDefault")).toBe("false");
expect(defaultOf("falseString")).toBe("false");
expect(defaultOf("falseBool")).toBe("false");
expect(defaultOf("trueString")).toBe("true");
expect(defaultOf("trueBool")).toBe("true");
});

test("timestamptz, timestamp, and datetime generate instant-in-time columns honoring nullable", async () => {
const entity: Entity = {
name: "Event",
Expand Down
Loading