more tests passing

This commit is contained in:
Kononnable 2019-10-07 13:42:48 +02:00
parent e86a26d279
commit 099f4bcc9a
5 changed files with 99 additions and 129 deletions

View File

@ -1,15 +1,11 @@
import RelationInfo from "./oldModels/RelationInfo";
import EntityInfo from "./oldModels/EntityInfo";
import ColumnInfo from "./oldModels/ColumnInfo";
import { Relation } from "./models/Relation";
import { Entity } from "./models/Entity";
import { Column } from "./models/Column";
export default abstract class AbstractNamingStrategy {
public abstract relationName(
columnName: string,
relation: RelationInfo,
dbModel: EntityInfo[]
): string;
public abstract relationName(relation: Relation, owner: Entity): string;
public abstract entityName(entityName: string, entity?: EntityInfo): string;
public abstract entityName(entityName: string, entity?: Entity): string;
public abstract columnName(columnName: string, column?: ColumnInfo): string;
public abstract columnName(columnName: string, column?: Column): string;
}

View File

@ -145,7 +145,7 @@ export function modelCustomizationPhase(
}
let retVal = setRelationId(generationOptions, dbModel);
// TODO:
// retVal = applyNamingStrategy(namingStrategy, retVal);
retVal = applyNamingStrategy(namingStrategy, retVal);
// retVal = addImportsAndGenerationOptions(retVal, generationOptions);
// retVal = removeColumnDefaultProperties(retVal, defaultValues);
return retVal;
@ -306,9 +306,18 @@ function createHandlebarsHelpers(generationOptions: IGenerationOptions) {
}
return retStr;
});
Handlebars.registerHelper("concat", (stra, strb) => {
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) =>
@ -448,124 +457,93 @@ function createTypeOrmConfig(
}
function applyNamingStrategy(
namingStrategy: AbstractNamingStrategy,
dbModel: EntityInfo[]
dbModel: Entity[]
) {
let retval = changeRelationNames(dbModel);
retval = changeEntityNames(retval);
retval = changeColumnNames(retval);
return retval;
function changeRelationNames(model: EntityInfo[]) {
function changeRelationNames(model: Entity[]) {
model.forEach(entity => {
entity.Columns.forEach(column => {
column.relations.forEach(relation => {
const newName = namingStrategy.relationName(
column.tsName,
relation,
model
);
model.forEach(entity2 => {
entity2.Columns.forEach(column2 => {
column2.relations.forEach(relation2 => {
if (
relation2.relatedTable ===
entity.tsEntityName &&
relation2.ownerColumn === column.tsName
) {
relation2.ownerColumn = newName;
}
if (
relation2.relatedTable ===
entity.tsEntityName &&
relation2.relatedColumn === column.tsName
) {
relation2.relatedColumn = newName;
}
if (relation.isOwner) {
entity.Indexes.forEach(ind => {
ind.columns
.filter(
col =>
col.name === column.tsName
)
.forEach(col => {
col.name = newName;
});
});
}
});
});
entity.relations.forEach(relation => {
const oldName = relation.fieldName;
const newName = namingStrategy.relationName(relation, entity);
const relatedEntity = model.find(
v => v.tscName === relation.relatedTable
)!;
const relation2 = relatedEntity.relations.find(
v => v.fieldName === relation.relatedField
)!;
relation.fieldName = newName;
relation2.relatedField = newName;
if (relation.relationOptions) {
entity.indices.forEach(ind => {
ind.columns.map(column2 =>
column2 === oldName ? newName : column2
);
});
column.tsName = newName;
});
}
});
});
return dbModel;
}
function changeColumnNames(model: EntityInfo[]) {
function changeColumnNames(model: Entity[]) {
model.forEach(entity => {
entity.Columns.forEach(column => {
entity.columns.forEach(column => {
const newName = namingStrategy.columnName(
column.tsName,
column.tscName,
column
);
entity.Indexes.forEach(index => {
index.columns
.filter(column2 => column2.name === column.tsName)
.forEach(column2 => {
column2.name = newName;
});
});
model.forEach(entity2 => {
entity2.Columns.forEach(column2 => {
column2.relations
.filter(
relation =>
relation.relatedTable ===
entity.tsEntityName &&
relation.relatedColumn === column.tsName
)
.forEach(v => {
v.relatedColumn = newName;
});
column2.relations
.filter(
relation =>
relation.relatedTable ===
entity.tsEntityName &&
relation.ownerColumn === column.tsName
)
.forEach(v => {
v.ownerColumn = newName;
});
});
entity.indices.forEach(index => {
index.columns.map(column2 =>
column2 === column.tscName ? newName : column2
);
});
// TODO: Should relational columns be changed by both changeRelationNames and changeColumnNames?
// model.forEach(entity2 => {
// entity2.columns.forEach(column2 => {
// column2.relations
// .filter(
// relation =>
// relation.relatedTable === entity.tscName &&
// relation.relatedColumn === column.tscName
// )
// .forEach(v => {
// v.relatedColumn = newName;
// });
// column2.relations
// .filter(
// relation =>
// relation.relatedTable === entity.tscName &&
// relation.ownerColumn === column.tscName
// )
// .forEach(v => {
// v.ownerColumn = newName;
// });
// });
// });
column.tsName = newName;
column.tscName = newName;
});
});
return model;
}
function changeEntityNames(entities: EntityInfo[]) {
function changeEntityNames(entities: Entity[]) {
entities.forEach(entity => {
const newName = namingStrategy.entityName(
entity.tsEntityName,
entity
);
const newName = namingStrategy.entityName(entity.tscName, entity);
entities.forEach(entity2 => {
entity2.Columns.forEach(column => {
column.relations.forEach(relation => {
if (relation.ownerTable === entity.tsEntityName) {
relation.ownerTable = newName;
}
if (relation.relatedTable === entity.tsEntityName) {
relation.relatedTable = newName;
}
});
entity2.relations.forEach(relation => {
if (relation.relatedTable === entity.tscName) {
relation.relatedTable = newName;
}
});
});
entity.tsEntityName = newName;
entity.tscName = newName;
});
return entities;
}

View File

@ -1,20 +1,16 @@
import AbstractNamingStrategy from "./AbstractNamingStrategy";
import RelationInfo from "./oldModels/RelationInfo";
import EntityInfo from "./oldModels/EntityInfo";
import { Relation } from "./models/Relation";
import { Entity } from "./models/Entity";
import changeCase = require("change-case");
/* eslint-disable class-methods-use-this */
export default class NamingStrategy extends AbstractNamingStrategy {
public relationName(
columnOldName: string,
relation: RelationInfo,
dbModel: EntityInfo[]
): string {
const isRelationToMany = relation.isOneToMany || relation.isManyToMany;
const ownerEntity = dbModel.find(
v => v.tsEntityName === relation.ownerTable
)!;
public relationName(relation: Relation, owner: Entity): string {
const columnOldName = relation.fieldName;
const isRelationToMany =
relation.relationType === "OneToMany" ||
relation.relationType === "ManyToMany";
let columnName = changeCase.camelCase(columnOldName);
if (
@ -38,18 +34,18 @@ export default class NamingStrategy extends AbstractNamingStrategy {
relation.relationType !== "ManyToMany" &&
columnOldName !== columnName
) {
if (ownerEntity.Columns.some(v => v.tsName === columnName)) {
if (owner.columns.some(v => v.tscName === columnName)) {
columnName += "_";
for (let i = 2; i <= ownerEntity.Columns.length; i++) {
for (let i = 2; i <= owner.columns.length; i++) {
columnName =
columnName.substring(
0,
columnName.length - i.toString().length
) + i.toString();
if (
ownerEntity.Columns.every(
owner.columns.every(
v =>
v.tsName !== columnName ||
v.tscName !== columnName ||
columnName === columnOldName
)
) {

View File

@ -348,6 +348,14 @@ export default abstract class AbstractDriver {
}
const ownerRelation: Relation = {
fieldName,
relatedField: AbstractDriver.findNameForNewField(
relationTmp.ownerTable.tscName,
relationTmp.relatedTable
),
relationOptions: {
onDelete: relationTmp.onDelete,
onUpdate: relationTmp.onUpdate
},
joinColumnOptions: relationTmp.ownerColumns.map(
(v, idx) => {
const retVal: JoinColumnOptions = {
@ -358,23 +366,15 @@ export default abstract class AbstractDriver {
return retVal;
}
),
relatedField: AbstractDriver.findNameForNewField(
relationTmp.ownerTable.tscName,
relationTmp.relatedTable
),
relatedTable: relationTmp.relatedTable.tscName,
relationOptions: {
onDelete: relationTmp.onDelete,
onUpdate: relationTmp.onUpdate
},
relationType: isOneToMany ? "OneToMany" : "OneToOne"
relationType: isOneToMany ? "ManyToOne" : "OneToOne"
};
const relatedRelation: Relation = {
fieldName: ownerRelation.relatedField,
relatedField: ownerRelation.fieldName,
relatedTable: relationTmp.ownerTable.tscName,
// relationOptions: ownerRelation.relationOptions,
relationType: isOneToMany ? "ManyToOne" : "OneToOne"
relationType: isOneToMany ? "OneToMany" : "OneToOne"
};
ownerEntity.relations.push(ownerRelation);

View File

@ -13,7 +13,7 @@ import { {{toEntityName .}} } from './{{toFileName .}}'
@{{relationType}}(()=>{{toEntityName relatedTable}},{{toEntityName relatedTable}}=>{{toEntityName relatedTable}}.{{toPropertyName relatedField}}{{#if (or joinColumnOptions joinTableOptions)}},{ {{json relationOptions}} }{{/if}})
{{#if joinColumnOptions}}@JoinColumn({{json joinColumnOptions}}){{/if}}
{{#if joinTableOptions}}@JoinTable{{json joinTableOptions}}){{/if}}
{{toPropertyName fieldName}}:{{toEntityName relatedTable}};
{{toPropertyName fieldName}}:{{toEntityName relatedTable}}{{#contains "ToMany" relationType}}[]{{/contains}};
{{/inline}}
{{#*inline "Entity"}}