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 19, 2021
1 parent 3cdc786 commit 908a638
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 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 @@ -63,7 +63,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
4 changes: 2 additions & 2 deletions packages/knex/src/schema/DatabaseTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,8 @@ export class DatabaseTable {
return empty;
}

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

if (propType === 'number') {
Expand Down
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 @@ -566,7 +566,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 @@ -930,7 +930,7 @@ export class Author3 {
age?: number;
@Property()
termsAccepted!: number;
termsAccepted: number = 0;
@Property({ nullable: true })
identities?: string;
Expand Down

0 comments on commit 908a638

Please sign in to comment.