code cleanup

This commit is contained in:
Kononnable 2019-10-12 11:02:46 +02:00
parent 15b6291606
commit 097ce29951
7 changed files with 20 additions and 94 deletions

View File

@ -273,7 +273,6 @@ function createHandlebarsHelpers(generationOptions: IGenerationOptions) {
const withoutQuotes = json.replace(/"([^(")"]+)":/g, "$1:");
return withoutQuotes.slice(1, withoutQuotes.length - 1);
});
Handlebars.registerHelper("curly", open => (open ? "{" : "}"));
Handlebars.registerHelper("toEntityName", str => {
let retStr = "";
switch (generationOptions.convertCaseEntity) {
@ -291,25 +290,6 @@ function createHandlebarsHelpers(generationOptions: IGenerationOptions) {
}
return retStr;
});
Handlebars.registerHelper("concat", (stra: string, strb: string) => {
return stra + strb;
});
Handlebars.registerHelper("contains", function contains(
searchTerm: string,
target: string,
options
) {
return target.indexOf(searchTerm) > -1
? options.fn(this)
: options.inverse(this);
});
Handlebars.registerHelper("toFileImports", (set: Set<any>) => {
return [...set].reduce(
(pv, cv) =>
`${pv}import { {{toEntityName ${cv}}} } from './{{toFileName ${cv}}};`,
""
);
});
Handlebars.registerHelper("toFileName", str => {
let retStr = "";
switch (generationOptions.convertCaseFile) {
@ -331,6 +311,7 @@ function createHandlebarsHelpers(generationOptions: IGenerationOptions) {
return retStr;
});
Handlebars.registerHelper("printPropertyVisibility", () =>
// TODO:
generationOptions.propertyVisibility !== "none"
? `${generationOptions.propertyVisibility} `
: ""
@ -352,7 +333,6 @@ function createHandlebarsHelpers(generationOptions: IGenerationOptions) {
}
return retStr;
});
Handlebars.registerHelper("toLowerCase", str => str.toLowerCase());
Handlebars.registerHelper(
"toRelation",
(entityType: string, relationType: Relation["relationType"]) => {
@ -366,18 +346,10 @@ function createHandlebarsHelpers(generationOptions: IGenerationOptions) {
return retVal;
}
);
Handlebars.registerHelper("tolowerCaseFirst", str =>
changeCase.lowerCaseFirst(str)
);
Handlebars.registerHelper("strictMode", () =>
// TODO:
generationOptions.strictMode ? generationOptions.strictMode : ""
);
Handlebars.registerHelper("toLazy", str => {
if (generationOptions.lazy) {
return `Promise<${str}>`;
}
return str;
});
Handlebars.registerHelper({
and: (v1, v2) => v1 && v2,
eq: (v1, v2) => v1 === v2,

View File

@ -80,8 +80,9 @@ WHERE TABLE_TYPE='BASE TABLE' and TABLE_SCHEMA in (${schema}) AND TABLE_CATALOG
})
.forEach(resp => {
const tscName = resp.COLUMN_NAME;
const options: Partial<Column["options"]> = {};
options.name = resp.COLUMN_NAME;
const options: Column["options"] = {
name: resp.COLUMN_NAME
};
if (resp.IS_NULLABLE === "YES") options.nullable = true;
if (resp.IsUnique === 1) options.unique = true;
const generated = resp.IsIdentity === 1 ? true : undefined;
@ -223,7 +224,7 @@ WHERE TABLE_TYPE='BASE TABLE' and TABLE_SCHEMA in (${schema}) AND TABLE_CATALOG
generated,
type: columnType,
default: defaultValue,
options: { name: "", ...options }, // TODO: Change
options,
tscName,
tscType
});

View File

@ -70,8 +70,9 @@ export default class MysqlDriver extends AbstractDriver {
.forEach(resp => {
const tscName = resp.COLUMN_NAME;
let tscType = "";
const options: Partial<Column["options"]> = {};
options.name = resp.COLUMN_NAME;
const options: Column["options"] = {
name: resp.COLUMN_NAME
};
const generated = resp.IsIdentity === 1 ? true : undefined;
const defaultValue = MysqlDriver.ReturnDefaultValueFunction(
resp.COLUMN_DEFAULT
@ -254,7 +255,7 @@ export default class MysqlDriver extends AbstractDriver {
generated,
type: columnType,
default: defaultValue,
options: { name: "", ...options }, // TODO: Change
options,
tscName,
tscType
});

View File

@ -72,8 +72,9 @@ export default class OracleDriver extends AbstractDriver {
.filter(filterVal => filterVal.TABLE_NAME === ent.tscName)
.forEach(resp => {
const tscName = resp.COLUMN_NAME;
const options: Partial<Column["options"]> = {};
options.name = resp.COLUMN_NAME;
const options: Column["options"] = {
name: resp.COLUMN_NAME
};
if (resp.NULLABLE === "Y") options.nullable = true;
if (resp.IS_UNIQUE > 0) options.unique = true;
const generated =
@ -205,7 +206,7 @@ export default class OracleDriver extends AbstractDriver {
generated,
type: columnType,
default: defaultValue,
options: { name: "", ...options }, // TODO: Change
options,
tscName,
tscType
});

View File

@ -80,8 +80,9 @@ export default class PostgresDriver extends AbstractDriver {
.filter(filterVal => filterVal.table_name === ent.tscName)
.forEach(resp => {
const tscName = resp.column_name;
const options: Partial<Column["options"]> = {};
options.name = resp.column_name;
const options: Column["options"] = {
name: resp.column_name
};
if (resp.is_nullable === "YES") options.nullable = true;
if (resp.isunique === "1") options.unique = true;
@ -156,7 +157,7 @@ export default class PostgresDriver extends AbstractDriver {
generated,
type: columnType,
default: defaultValue,
options: { name: "", ...options }, // TODO: Change
options,
tscName,
tscType
});

View File

@ -66,8 +66,7 @@ export default class SqliteDriver extends AbstractDriver {
response.forEach(resp => {
const tscName = resp.name;
let tscType = "";
const options: Partial<Column["options"]> = {};
options.name = resp.name;
const options: Column["options"] = { name: resp.name };
if (resp.notnull === 0) options.nullable = true;
const isPrimary = resp.pk > 0 ? true : undefined;
const defaultValue = SqliteDriver.ReturnDefaultValueFunction(
@ -216,7 +215,7 @@ export default class SqliteDriver extends AbstractDriver {
primary: isPrimary,
type: columnType,
default: defaultValue,
options: { name: "", ...options }, // TODO: Change
options,
tscName,
tscType
});

View File

@ -36,52 +36,3 @@ import {BaseEntity,Column,Entity,Index,JoinColumn,JoinTable,ManyToMany,ManyToOne
{{#fileImports}}{{> Import}}{{/fileImports~}}
{{~> Entity}}
{{!--
{{relationImports}}{{#each UniqueImports}}import {{curly true}}{{toEntityName this}}{{curly false}} from "./{{toFileName this}}";
{{/each}}
@Entity("{{sqlEntityName}}"{{#Schema}} ,{schema:"{{.}}"{{#if ../Database}}, database:"{{../Database}}"{{/if}} } {{/Schema}})
{{#Indexes}}{{^isPrimaryKey}}@Index("{{name}}",[{{#columns}}"{{toPropertyName name}}",{{/columns}}]{{#isUnique}},{unique:true}{{/isUnique}})
{{/isPrimaryKey}}{{/Indexes}}export class {{toEntityName tsEntityName}}{{#IsActiveRecord}} extends BaseEntity{{/IsActiveRecord}} {
{{#Columns}}
{{^relations}}{{#options}}{{#generated}} @PrimaryGeneratedColumn({
type:"{{type}}", {{/generated}}{{^generated}} @Column("{{type}}",{ {{#nullable}}
nullable:true,{{/nullable}}{{^nullable}}
nullable:false,{{/nullable}}{{#primary}}
primary:{{primary}},{{/primary}}{{/generated}}{{#unique}}
unique: true,{{/unique}}{{#length}}
length:{{.}},{{/length}}{{#width}}
width:{{.}},{{/width}}{{#unsigned}}
unsigned: true,{{/unsigned}}{{#default}}
default: {{.}},{{/default}}{{#precision}}
precision:{{.}},{{/precision}}{{#scale}}
scale:{{.}},{{/scale}}{{#enum}}
enum:[{{.}}],{{/enum}}{{#array}}
array:{{array}},{{/array}}
name:"{{name}}"
}){{/options}}
{{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}}{{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{{strictMode}}: {{#if isOneToOne}}{{toLazy ../tsType}}{{else}}{{toLazy (concat ../tsType "[]")}}{{/if}};{{/if}}{{/relations}}
{{/Columns}}
{{#if GenerateConstructor}}
{{printPropertyVisibility}}constructor(init?: Partial<{{toEntityName tsEntityName}}>) {
{{#IsActiveRecord}}super();
{{/IsActiveRecord}}Object.assign(this, init);
}
{{/if}}
}
--}}