From 2a7c1453cce4172f79ac918058c65561d003dc5b Mon Sep 17 00:00:00 2001 From: Kononnable Date: Sat, 5 May 2018 21:14:06 +0200 Subject: [PATCH] Add option to include partial constructor #67 --- src/Engine.ts | 2 ++ src/entity.mst | 5 +++++ src/index.ts | 8 +++++++- src/models/EntityInfo.ts | 1 + test/utils/EntityFileToJson.ts | 13 +++++++++++-- test/utils/GeneralTestUtils.ts | 18 ++++++++++++------ 6 files changed, 38 insertions(+), 9 deletions(-) diff --git a/src/Engine.ts b/src/Engine.ts index d06a046..df5b3d2 100644 --- a/src/Engine.ts +++ b/src/Engine.ts @@ -76,6 +76,7 @@ export class Engine { } }); }); + element.GenerateConstructor = this.Options.constructor; element.Imports.filter(function(elem, index, self) { return index === self.indexOf(elem); }); @@ -271,4 +272,5 @@ export interface EngineOptions { convertCaseEntity: "pascal" | "camel" | "none"; convertCaseProperty: "pascal" | "camel" | "none"; lazy: boolean; + constructor: boolean; } diff --git a/src/entity.mst b/src/entity.mst index c2f712e..86d5279 100644 --- a/src/entity.mst +++ b/src/entity.mst @@ -30,4 +30,9 @@ import {Index,Entity, PrimaryColumn, Column, OneToOne, OneToMany, ManyToOne, Man {{else}}{{toPropertyName ../name}}:{{toLazy (toEntityName relatedTable)}}; {{/if}}{{/relations}} {{/Columns}} + {{#if GenerateConstructor}} + constructor(init?: Partial<{{toEntityName EntityName}}>) { + Object.assign(this, init); + } + {{/if}} } diff --git a/src/index.ts b/src/index.ts index 7eb176d..8e79a44 100644 --- a/src/index.ts +++ b/src/index.ts @@ -84,6 +84,11 @@ var argv = Yargs.usage( describe: "Generate lazy relations", boolean: true, default: false + }) + .option("generateConstructor", { + describe: "Generate constructor allowing partial initialization", + boolean: true, + default: false }).argv; var driver: AbstractDriver; @@ -135,7 +140,8 @@ let engine = new Engine(driver, { convertCaseFile: argv.cf, convertCaseEntity: argv.ce, convertCaseProperty: argv.cp, - lazy: argv.ssl + lazy: argv.lazy, + constructor: argv.constructor }); console.log( diff --git a/src/models/EntityInfo.ts b/src/models/EntityInfo.ts index ad016f7..8e511f4 100644 --- a/src/models/EntityInfo.ts +++ b/src/models/EntityInfo.ts @@ -11,6 +11,7 @@ export class EntityInfo { UniqueImports: string[]; Indexes: IndexInfo[]; Schema: string; + GenerateConstructor: boolean; relationImports(): any { var returnString = ""; diff --git a/test/utils/EntityFileToJson.ts b/test/utils/EntityFileToJson.ts index 1f17050..15da64e 100644 --- a/test/utils/EntityFileToJson.ts +++ b/test/utils/EntityFileToJson.ts @@ -298,6 +298,15 @@ export class EntityFileToJson { retVal.indicies.push(ind); continue; } + } else if (trimmedLine.startsWith('constructor')) { + if (this.isPartOfMultilineStatement(trimmedLine)) { + isMultilineStatement = true; + priorPartOfMultilineStatement = trimmedLine; + continue; + } else { + isMultilineStatement = false; + continue; + } } else if (trimmedLine.split(':').length - 1 > 0) { retVal.columns[retVal.columns.length - 1].columnName = trimmedLine.split(':')[0].trim(); //TODO:Should check if null only column is nullable? @@ -354,8 +363,8 @@ export class EntityFileToJson { return retVal; } isPartOfMultilineStatement(statement: string) { - let matchStarting = statement.split('(').length - 1 - let matchEnding = statement.split(')').length - 1 + let matchStarting = statement.split('(').length+statement.split('{').length + let matchEnding = statement.split(')').length+statement.split('}').length return !(matchStarting == matchEnding) } diff --git a/test/utils/GeneralTestUtils.ts b/test/utils/GeneralTestUtils.ts index 1d19a35..83012ad 100644 --- a/test/utils/GeneralTestUtils.ts +++ b/test/utils/GeneralTestUtils.ts @@ -64,7 +64,8 @@ export async function createMSSQLModels(filesOrgPath: string, resultsPath: strin convertCaseEntity: 'none', convertCaseFile: 'none', convertCaseProperty: 'none', - lazy: false + lazy: false, + constructor:false }); conn = await createConnection(connOpt) @@ -128,7 +129,8 @@ export async function createPostgresModels(filesOrgPath: string, resultsPath: st convertCaseEntity: 'none', convertCaseFile: 'none', convertCaseProperty: 'none', - lazy: false + lazy: false, + constructor:false }); conn = await createConnection(connOpt) @@ -184,7 +186,8 @@ export async function createSQLiteModels(filesOrgPath: string, resultsPath: stri convertCaseEntity: 'none', convertCaseFile: 'none', convertCaseProperty: 'none', - lazy: false + lazy: false, + constructor:false }); conn = await createConnection(connOpt) @@ -238,7 +241,8 @@ export async function createMysqlModels(filesOrgPath: string, resultsPath: strin convertCaseEntity: 'none', convertCaseFile: 'none', convertCaseProperty: 'none', - lazy: false + lazy: false, + constructor:false }); @@ -288,7 +292,8 @@ export async function createMariaDBModels(filesOrgPath: string, resultsPath: str convertCaseEntity: 'none', convertCaseFile: 'none', convertCaseProperty: 'none', - lazy: false + lazy: false, + constructor:false }); @@ -340,7 +345,8 @@ export async function createOracleDBModels(filesOrgPath: string, resultsPath: st convertCaseEntity: 'none', convertCaseFile: 'none', convertCaseProperty: 'none', - lazy: false + lazy: false, + constructor:false }); return engine;