diff --git a/README.md b/README.md index 3ccb0bc..a57a805 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/Engine.ts b/src/Engine.ts index 58a8d52..a0a9aae 100644 --- a/src/Engine.ts +++ b/src/Engine.ts @@ -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; diff --git a/src/entity.mst b/src/entity.mst index 0dd8ee7..eb5388b 100644 --- a/src/entity.mst +++ b/src/entity.mst @@ -1,11 +1,11 @@ -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}}{{#IsActiveRecord}} extends BaseEntity{{/IsActiveRecord}} { {{#Columns}} {{^relations}}{{#isGenerated}} @PrimaryGeneratedColumn({ diff --git a/src/index.ts b/src/index.ts index 6e36e54..8a4e903 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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, diff --git a/src/models/EntityInfo.ts b/src/models/EntityInfo.ts index 82d90ed..30eec70 100644 --- a/src/models/EntityInfo.ts +++ b/src/models/EntityInfo.ts @@ -8,6 +8,7 @@ export class EntityInfo { public Indexes: IndexInfo[]; public Schema: string; public GenerateConstructor: boolean; + public IsActiveRecord: boolean; public relationImports() { const imports: string[] = []; diff --git a/test/utils/GeneralTestUtils.ts b/test/utils/GeneralTestUtils.ts index e3c3c47..596e9e1 100644 --- a/test/utils/GeneralTestUtils.ts +++ b/test/utils/GeneralTestUtils.ts @@ -71,7 +71,8 @@ export async function createMSSQLModels(filesOrgPath: string, resultsPath: strin lazy: false, constructor: false, namingStrategy, - relationIds: false + relationIds: false, + activeRecord: false }); conn = await createConnection(connOpt) @@ -143,7 +144,8 @@ export async function createPostgresModels(filesOrgPath: string, resultsPath: st lazy: false, constructor: false, namingStrategy, - relationIds: false + relationIds: false, + activeRecord: false }); conn = await createConnection(connOpt) @@ -207,7 +209,8 @@ export async function createSQLiteModels(filesOrgPath: string, resultsPath: stri lazy: false, constructor: false, namingStrategy, - relationIds: false + relationIds: false, + activeRecord: false }); conn = await createConnection(connOpt) @@ -269,7 +272,8 @@ export async function createMysqlModels(filesOrgPath: string, resultsPath: strin lazy: false, constructor: false, namingStrategy, - relationIds: false + relationIds: false, + activeRecord: false }); return engine; @@ -323,7 +327,8 @@ export async function createMariaDBModels(filesOrgPath: string, resultsPath: str lazy: false, constructor: false, namingStrategy, - relationIds: false + relationIds: false, + activeRecord: false }); @@ -380,7 +385,8 @@ export async function createOracleDBModels(filesOrgPath: string, resultsPath: st lazy: false, constructor: false, namingStrategy, - relationIds: false + relationIds: false, + activeRecord: false }); return engine;