add strictMode generation option
This commit is contained in:
parent
3cec1836a1
commit
14feb68cca
@ -284,6 +284,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}>`;
|
||||
|
@ -21,4 +21,6 @@ export default class IGenerationOptions {
|
||||
public customNamingStrategyPath: string = "";
|
||||
|
||||
public relationIds: boolean = false;
|
||||
|
||||
public strictMode: false | "?" | "!" = false;
|
||||
}
|
||||
|
@ -221,9 +221,7 @@ export default abstract class AbstractDriver {
|
||||
);
|
||||
if (!ownerEntity) {
|
||||
TomgUtils.LogError(
|
||||
`Relation between tables ${relationTmp.ownerTable} and ${
|
||||
relationTmp.referencedTable
|
||||
} didn't found entity model ${relationTmp.ownerTable}.`
|
||||
`Relation between tables ${relationTmp.ownerTable} and ${relationTmp.referencedTable} didn't found entity model ${relationTmp.ownerTable}.`
|
||||
);
|
||||
return;
|
||||
}
|
||||
@ -233,9 +231,7 @@ export default abstract class AbstractDriver {
|
||||
);
|
||||
if (!referencedEntity) {
|
||||
TomgUtils.LogError(
|
||||
`Relation between tables ${relationTmp.ownerTable} and ${
|
||||
relationTmp.referencedTable
|
||||
} didn't found entity model ${relationTmp.referencedTable}.`
|
||||
`Relation between tables ${relationTmp.ownerTable} and ${relationTmp.referencedTable} didn't found entity model ${relationTmp.referencedTable}.`
|
||||
);
|
||||
return;
|
||||
}
|
||||
@ -251,13 +247,7 @@ export default abstract class AbstractDriver {
|
||||
);
|
||||
if (!ownerColumn) {
|
||||
TomgUtils.LogError(
|
||||
`Relation between tables ${
|
||||
relationTmp.ownerTable
|
||||
} and ${
|
||||
relationTmp.referencedTable
|
||||
} didn't found entity column ${
|
||||
relationTmp.ownerTable
|
||||
}.${ownerColumn}.`
|
||||
`Relation between tables ${relationTmp.ownerTable} and ${relationTmp.referencedTable} didn't found entity column ${relationTmp.ownerTable}.${ownerColumn}.`
|
||||
);
|
||||
return;
|
||||
}
|
||||
@ -268,13 +258,7 @@ export default abstract class AbstractDriver {
|
||||
);
|
||||
if (!relatedColumn) {
|
||||
TomgUtils.LogError(
|
||||
`Relation between tables ${
|
||||
relationTmp.ownerTable
|
||||
} and ${
|
||||
relationTmp.referencedTable
|
||||
} didn't found entity column ${
|
||||
relationTmp.referencedTable
|
||||
}.${relatedColumn}.`
|
||||
`Relation between tables ${relationTmp.ownerTable} and ${relationTmp.referencedTable} didn't found entity column ${relationTmp.referencedTable}.${relatedColumn}.`
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
@ -191,11 +191,7 @@ WHERE TABLE_TYPE='BASE TABLE' and TABLE_SCHEMA in (${schema}) AND TABLE_CATALOG
|
||||
break;
|
||||
default:
|
||||
TomgUtils.LogError(
|
||||
`Unknown column type: ${
|
||||
resp.DATA_TYPE
|
||||
} table name: ${
|
||||
resp.TABLE_NAME
|
||||
} column name: ${resp.COLUMN_NAME}`
|
||||
`Unknown column type: ${resp.DATA_TYPE} table name: ${resp.TABLE_NAME} column name: ${resp.COLUMN_NAME}`
|
||||
);
|
||||
break;
|
||||
}
|
||||
|
@ -208,11 +208,7 @@ export default class MysqlDriver extends AbstractDriver {
|
||||
break;
|
||||
default:
|
||||
TomgUtils.LogError(
|
||||
`Unknown column type: ${
|
||||
resp.DATA_TYPE
|
||||
} table name: ${
|
||||
resp.TABLE_NAME
|
||||
} column name: ${resp.COLUMN_NAME}`
|
||||
`Unknown column type: ${resp.DATA_TYPE} table name: ${resp.TABLE_NAME} column name: ${resp.COLUMN_NAME}`
|
||||
);
|
||||
break;
|
||||
}
|
||||
|
@ -309,9 +309,7 @@ export default class OracleDriver extends AbstractDriver {
|
||||
let config: any;
|
||||
if (connectionOptons.user === String(process.env.ORACLE_UsernameSys)) {
|
||||
config /* Oracle.IConnectionAttributes */ = {
|
||||
connectString: `${connectionOptons.host}:${
|
||||
connectionOptons.port
|
||||
}/${connectionOptons.databaseName}`,
|
||||
connectString: `${connectionOptons.host}:${connectionOptons.port}/${connectionOptons.databaseName}`,
|
||||
externalAuth: connectionOptons.ssl,
|
||||
password: connectionOptons.password,
|
||||
privilege: this.Oracle.SYSDBA,
|
||||
@ -319,9 +317,7 @@ export default class OracleDriver extends AbstractDriver {
|
||||
};
|
||||
} else {
|
||||
config /* Oracle.IConnectionAttributes */ = {
|
||||
connectString: `${connectionOptons.host}:${
|
||||
connectionOptons.port
|
||||
}/${connectionOptons.databaseName}`,
|
||||
connectString: `${connectionOptons.host}:${connectionOptons.port}/${connectionOptons.databaseName}`,
|
||||
externalAuth: connectionOptons.ssl,
|
||||
password: connectionOptons.password,
|
||||
user: connectionOptons.user
|
||||
|
@ -102,19 +102,11 @@ WHERE "n"."nspname" = table_schema AND "t"."typname"=udt_name
|
||||
resp.data_type === "ARRAY"
|
||||
) {
|
||||
TomgUtils.LogError(
|
||||
`Unknown ${resp.data_type} column type: ${
|
||||
resp.udt_name
|
||||
} table name: ${
|
||||
resp.table_name
|
||||
} column name: ${resp.column_name}`
|
||||
`Unknown ${resp.data_type} column type: ${resp.udt_name} table name: ${resp.table_name} column name: ${resp.column_name}`
|
||||
);
|
||||
} else {
|
||||
TomgUtils.LogError(
|
||||
`Unknown column type: ${
|
||||
resp.data_type
|
||||
} table name: ${
|
||||
resp.table_name
|
||||
} column name: ${resp.column_name}`
|
||||
`Unknown column type: ${resp.data_type} table name: ${resp.table_name} column name: ${resp.column_name}`
|
||||
);
|
||||
}
|
||||
return;
|
||||
|
@ -163,11 +163,7 @@ export default class SqliteDriver extends AbstractDriver {
|
||||
break;
|
||||
default:
|
||||
TomgUtils.LogError(
|
||||
`Unknown column type: ${
|
||||
colInfo.options.type
|
||||
} table name: ${
|
||||
ent.tsEntityName
|
||||
} column name: ${resp.name}`
|
||||
`Unknown column type: ${colInfo.options.type} table name: ${ent.tsEntityName} column name: ${resp.name}`
|
||||
);
|
||||
break;
|
||||
}
|
||||
|
@ -24,17 +24,17 @@ 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}}, ({{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}}>) {
|
||||
|
19
src/index.ts
19
src/index.ts
@ -158,6 +158,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
|
||||
@ -195,6 +200,8 @@ function GetUtilParametersByArgs() {
|
||||
generationOptions.propertyVisibility = argv.pv;
|
||||
generationOptions.relationIds = argv.relationIds;
|
||||
generationOptions.resultsPath = argv.o ? argv.o.toString() : null;
|
||||
generationOptions.strictMode =
|
||||
argv.strictMode === "none" ? false : argv.strictMode;
|
||||
|
||||
return { driver, connectionOptions, generationOptions };
|
||||
}
|
||||
@ -390,6 +397,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(
|
||||
|
Loading…
Reference in New Issue
Block a user