commit
3298388a4e
@ -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}>`;
|
||||
|
@ -22,5 +22,7 @@ export default class IGenerationOptions {
|
||||
|
||||
public relationIds: boolean = false;
|
||||
|
||||
public strictMode: false | "?" | "!" = false;
|
||||
|
||||
public skipSchema: boolean = false;
|
||||
}
|
||||
|
@ -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}}
|
||||
}
|
||||
|
20
src/index.ts
20
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(
|
||||
|
@ -24,7 +24,8 @@ export function getGenerationOptions(resultsPath: string): IGenerationOptions {
|
||||
customNamingStrategyPath: "",
|
||||
relationIds: false,
|
||||
skipSchema: false,
|
||||
activeRecord: false
|
||||
activeRecord: false,
|
||||
strictMode: false
|
||||
};
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user