From 270461a1934f52a00a9fe158ed2367da691693cf Mon Sep 17 00:00:00 2001 From: Kononnable Date: Sun, 6 Jan 2019 15:37:52 +0100 Subject: [PATCH] fixed naming strategy changing entity name in db --- src/Engine.ts | 10 ++++----- src/NamingStrategy.ts | 2 +- src/drivers/AbstractDriver.ts | 38 +++++++++++++++++--------------- src/drivers/MssqlDriver.ts | 4 ++-- src/drivers/MysqlDriver.ts | 4 ++-- src/drivers/OracleDriver.ts | 4 ++-- src/drivers/PostgresDriver.ts | 4 ++-- src/drivers/SqliteDriver.ts | 18 ++++++++------- src/entity.mst | 8 +++---- src/models/EntityInfo.ts | 5 +++-- test/drivers/MssqlDriver.test.ts | 5 +++-- 11 files changed, 54 insertions(+), 48 deletions(-) diff --git a/src/Engine.ts b/src/Engine.ts index a0a9aae..00bdfea 100644 --- a/src/Engine.ts +++ b/src/Engine.ts @@ -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( diff --git a/src/NamingStrategy.ts b/src/NamingStrategy.ts index 698f5a6..10bf0c5 100644 --- a/src/NamingStrategy.ts +++ b/src/NamingStrategy.ts @@ -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 = diff --git a/src/drivers/AbstractDriver.ts b/src/drivers/AbstractDriver.ts index 6a699cb..f04cf2e 100644 --- a/src/drivers/AbstractDriver.ts +++ b/src/drivers/AbstractDriver.ts @@ -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; diff --git a/src/drivers/MssqlDriver.ts b/src/drivers/MssqlDriver.ts index 338603f..14b7c21 100644 --- a/src/drivers/MssqlDriver.ts +++ b/src/drivers/MssqlDriver.ts @@ -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; diff --git a/src/drivers/MysqlDriver.ts b/src/drivers/MysqlDriver.ts index ba7ff4d..cd110cd 100644 --- a/src/drivers/MysqlDriver.ts +++ b/src/drivers/MysqlDriver.ts @@ -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; diff --git a/src/drivers/OracleDriver.ts b/src/drivers/OracleDriver.ts index c1b0e12..75baece 100644 --- a/src/drivers/OracleDriver.ts +++ b/src/drivers/OracleDriver.ts @@ -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; diff --git a/src/drivers/PostgresDriver.ts b/src/drivers/PostgresDriver.ts index 4716818..4b8304a 100644 --- a/src/drivers/PostgresDriver.ts +++ b/src/drivers/PostgresDriver.ts @@ -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; diff --git a/src/drivers/SqliteDriver.ts b/src/drivers/SqliteDriver.ts index b0799a0..ed7e6c6 100644 --- a/src/drivers/SqliteDriver.ts +++ b/src/drivers/SqliteDriver.ts @@ -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); diff --git a/src/entity.mst b/src/entity.mst index 2a8fa93..eb6846f 100644 --- a/src/entity.mst +++ b/src/entity.mst @@ -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}} diff --git a/src/models/EntityInfo.ts b/src/models/EntityInfo.ts index 30eec70..1c51dba 100644 --- a/src/models/EntityInfo.ts +++ b/src/models/EntityInfo.ts @@ -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); } }); diff --git a/test/drivers/MssqlDriver.test.ts b/test/drivers/MssqlDriver.test.ts index 30448e6..be80d21 100644 --- a/test/drivers/MssqlDriver.test.ts +++ b/test/drivers/MssqlDriver.test.ts @@ -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)