fixed naming strategy changing entity name in db

This commit is contained in:
Kononnable 2019-01-06 15:37:52 +01:00
parent 31ff9c8bf5
commit 270461a193
11 changed files with 54 additions and 48 deletions

View File

@ -82,7 +82,7 @@ export class Engine {
element.Imports = [];
element.Columns.forEach(column => {
column.relations.forEach(relation => {
if (element.EntityName !== relation.relatedTable) {
if (element.tsEntityName !== relation.relatedTable) {
element.Imports.push(relation.relatedTable);
}
});
@ -95,16 +95,16 @@ export class Engine {
let casedFileName = "";
switch (this.Options.convertCaseFile) {
case "camel":
casedFileName = changeCase.camelCase(element.EntityName);
casedFileName = changeCase.camelCase(element.tsEntityName);
break;
case "param":
casedFileName = changeCase.paramCase(element.EntityName);
casedFileName = changeCase.paramCase(element.tsEntityName);
break;
case "pascal":
casedFileName = changeCase.pascalCase(element.EntityName);
casedFileName = changeCase.pascalCase(element.tsEntityName);
break;
case "none":
casedFileName = element.EntityName;
casedFileName = element.tsEntityName;
break;
}
const resultFilePath = path.resolve(

View File

@ -10,7 +10,7 @@ export class NamingStrategy extends AbstractNamingStrategy {
): string {
const isRelationToMany = relation.isOneToMany || relation.isManyToMany;
const ownerEntity = dbModel.entities.find(
v => v.EntityName === relation.ownerTable
v => v.tsEntityName === relation.ownerTable
)!;
let columnName =

View File

@ -79,7 +79,7 @@ export abstract class AbstractDriver {
.filter(
relation =>
relation.relatedTable ===
entity.EntityName &&
entity.tsEntityName &&
relation.relatedColumn === column.tsName
)
.map(v => (v.relatedColumn = newName));
@ -87,7 +87,7 @@ export abstract class AbstractDriver {
.filter(
relation =>
relation.relatedTable ===
entity.EntityName &&
entity.tsEntityName &&
relation.ownerColumn === column.tsName
)
.map(v => (v.ownerColumn = newName));
@ -100,20 +100,20 @@ export abstract class AbstractDriver {
}
public changeEntityNames(dbModel: DatabaseModel) {
dbModel.entities.forEach(entity => {
const newName = this.namingStrategy.entityName(entity.EntityName);
const newName = this.namingStrategy.entityName(entity.tsEntityName);
dbModel.entities.forEach(entity2 => {
entity2.Columns.forEach(column => {
column.relations.forEach(relation => {
if (relation.ownerTable === entity.EntityName) {
if (relation.ownerTable === entity.tsEntityName) {
relation.ownerTable = newName;
}
if (relation.relatedTable === entity.EntityName) {
if (relation.relatedTable === entity.tsEntityName) {
relation.relatedTable = newName;
}
});
});
});
entity.EntityName = newName;
entity.tsEntityName = newName;
});
}
public changeRelationNames(dbModel: DatabaseModel) {
@ -130,14 +130,14 @@ export abstract class AbstractDriver {
column2.relations.forEach(relation2 => {
if (
relation2.relatedTable ===
entity.EntityName &&
entity.tsEntityName &&
relation2.ownerColumn === column.tsName
) {
relation2.ownerColumn = newName;
}
if (
relation2.relatedTable ===
entity.EntityName &&
entity.tsEntityName &&
relation2.relatedColumn === column.tsName
) {
relation2.relatedColumn = newName;
@ -185,25 +185,25 @@ export abstract class AbstractDriver {
.filter((v, i, s) => s.indexOf(v) === i);
if (namesOfRelatedTables.length === 2) {
const relatedTable1 = dbModel.entities.find(
v => v.EntityName === namesOfRelatedTables[0]
v => v.tsEntityName === namesOfRelatedTables[0]
)!;
relatedTable1.Columns = relatedTable1.Columns.filter(
v =>
!v.tsName
.toLowerCase()
.startsWith(entity.EntityName.toLowerCase())
.startsWith(entity.tsEntityName.toLowerCase())
);
const relatedTable2 = dbModel.entities.find(
v => v.EntityName === namesOfRelatedTables[1]
v => v.tsEntityName === namesOfRelatedTables[1]
)!;
relatedTable2.Columns = relatedTable2.Columns.filter(
v =>
!v.tsName
.toLowerCase()
.startsWith(entity.EntityName.toLowerCase())
.startsWith(entity.tsEntityName.toLowerCase())
);
dbModel.entities = dbModel.entities.filter(ent => {
return ent.EntityName !== entity.EntityName;
return ent.tsEntityName !== entity.tsEntityName;
});
const column1 = new ColumnInfo();
@ -278,7 +278,8 @@ export abstract class AbstractDriver {
const ret: EntityInfo[] = [] as EntityInfo[];
response.forEach(val => {
const ent: EntityInfo = new EntityInfo();
ent.EntityName = val.TABLE_NAME;
ent.tsEntityName = val.TABLE_NAME;
ent.sqlEntityName = val.TABLE_NAME;
ent.Schema = val.TABLE_SCHEMA;
ent.Columns = [] as ColumnInfo[];
ent.Indexes = [] as IndexInfo[];
@ -293,7 +294,7 @@ export abstract class AbstractDriver {
) {
relationsTemp.forEach(relationTmp => {
const ownerEntity = entities.find(
entitity => entitity.EntityName === relationTmp.ownerTable
entitity => entitity.tsEntityName === relationTmp.ownerTable
);
if (!ownerEntity) {
TomgUtils.LogError(
@ -304,7 +305,8 @@ export abstract class AbstractDriver {
return;
}
const referencedEntity = entities.find(
entitity => entitity.EntityName === relationTmp.referencedTable
entitity =>
entitity.tsEntityName === relationTmp.referencedTable
);
if (!referencedEntity) {
TomgUtils.LogError(
@ -375,7 +377,7 @@ export abstract class AbstractDriver {
: "OneToOne";
ownerRelation.relationIdField = this.generateRelationsIds;
let columnName = ownerEntity.EntityName;
let columnName = ownerEntity.tsEntityName;
if (
referencedEntity.Columns.some(v => v.tsName === columnName)
) {
@ -464,7 +466,7 @@ export abstract class AbstractDriver {
})
) {
TomgUtils.LogError(
`Table ${entity.EntityName} has no PK.`,
`Table ${entity.tsEntityName} has no PK.`,
false
);
return;

View File

@ -50,7 +50,7 @@ export class MssqlDriver extends AbstractDriver {
entities.forEach(ent => {
response
.filter(filterVal => {
return filterVal.TABLE_NAME === ent.EntityName;
return filterVal.TABLE_NAME === ent.tsEntityName;
})
.forEach(resp => {
const colInfo: ColumnInfo = new ColumnInfo();
@ -233,7 +233,7 @@ ORDER BY
t.name, ind.name, ind.index_id, ic.key_ordinal;`)).recordset;
entities.forEach(ent => {
response
.filter(filterVal => filterVal.TableName === ent.EntityName)
.filter(filterVal => filterVal.TableName === ent.tsEntityName)
.forEach(resp => {
let indexInfo: IndexInfo = {} as IndexInfo;
const indexColumnInfo: IndexColumnInfo = {} as IndexColumnInfo;

View File

@ -42,7 +42,7 @@ export class MysqlDriver extends AbstractDriver {
FROM INFORMATION_SCHEMA.COLUMNS where TABLE_SCHEMA like DATABASE()`);
entities.forEach(ent => {
response
.filter(filterVal => filterVal.TABLE_NAME === ent.EntityName)
.filter(filterVal => filterVal.TABLE_NAME === ent.tsEntityName)
.forEach(resp => {
const colInfo: ColumnInfo = new ColumnInfo();
colInfo.tsName = resp.COLUMN_NAME;
@ -230,7 +230,7 @@ export class MysqlDriver extends AbstractDriver {
`);
entities.forEach(ent => {
response
.filter(filterVal => filterVal.TableName === ent.EntityName)
.filter(filterVal => filterVal.TableName === ent.tsEntityName)
.forEach(resp => {
let indexInfo: IndexInfo = {} as IndexInfo;
const indexColumnInfo: IndexColumnInfo = {} as IndexColumnInfo;

View File

@ -53,7 +53,7 @@ export class OracleDriver extends AbstractDriver {
entities.forEach(ent => {
response
.filter(filterVal => filterVal.TABLE_NAME === ent.EntityName)
.filter(filterVal => filterVal.TABLE_NAME === ent.tsEntityName)
.forEach(resp => {
const colInfo: ColumnInfo = new ColumnInfo();
colInfo.tsName = resp.COLUMN_NAME;
@ -206,7 +206,7 @@ export class OracleDriver extends AbstractDriver {
entities.forEach(ent => {
response
.filter(filterVal => filterVal.TABLE_NAME === ent.EntityName)
.filter(filterVal => filterVal.TABLE_NAME === ent.tsEntityName)
.forEach(resp => {
let indexInfo: IndexInfo = {} as IndexInfo;
const indexColumnInfo: IndexColumnInfo = {} as IndexColumnInfo;

View File

@ -50,7 +50,7 @@ export class PostgresDriver extends AbstractDriver {
.rows;
entities.forEach(ent => {
response
.filter(filterVal => filterVal.table_name === ent.EntityName)
.filter(filterVal => filterVal.table_name === ent.tsEntityName)
.forEach(resp => {
const colInfo: ColumnInfo = new ColumnInfo();
colInfo.tsName = resp.column_name;
@ -400,7 +400,7 @@ export class PostgresDriver extends AbstractDriver {
ORDER BY c.relname,f.attname;`)).rows;
entities.forEach(ent => {
response
.filter(filterVal => filterVal.tablename === ent.EntityName)
.filter(filterVal => filterVal.tablename === ent.tsEntityName)
.forEach(resp => {
let indexInfo: IndexInfo = {} as IndexInfo;
const indexColumnInfo: IndexColumnInfo = {} as IndexColumnInfo;

View File

@ -16,11 +16,11 @@ export class SqliteDriver extends AbstractDriver {
);
rows.forEach(val => {
const ent: EntityInfo = new EntityInfo();
ent.EntityName = val.tbl_name;
ent.tsEntityName = val.tbl_name;
ent.Columns = [] as ColumnInfo[];
ent.Indexes = [] as IndexInfo[];
if (val.sql.includes("AUTOINCREMENT")) {
this.tablesWithGeneratedPrimaryKey.push(ent.EntityName);
this.tablesWithGeneratedPrimaryKey.push(ent.tsEntityName);
}
ret.push(ent);
});
@ -38,7 +38,7 @@ export class SqliteDriver extends AbstractDriver {
notnull: number;
dflt_value: string;
pk: number;
}>(`PRAGMA table_info('${ent.EntityName}');`);
}>(`PRAGMA table_info('${ent.tsEntityName}');`);
response.forEach(resp => {
const colInfo: ColumnInfo = new ColumnInfo();
colInfo.tsName = resp.name;
@ -54,7 +54,9 @@ export class SqliteDriver extends AbstractDriver {
.trim();
colInfo.isGenerated =
colInfo.isPrimary &&
this.tablesWithGeneratedPrimaryKey.includes(ent.EntityName);
this.tablesWithGeneratedPrimaryKey.includes(
ent.tsEntityName
);
switch (colInfo.sqlType) {
case "int":
colInfo.tsType = "number";
@ -141,7 +143,7 @@ export class SqliteDriver extends AbstractDriver {
TomgUtils.LogError(
`Unknown column type: ${
colInfo.sqlType
} table name: ${ent.EntityName} column name: ${
} table name: ${ent.tsEntityName} column name: ${
resp.name
}`
);
@ -205,7 +207,7 @@ export class SqliteDriver extends AbstractDriver {
unique: number;
origin: string;
partial: number;
}>(`PRAGMA index_list('${ent.EntityName}');`);
}>(`PRAGMA index_list('${ent.tsEntityName}');`);
for (const resp of response) {
const indexColumnsResponse = await this.ExecQuery<{
seqno: number;
@ -259,7 +261,7 @@ export class SqliteDriver extends AbstractDriver {
on_update: "RESTRICT" | "CASCADE" | "SET NULL" | "NO ACTION";
on_delete: "RESTRICT" | "CASCADE" | "SET NULL" | "NO ACTION";
match: string;
}>(`PRAGMA foreign_key_list('${entity.EntityName}');`);
}>(`PRAGMA foreign_key_list('${entity.tsEntityName}');`);
const relationsTemp: IRelationTempInfo[] = [] as IRelationTempInfo[];
response.forEach(resp => {
const rels = {} as IRelationTempInfo;
@ -269,7 +271,7 @@ export class SqliteDriver extends AbstractDriver {
resp.on_delete === "NO ACTION" ? null : resp.on_delete;
rels.actionOnUpdate =
resp.on_update === "NO ACTION" ? null : resp.on_update;
rels.ownerTable = entity.EntityName;
rels.ownerTable = entity.tsEntityName;
rels.referencedTable = resp.table;
relationsTemp.push(rels);
rels.ownerColumnsNames.push(resp.from);

View File

@ -3,9 +3,9 @@ import {BaseEntity,Column,Entity,Index,JoinColumn,JoinTable,ManyToMany,ManyToOne
{{/each}}
@Entity("{{EntityName}}"{{#Schema}},{schema:"{{.}}"}{{/Schema}})
@Entity("{{sqlEntityName}}"{{#Schema}},{schema:"{{.}}"}{{/Schema}})
{{#Indexes}}{{^isPrimaryKey}}@Index("{{name}}",[{{#columns}}"{{toPropertyName name}}",{{/columns}}]{{#isUnique}},{unique:true}{{/isUnique}})
{{/isPrimaryKey}}{{/Indexes}}export class {{toEntityName EntityName}}{{#IsActiveRecord}} extends BaseEntity{{/IsActiveRecord}} {
{{/isPrimaryKey}}{{/Indexes}}export class {{toEntityName tsEntityName}}{{#IsActiveRecord}} extends BaseEntity{{/IsActiveRecord}} {
{{#Columns}}
{{^relations}}{{#isGenerated}} @PrimaryGeneratedColumn({
@ -32,11 +32,11 @@ import {BaseEntity,Column,Entity,Index,JoinColumn,JoinTable,ManyToMany,ManyToOne
{{/if}}
{{#if relationIdField }}
@RelationId(({{../../EntityName}}: {{../../EntityName}}) => {{../../EntityName}}.{{toPropertyName ../tsName}})
@RelationId(({{../../tsEntityName}}: {{../../tsEntityName}}) => {{../../tsEntityName}}.{{toPropertyName ../tsName}})
{{toPropertyName ../tsName}}Id: {{#if isOneToOne}}{{toLazy ../tsType}}{{else}}{{toLazy (concat ../tsType "[]")}}{{/if}};{{/if}}{{/relations}}
{{/Columns}}
{{#if GenerateConstructor}}
constructor(init?: Partial<{{toEntityName EntityName}}>) {
constructor(init?: Partial<{{toEntityName tsEntityName}}>) {
Object.assign(this, init);
}
{{/if}}

View File

@ -1,7 +1,8 @@
import { ColumnInfo } from "./ColumnInfo";
export class EntityInfo {
public EntityName: string;
public tsEntityName: string;
public sqlEntityName: string;
public Columns: ColumnInfo[];
public Imports: string[];
public UniqueImports: string[];
@ -14,7 +15,7 @@ export class EntityInfo {
const imports: string[] = [];
this.Columns.forEach(column => {
column.relations.forEach(relation => {
if (this.EntityName !== relation.relatedTable) {
if (this.tsEntityName !== relation.relatedTable) {
imports.push(relation.relatedTable);
}
});

View File

@ -49,7 +49,8 @@ describe('MssqlDriver', function () {
const result = await driver.GetAllTables('schema')
const expectedResult = [] as EntityInfo[];
const y = new EntityInfo();
y.EntityName = 'name'
y.tsEntityName = 'name'
y.sqlEntityName = 'name'
y.Schema='schema'
y.Columns = [] as ColumnInfo[];
y.Indexes = [] as IndexInfo[];
@ -75,7 +76,7 @@ describe('MssqlDriver', function () {
const entities = [] as EntityInfo[];
const y = new EntityInfo();
y.EntityName = 'name'
y.tsEntityName = 'name'
y.Columns = [] as ColumnInfo[];
y.Indexes = [] as IndexInfo[];
entities.push(y)