Add flag to models that use the ActiveRecord syntax.

Closes #131
This commit is contained in:
Jared Malcolm 2018-12-19 10:25:49 -06:00
parent 62c9318833
commit f66ee271b5
5 changed files with 28 additions and 2 deletions

View File

@ -56,6 +56,8 @@ Options:
--cp, --case-property Convert property names to specified case
[choices: "pascal", "camel", "none"] [default: "none"]
--lazy Generate lazy relations [boolean] [default: false]
-a, --active-record Generate models that use the ActiveRecord syntax
[boolean] [default: false]
--namingStrategy Use custom naming strategy
--relationIds Generate RelationId fields [boolean] [default: false]
--generateConstructor Generate constructor allowing partial initialization

View File

@ -88,6 +88,7 @@ export class Engine {
});
});
element.GenerateConstructor = this.Options.constructor;
element.IsActiveRecord = this.Options.activeRecord;
element.Imports.filter((elem, index, self) => {
return index === self.indexOf(elem);
});
@ -274,6 +275,7 @@ export interface IEngineOptions {
convertCaseProperty: "pascal" | "camel" | "none";
propertyVisibility: "public" | "protected" | "private" | "none";
lazy: boolean;
activeRecord: boolean;
constructor: boolean;
namingStrategy: AbstractNamingStrategy;
relationIds: boolean;

View File

@ -1,11 +1,25 @@
import {Index,Entity, PrimaryColumn, PrimaryGeneratedColumn, Column, OneToOne, OneToMany, ManyToOne, ManyToMany, JoinColumn, JoinTable, RelationId} from "typeorm";
import {
BaseEntity,
Column,
Entity,
Index,
JoinColumn,
JoinTable,
ManyToMany,
ManyToOne,
OneToMany,
OneToOne,
PrimaryColumn,
PrimaryGeneratedColumn,
RelationId
} from "typeorm";
{{relationImports}}{{#each UniqueImports}}import {{curly true}}{{toEntityName this}}{{curly false}} from "./{{toFileName this}}";
{{/each}}
@Entity("{{EntityName}}"{{#Schema}},{schema:"{{.}}"}{{/Schema}})
{{#Indexes}}{{^isPrimaryKey}}@Index("{{name}}",[{{#columns}}"{{toPropertyName name}}",{{/columns}}]{{#isUnique}},{unique:true}{{/isUnique}})
{{/isPrimaryKey}}{{/Indexes}}export class {{toEntityName EntityName}} {
{{/isPrimaryKey}}{{/Indexes}}export class {{toEntityName EntityName}}{{#if IsActiveRecord}} extends BaseEntity{{/if}} {
{{#Columns}}
{{^relations}}{{#isGenerated}} @PrimaryGeneratedColumn({

View File

@ -92,6 +92,12 @@ const argv = Yargs.usage(
default: false,
describe: "Generate lazy relations"
})
.option("a", {
alias: "active-record",
boolean: true,
default: false,
describe: "Use ActiveRecord syntax for generated models"
})
.option("namingStrategy", {
describe: "Use custom naming strategy"
})
@ -164,6 +170,7 @@ const engine = new Engine(driver, {
databaseType: argv.e,
host: argv.h,
lazy: argv.lazy,
activeRecord: argv.a,
namingStrategy,
noConfigs: argv.noConfig,
password: argv.x ? argv.x.toString() : null,

View File

@ -8,6 +8,7 @@ export class EntityInfo {
public Indexes: IndexInfo[];
public Schema: string;
public GenerateConstructor: boolean;
public IsActiveRecord: boolean;
public relationImports() {
const imports: string[] = [];