Merge branch 'master' of https://github.com/Kononnable/typeorm-model-generator into relation-id-fix

# Conflicts:
#	src/entity.mst
This commit is contained in:
Vasil Rangelov 2019-08-23 12:34:48 +03:00
commit b1047c88f7
5 changed files with 35 additions and 7 deletions

View File

@ -288,6 +288,9 @@ function createHandlebarsHelpers(generationOptions: IGenerationOptions) {
Handlebars.registerHelper("tolowerCaseFirst", str =>
changeCase.lowerCaseFirst(str)
);
Handlebars.registerHelper("strictMode", () =>
generationOptions.strictMode ? generationOptions.strictMode : ""
);
Handlebars.registerHelper("toLazy", str => {
if (generationOptions.lazy) {
return `Promise<${str}>`;

View File

@ -22,5 +22,7 @@ export default class IGenerationOptions {
public relationIds: boolean = false;
public strictMode: false | "?" | "!" = false;
public skipSchema: boolean = false;
}

View File

@ -24,21 +24,23 @@ import {BaseEntity,Column,Entity,Index,JoinColumn,JoinTable,ManyToMany,ManyToOne
array:{{array}},{{/array}}
name:"{{name}}"
}){{/options}}
{{printPropertyVisibility}}{{toPropertyName tsName}}:{{tsType}}{{#options/nullable}} | null{{/options/nullable}};
{{printPropertyVisibility}}{{toPropertyName tsName}}{{strictMode}}:{{tsType}}{{#options/nullable}} | null{{/options/nullable}};
{{/relations}}{{#relations}}
@{{relationType}}(()=>{{toEntityName relatedTable}}, {{tolowerCaseFirst relatedTable}}=>{{tolowerCaseFirst relatedTable}}.{{#if isOwner}}{{toPropertyName ownerColumn}},{ {{#../options/primary}}primary:true,{{/../options/primary}}{{^../options/nullable}} nullable:false,{{/../options/nullable}}{{#actionOnDelete}}onDelete: '{{.}}',{{/actionOnDelete}}{{#actionOnUpdate}}onUpdate: '{{.}}'{{/actionOnUpdate}} }{{else}}{{toPropertyName relatedColumn}}{{#if (or actionOnDelete actionOnUpdate ) }},{ {{#actionOnDelete}}onDelete: '{{.}}' ,{{/actionOnDelete}}{{#actionOnUpdate}}onUpdate: '{{.}}'{{/actionOnUpdate}} }{{/if}}{{/if}}){{#isOwner}}
@{{relationType}}(()=>{{toEntityName relatedTable}}, ({{toPropertyName relatedTable}}: {{toEntityName relatedTable}})=>{{toPropertyName relatedTable}}.{{#if isOwner}}{{toPropertyName ownerColumn}},{ {{#../options/primary}}primary:true,{{/../options/primary}}{{^../options/nullable}} nullable:false,{{/../options/nullable}}{{#actionOnDelete}}onDelete: '{{.}}',{{/actionOnDelete}}{{#actionOnUpdate}}onUpdate: '{{.}}'{{/actionOnUpdate}} }{{else}}{{toPropertyName relatedColumn}}{{#if (or actionOnDelete actionOnUpdate ) }},{ {{#actionOnDelete}}onDelete: '{{.}}' ,{{/actionOnDelete}}{{#actionOnUpdate}}onUpdate: '{{.}}'{{/actionOnUpdate}} }{{/if}}{{/if}}){{#isOwner}}
{{#if isManyToMany}}@JoinTable({ name:'{{ ../options/name}}'}){{else}}@JoinColumn({ name:'{{ ../options/name}}'}){{/if}}{{/isOwner}}
{{#if (or isOneToMany isManyToMany)}}{{printPropertyVisibility}}{{toPropertyName ../tsName}}:{{toLazy (concat (toEntityName relatedTable) "[]")}};
{{else}}{{printPropertyVisibility}}{{toPropertyName ../tsName}}:{{toLazy (concat (toEntityName relatedTable) ' | null')}};
{{#if (or isOneToMany isManyToMany)}}{{printPropertyVisibility}}{{toPropertyName ../tsName}}{{strictMode}}:{{toLazy (concat (toEntityName relatedTable) "[]")}};
{{else}}{{printPropertyVisibility}}{{toPropertyName ../tsName}}{{strictMode}}:{{toLazy (concat (toEntityName relatedTable) ' | null')}};
{{/if}}
{{#if relationIdField }}
@RelationId(({{toPropertyName ../../tsEntityName}}: {{toEntityName ../../tsEntityName}}) => {{toPropertyName ../../tsEntityName}}.{{toPropertyName ../tsName}})
{{printPropertyVisibility}}{{toPropertyName ../tsName}}Id: {{#if isOneToOne}}{{toLazy ../tsType}}{{else}}{{toLazy (concat ../tsType "[]")}}{{/if}};{{/if}}{{/relations}}
{{printPropertyVisibility}}{{toPropertyName ../tsName}}Id{{strictMode}}: {{#if isOneToOne}}{{toLazy ../tsType}}{{else}}{{toLazy (concat ../tsType "[]")}}{{/if}};{{/if}}{{/relations}}
{{/Columns}}
{{#if GenerateConstructor}}
{{printPropertyVisibility}}constructor(init?: Partial<{{toEntityName tsEntityName}}>) {
Object.assign(this, init);
{{#IsActiveRecord}}super();
{{/IsActiveRecord}}Object.assign(this, init);
}
{{/if}}
}

View File

@ -163,6 +163,11 @@ function GetUtilParametersByArgs() {
default: false,
describe: "Generate constructor allowing partial initialization"
})
.option("strictMode", {
choices: ["none", "?", "!"],
default: "none",
describe: "Mark fields as optional(?) or non-null(!)"
})
.option("timeout", {
describe: "SQL Query timeout(ms)",
number: true
@ -201,6 +206,9 @@ function GetUtilParametersByArgs() {
generationOptions.relationIds = argv.relationIds;
generationOptions.skipSchema = argv.skipSchema;
generationOptions.resultsPath = argv.o ? argv.o.toString() : null;
generationOptions.strictMode =
argv.strictMode === "none" ? false : argv.strictMode;
return { driver, connectionOptions, generationOptions };
}
@ -399,6 +407,18 @@ async function GetUtilParametersByInquirer() {
}
])) as any).propertyVisibility;
const { strictModeRaw } = (await inquirer.prompt([
{
choices: ["none", "?", "!"],
message: "Mark fields as optional(?) or non-null(!)",
name: "strictModeRaw",
default: "none",
type: "list"
}
])) as any;
generationOptions.strictMode =
strictModeRaw === "none" ? false : strictModeRaw;
generationOptions.noConfigs = !customizations.includes("config");
generationOptions.lazy = customizations.includes("lazy");
generationOptions.activeRecord = customizations.includes(

View File

@ -24,7 +24,8 @@ export function getGenerationOptions(resultsPath: string): IGenerationOptions {
customNamingStrategyPath: "",
relationIds: false,
skipSchema: false,
activeRecord: false
activeRecord: false,
strictMode: false
};
}