RelationIds for OneToOne, OneToMany

This commit is contained in:
Kononnable 2019-10-10 15:40:08 +02:00
parent 0936f81e3c
commit ad8e1f528f
4 changed files with 70 additions and 10 deletions

View File

@ -1,8 +1,15 @@
import { Relation } from "./models/Relation";
import { Entity } from "./models/Entity";
import { Column } from "./models/Column";
import { RelationId } from "./models/RelationId";
export default abstract class AbstractNamingStrategy {
public abstract relationIdName(
relationId: RelationId,
relation: Relation,
owner: Entity
): string;
public abstract relationName(relation: Relation, owner: Entity): string;
public abstract entityName(entityName: string, entity?: Entity): string;

View File

@ -473,10 +473,41 @@ function applyNamingStrategy(
dbModel: Entity[]
) {
let retval = changeRelationNames(dbModel);
retval = changeRelationIdNames(retval);
retval = changeEntityNames(retval);
retval = changeColumnNames(retval);
return retval;
function changeRelationIdNames(model: Entity[]) {
// TODO:
model.forEach(entity => {
entity.relationIds.forEach(relationId => {
const oldName = relationId.fieldName;
const relation = entity.relations.find(
v => v.fieldName === relationId.relationField
)!;
let newName = namingStrategy.relationIdName(
relationId,
relation,
entity
);
newName = TomgUtils.findNameForNewField(
newName,
entity,
oldName
);
entity.indices.forEach(index => {
index.columns = index.columns.map(column2 =>
column2 === oldName ? newName : column2
);
});
relationId.fieldName = newName;
});
});
return dbModel;
}
function changeRelationNames(model: Entity[]) {
model.forEach(entity => {
entity.relations.forEach(relation => {
@ -495,6 +526,10 @@ function applyNamingStrategy(
v => v.fieldName === relation.relatedField
)!;
entity.relationIds
.filter(v => v.relationField === oldName)
.forEach(v => (v.relationField = newName));
relation.fieldName = newName;
relation2.relatedField = newName;

View File

@ -1,12 +1,37 @@
import AbstractNamingStrategy from "./AbstractNamingStrategy";
import { Relation } from "./models/Relation";
import { Entity } from "./models/Entity";
import { findNameForNewField } from "./Utils";
import { RelationId } from "./models/RelationId";
import changeCase = require("change-case");
/* eslint-disable class-methods-use-this */
export default class NamingStrategy extends AbstractNamingStrategy {
public relationIdName(
relationId: RelationId,
relation: Relation,
owner: Entity
): string {
const columnOldName = relationId.fieldName;
const isRelationToMany =
relation.relationType === "OneToMany" ||
relation.relationType === "ManyToMany";
let columnName = changeCase.camelCase(
columnOldName.replace(/[0-9]$/, "")
);
if (!Number.isNaN(parseInt(columnName[columnName.length - 1], 10))) {
columnName = columnName.substring(0, columnName.length - 1);
}
if (!Number.isNaN(parseInt(columnName[columnName.length - 1], 10))) {
columnName = columnName.substring(0, columnName.length - 1);
}
columnName += isRelationToMany ? "s" : "";
return columnName;
}
public relationName(relation: Relation, owner: Entity): string {
const columnOldName = relation.fieldName;
@ -34,13 +59,6 @@ export default class NamingStrategy extends AbstractNamingStrategy {
}
columnName += isRelationToMany ? "s" : "";
if (
relation.relationType !== "ManyToMany" &&
columnOldName !== columnName
) {
columnName = findNameForNewField(columnName, owner, columnOldName);
}
return columnName;
}

View File

@ -410,11 +410,11 @@ export default abstract class AbstractDriver {
ownerEntity
);
ownerEntity.relationIds.push({
fieldName: relationIdFieldName, // TODO: generate name without number(naming strategy)
fieldName: relationIdFieldName,
fieldType: isOneToMany
? `${ownerColumns[0].tscType}[]`
: ownerColumns[0].tscType,
relationField: ownerRelation.fieldName // TODO: naming strategy
relationField: ownerRelation.fieldName
});
// TODO: RelationId on ManyToMany
}