Skip to content

Commit

Permalink
fix(entity-generator): fix boolean default values
Browse files Browse the repository at this point in the history
Closes #1917
  • Loading branch information
B4nan committed Jun 29, 2021
1 parent ed399b1 commit 219fc0c
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/entity-generator/src/SourceFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class SourceFile {

private getPropertyDefinition(prop: EntityProperty, padLeft: number): string {
// string defaults are usually things like SQL functions
const useDefault = prop.default && typeof prop.default !== 'string';
const useDefault = prop.default != null && typeof prop.default !== 'string';
const optional = prop.nullable ? '?' : (useDefault ? '' : '!');
const ret = `${prop.name}${optional}: ${prop.type}`;
const padding = ' '.repeat(padLeft);
Expand Down
2 changes: 1 addition & 1 deletion packages/knex/src/schema/DatabaseTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export class DatabaseTable {
}

if (propType === 'boolean') {
return !!column.defaultValue;
return !['0', 'false', 'f', 'n', 'no', 'off'].includes('' + column.defaultValue);
}

if (propType === 'number') {
Expand Down
6 changes: 3 additions & 3 deletions tests/__snapshots__/EntityGenerator.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class Author2 {
@Index({ name: 'author2_terms_accepted_index' })
@Property()
termsAccepted: boolean = true;
termsAccepted: boolean = false;
@Property({ nullable: true })
optional?: boolean;
Expand Down Expand Up @@ -560,7 +560,7 @@ export class Author2 {
@Index({ name: 'author2_terms_accepted_index' })
@Property()
termsAccepted: boolean = true;
termsAccepted: boolean = false;
@Property({ nullable: true })
optional?: boolean;
Expand Down Expand Up @@ -917,7 +917,7 @@ export class Author3 {
age?: number;
@Property()
termsAccepted!: number;
termsAccepted: number = 0;
@Property({ columnType: 'text', nullable: true })
identities?: string;
Expand Down

0 comments on commit 219fc0c

Please sign in to comment.