Skip to content

Commit

Permalink
fix(generator): fixed default values and types for nullable properties (
Browse files Browse the repository at this point in the history
  • Loading branch information
pepakriz authored and B4nan committed Oct 10, 2019
1 parent 06f7d29 commit 1cdccd3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 55 deletions.
10 changes: 6 additions & 4 deletions lib/schema/EntityGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,23 @@ export class EntityGenerator {
const columnType = this.getPropertyType(column, '__false') === '__false' ? column.type : undefined;
const defaultValue = this.getPropertyDefaultValue(column, type);
const decorator = this.getPropertyDecorator(prop, column, type, defaultValue, columnType);
const definition = this.getPropertyDefinition(prop, type, defaultValue);
const definition = this.getPropertyDefinition(column, prop, type, defaultValue);

writer.blankLineIfLastNot();
writer.writeLine(decorator);
writer.writeLine(definition);
writer.blankLine();
}

private getPropertyDefinition(prop: string, type: string, defaultValue: any): string {
private getPropertyDefinition(column: Column, prop: string, type: string, defaultValue: any): string {
const ret = `${prop}${column.nullable ? '?' : ''}: ${type}`;

// string defaults are usually things like SQL functions
if (!defaultValue || typeof defaultValue === 'string') {
return `${prop}: ${type};`;
return ret + ';';
}

return `${prop}: ${type} = ${defaultValue};`;
return `${ret} = ${defaultValue};`;
}

private getPropertyDecorator(prop: string, column: Column, type: string, defaultValue: any, columnType?: string): string {
Expand Down
102 changes: 51 additions & 51 deletions tests/__snapshots__/EntityGenerator.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ export class Author2 {
email: string;
@Property({ nullable: true })
age: number;
age?: number;
@Property()
termsAccepted: boolean = true;
@Property({ nullable: true })
identities: object;
identities?: object;
@Property({ nullable: true })
born: Date;
born?: Date;
@ManyToOne({ entity: () => Book2, nullable: true })
favouriteBook: Book2;
favouriteBook?: Book2;
@ManyToOne({ entity: () => Author2, nullable: true })
favouriteAuthor: Author2;
favouriteAuthor?: Author2;
}
",
Expand All @@ -57,28 +57,28 @@ export class Book2 {
createdAt: Date;
@Property({ length: 255, nullable: true })
title: string;
title?: string;
@Property({ type: 'text', length: 65535, nullable: true })
perex: string;
perex?: string;
@Property({ type: 'float', nullable: true })
price: number;
price?: number;
@Property({ type: 'double', nullable: true })
double: number;
double?: number;
@Property({ nullable: true })
meta: object;
meta?: object;
@ManyToOne({ entity: () => Author2, cascade: [Cascade.MERGE], nullable: true })
author: Author2;
author?: Author2;
@ManyToOne({ entity: () => Publisher2, cascade: [Cascade.ALL], nullable: true })
publisher: Publisher2;
publisher?: Publisher2;
@Property({ length: 255, nullable: true })
foo: string;
foo?: string;
}
",
Expand Down Expand Up @@ -126,10 +126,10 @@ export class FooBar2 {
name: string;
@OneToOne({ entity: () => FooBaz2, nullable: true })
baz: FooBaz2;
baz?: FooBaz2;
@OneToOne({ entity: () => FooBar2, nullable: true })
fooBar: FooBar2;
fooBar?: FooBar2;
@Property({ length: 3, default: \`current_timestamp(3)\` })
version: Date;
Expand Down Expand Up @@ -197,19 +197,19 @@ export class Test2 {
id: number;
@Property({ length: 255, nullable: true })
name: string;
name?: string;
@OneToOne({ entity: () => Book2, cascade: [Cascade.MERGE], nullable: true })
book: Book2;
book?: Book2;
@Property()
version: number = 1;
@OneToOne({ entity: () => FooBar2, fieldName: 'foo___bar', nullable: true })
fooBar: FooBar2;
fooBar?: FooBar2;
@Property({ fieldName: 'foo___baz', nullable: true })
fooBaz: number;
fooBaz?: number;
}
",
Expand Down Expand Up @@ -240,22 +240,22 @@ export class Author2 {
email: string;
@Property({ nullable: true })
age: number;
age?: number;
@Property()
termsAccepted: boolean = true;
@Property({ nullable: true })
identities: object;
identities?: object;
@Property({ nullable: true })
born: Date;
born?: Date;
@ManyToOne({ entity: () => Book2, nullable: true })
favouriteBook: Book2;
favouriteBook?: Book2;
@ManyToOne({ entity: () => Author2, nullable: true })
favouriteAuthor: Author2;
favouriteAuthor?: Author2;
}
",
Expand All @@ -273,28 +273,28 @@ export class Book2 {
createdAt: Date;
@Property({ length: 255, nullable: true })
title: string;
title?: string;
@Property({ type: 'text', nullable: true })
perex: string;
perex?: string;
@Property({ type: 'float8', nullable: true })
price: number;
price?: number;
@Property({ type: 'float8', nullable: true })
double: number;
double?: number;
@Property({ nullable: true })
meta: object;
meta?: object;
@ManyToOne({ entity: () => Author2, cascade: [Cascade.MERGE], nullable: true })
author: Author2;
author?: Author2;
@ManyToOne({ entity: () => Publisher2, cascade: [Cascade.ALL], nullable: true })
publisher: Publisher2;
publisher?: Publisher2;
@Property({ length: 255, nullable: true })
foo: string;
foo?: string;
}
",
Expand Down Expand Up @@ -342,10 +342,10 @@ export class FooBar2 {
name: string;
@OneToOne({ entity: () => FooBaz2, nullable: true })
baz: FooBaz2;
baz?: FooBaz2;
@OneToOne({ entity: () => FooBar2, nullable: true })
fooBar: FooBar2;
fooBar?: FooBar2;
@Property({ length: 3, default: \`current_timestamp(3)\` })
version: Date;
Expand Down Expand Up @@ -425,16 +425,16 @@ export class Test2 {
id: number;
@Property({ length: 255, nullable: true })
name: string;
name?: string;
@OneToOne({ entity: () => Book2, cascade: [Cascade.MERGE], nullable: true })
book: Book2;
book?: Book2;
@Property()
version: number = 1;
@Property({ columnType: 'polygon', nullable: true })
path: string;
path?: string;
}
",
Expand All @@ -453,10 +453,10 @@ export class Author3 {
id: number;
@Property({ nullable: true })
createdAt: Date;
createdAt?: Date;
@Property({ nullable: true })
updatedAt: Date;
updatedAt?: Date;
@Property()
name: string;
Expand All @@ -465,19 +465,19 @@ export class Author3 {
email: string;
@Property({ nullable: true })
age: number;
age?: number;
@Property()
termsAccepted: number;
@Property({ nullable: true })
identities: string;
identities?: string;
@Property({ nullable: true })
born: Date;
born?: Date;
@ManyToOne({ entity: () => Book3, nullable: true })
favouriteBook: Book3;
favouriteBook?: Book3;
}
",
Expand All @@ -495,13 +495,13 @@ export class Book3 {
title: string;
@Property({ nullable: true })
foo: string;
foo?: string;
@ManyToOne({ entity: () => Author3, nullable: true })
author: Author3;
author?: Author3;
@ManyToOne({ entity: () => Publisher3, nullable: true })
publisher: Publisher3;
publisher?: Publisher3;
}
",
Expand All @@ -516,10 +516,10 @@ export class Book3ToBookTag3 {
id: number;
@ManyToOne({ entity: () => Book3, cascade: [Cascade.ALL], nullable: true })
book3: Book3;
book3?: Book3;
@ManyToOne({ entity: () => BookTag3, cascade: [Cascade.ALL], nullable: true })
bookTag3: BookTag3;
bookTag3?: BookTag3;
}
",
Expand Down Expand Up @@ -566,10 +566,10 @@ export class Publisher3ToTest3 {
id: number;
@ManyToOne({ entity: () => Publisher3, cascade: [Cascade.ALL], nullable: true })
publisher3: Publisher3;
publisher3?: Publisher3;
@ManyToOne({ entity: () => Test3, cascade: [Cascade.ALL], nullable: true })
test3: Test3;
test3?: Test3;
}
",
Expand All @@ -582,7 +582,7 @@ export class Test3 {
id: number;
@Property({ nullable: true })
name: string;
name?: string;
@Property()
version: number = 1;
Expand Down

0 comments on commit 1cdccd3

Please sign in to comment.