Add option to include partial constructor #67

This commit is contained in:
Kononnable 2018-05-05 21:14:06 +02:00
parent fa1ceab1d1
commit 2a7c1453cc
6 changed files with 38 additions and 9 deletions

View File

@ -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;
}

View File

@ -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}}
}

View File

@ -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(

View File

@ -11,6 +11,7 @@ export class EntityInfo {
UniqueImports: string[];
Indexes: IndexInfo[];
Schema: string;
GenerateConstructor: boolean;
relationImports(): any {
var returnString = "";

View File

@ -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)
}

View File

@ -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;