diff --git a/src/Engine.ts b/src/Engine.ts index af444af..687d26b 100644 --- a/src/Engine.ts +++ b/src/Engine.ts @@ -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}>`; diff --git a/src/IGenerationOptions.ts b/src/IGenerationOptions.ts index 94f8750..9b64956 100644 --- a/src/IGenerationOptions.ts +++ b/src/IGenerationOptions.ts @@ -22,5 +22,7 @@ export default class IGenerationOptions { public relationIds: boolean = false; + public strictMode: false | "?" | "!" = false; + public skipSchema: boolean = false; } diff --git a/src/entity.mst b/src/entity.mst index c474516..04c5aa8 100644 --- a/src/entity.mst +++ b/src/entity.mst @@ -24,21 +24,22 @@ 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}}) => {{toEntityName ../../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}} } diff --git a/src/index.ts b/src/index.ts index 85a8758..ada1ac9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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( diff --git a/test/utils/GeneralTestUtils.ts b/test/utils/GeneralTestUtils.ts index c3981d9..8c4af84 100644 --- a/test/utils/GeneralTestUtils.ts +++ b/test/utils/GeneralTestUtils.ts @@ -24,7 +24,8 @@ export function getGenerationOptions(resultsPath: string): IGenerationOptions { customNamingStrategyPath: "", relationIds: false, skipSchema: false, - activeRecord: false + activeRecord: false, + strictMode: false }; }