naming strategy use functions instead class

This commit is contained in:
Kononnable 2019-11-17 16:43:52 +01:00
parent 20f3e24dc8
commit 3496c0647b
4 changed files with 141 additions and 109 deletions

View File

@ -1,18 +0,0 @@
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;
public abstract columnName(columnName: string, column?: Column): string;
}

View File

@ -2,26 +2,83 @@ import { DataTypeDefaults } from "typeorm/driver/types/DataTypeDefaults";
import { DefaultNamingStrategy } from "typeorm/naming-strategy/DefaultNamingStrategy";
import { Entity } from "./models/Entity";
import IGenerationOptions from "./IGenerationOptions";
import AbstractNamingStrategy from "./AbstractNamingStrategy";
import NamingStrategy from "./NamingStrategy";
import * as NamingStrategy from "./NamingStrategy";
import * as TomgUtils from "./Utils";
import { Relation } from "./models/Relation";
import { RelationId } from "./models/RelationId";
import { Column } from "./models/Column";
type NamingStrategy = {
relationIdName: (
relationId: RelationId,
relation: Relation,
owner: Entity
) => string;
relationName: (relation: Relation, owner: Entity) => string;
columnName: (columnName: string, column?: Column) => string;
entityName: (entityName: string, entity?: Entity) => string;
};
export default function modelCustomizationPhase(
dbModel: Entity[],
generationOptions: IGenerationOptions,
defaultValues: DataTypeDefaults
): Entity[] {
let namingStrategy: AbstractNamingStrategy;
const namingStrategy: NamingStrategy = {
columnName: NamingStrategy.columnName,
entityName: NamingStrategy.entityName,
relationIdName: NamingStrategy.relationIdName,
relationName: NamingStrategy.relationName
};
if (
generationOptions.customNamingStrategyPath &&
generationOptions.customNamingStrategyPath !== ""
) {
// TODO: change form of logging
// eslint-disable-next-line global-require, import/no-dynamic-require, @typescript-eslint/no-var-requires
const req = require(generationOptions.customNamingStrategyPath);
// eslint-disable-next-line new-cap
namingStrategy = new req.default();
} else {
namingStrategy = new NamingStrategy();
const req = require(generationOptions.customNamingStrategyPath) as Partial<
NamingStrategy
>;
if (req.columnName) {
console.log(
`[${new Date().toLocaleTimeString()}] Using custom naming strategy for column names.`
);
namingStrategy.columnName = req.columnName;
} else {
console.log(
`[${new Date().toLocaleTimeString()}] Using standard naming strategy for column names.`
);
}
if (req.entityName) {
console.log(
`[${new Date().toLocaleTimeString()}] Using custom naming strategy for entity names.`
);
namingStrategy.entityName = req.entityName;
} else {
console.log(
`[${new Date().toLocaleTimeString()}] Using standard naming strategy for entity names.`
);
}
if (req.relationIdName) {
console.log(
`[${new Date().toLocaleTimeString()}] Using custom naming strategy for relationId field names.`
);
namingStrategy.relationIdName = req.relationIdName;
} else {
console.log(
`[${new Date().toLocaleTimeString()}] Using standard naming strategy for relationId field names.`
);
}
if (req.relationName) {
console.log(
`[${new Date().toLocaleTimeString()}] Using custom naming strategy for relation field names.`
);
namingStrategy.relationName = req.relationName;
} else {
console.log(
`[${new Date().toLocaleTimeString()}] Using standard naming strategy for relation field names.`
);
}
}
let retVal = removeIndicesGeneratedByTypeorm(dbModel);
retVal = removeColumnsInRelation(dbModel);
@ -173,7 +230,7 @@ function addImportsAndGenerationOptions(
}
function applyNamingStrategy(
namingStrategy: AbstractNamingStrategy,
namingStrategy: NamingStrategy,
dbModel: Entity[]
): Entity[] {
let retVal = changeRelationNames(dbModel);

View File

@ -1,77 +1,72 @@
import { plural } from "pluralize";
import AbstractNamingStrategy from "./AbstractNamingStrategy";
import { Relation } from "./models/Relation";
import { RelationId } from "./models/RelationId";
import changeCase = require("change-case");
// TODO: Use function instead of class
// TODO: Allow users to change only specific functions instead of all of them(with logging if used standard or user function)
// TODO: Fix naming strategy relative path
/* eslint-disable class-methods-use-this */
export default class NamingStrategy extends AbstractNamingStrategy {
public relationIdName(relationId: RelationId, relation: Relation): string {
const columnOldName = relationId.fieldName;
export function relationIdName(
relationId: RelationId,
relation: Relation
): string {
const columnOldName = relationId.fieldName;
const isRelationToMany =
relation.relationType === "OneToMany" ||
relation.relationType === "ManyToMany";
let columnName = changeCase.camelCase(
columnOldName.replace(/[0-9]$/, "")
);
const isRelationToMany =
relation.relationType === "OneToMany" ||
relation.relationType === "ManyToMany";
let newColumnName = 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);
}
if (isRelationToMany) {
columnName = plural(columnName);
}
return columnName;
if (!Number.isNaN(parseInt(newColumnName[newColumnName.length - 1], 10))) {
newColumnName = newColumnName.substring(0, newColumnName.length - 1);
}
if (!Number.isNaN(parseInt(newColumnName[newColumnName.length - 1], 10))) {
newColumnName = newColumnName.substring(0, newColumnName.length - 1);
}
if (isRelationToMany) {
newColumnName = plural(newColumnName);
}
public relationName(relation: Relation): string {
const columnOldName = relation.fieldName;
const isRelationToMany =
relation.relationType === "OneToMany" ||
relation.relationType === "ManyToMany";
let columnName = changeCase.camelCase(
columnOldName.replace(/[0-9]$/, "")
);
if (
columnName.toLowerCase().endsWith("id") &&
!columnName.toLowerCase().endsWith("guid")
) {
columnName = columnName.substring(
0,
columnName.toLowerCase().lastIndexOf("id")
);
}
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);
}
if (isRelationToMany) {
columnName = plural(columnName);
}
return columnName;
}
public entityName(entityName: string): string {
return entityName;
}
public columnName(columnName: string): string {
return columnName;
}
return newColumnName;
}
/* eslint-enable class-methods-use-this */
export function relationName(relation: Relation): string {
const columnOldName = relation.fieldName;
const isRelationToMany =
relation.relationType === "OneToMany" ||
relation.relationType === "ManyToMany";
let newColumnName = changeCase.camelCase(
columnOldName.replace(/[0-9]$/, "")
);
if (
newColumnName.toLowerCase().endsWith("id") &&
!newColumnName.toLowerCase().endsWith("guid")
) {
newColumnName = newColumnName.substring(
0,
newColumnName.toLowerCase().lastIndexOf("id")
);
}
if (!Number.isNaN(parseInt(newColumnName[newColumnName.length - 1], 10))) {
newColumnName = newColumnName.substring(0, newColumnName.length - 1);
}
if (!Number.isNaN(parseInt(newColumnName[newColumnName.length - 1], 10))) {
newColumnName = newColumnName.substring(0, newColumnName.length - 1);
}
if (isRelationToMany) {
newColumnName = plural(newColumnName);
}
return newColumnName;
}
export function entityName(oldEntityName: string): string {
return oldEntityName;
}
export function columnName(oldColumnName: string): string {
return oldColumnName;
}

View File

@ -1,24 +1,22 @@
import StandardNamingStrategy from "../../src/NamingStrategy";
import * as NamingStrategy from "../../src/NamingStrategy";
import { RelationId } from "../../src/models/RelationId";
import { Relation } from "../../src/models/Relation";
/* eslint-disable class-methods-use-this */
export default class NamingStrategy extends StandardNamingStrategy {
public relationIdName(relationId: RelationId, relation: Relation): string {
return `${super.relationIdName(relationId, relation)}`;
}
public relationName(relation: Relation): string {
return `${super.relationName(relation)}_A`;
}
public entityName(entityName: string): string {
return `${super.entityName(entityName)}_B`;
}
public columnName(columnName: string): string {
return `${super.columnName(columnName)}_C`;
}
export function relationIdName(
relationId: RelationId,
relation: Relation
): string {
return `${NamingStrategy.relationIdName(relationId, relation)}`;
}
/* eslint-enable class-methods-use-this */
export function relationName(relation: Relation): string {
return `${NamingStrategy.relationName(relation)}_A`;
}
export function entityName(oldEntityName: string): string {
return `${NamingStrategy.entityName(oldEntityName)}_B`;
}
export function columnName(oldColumnName: string): string {
return `${NamingStrategy.columnName(oldColumnName)}_C`;
}