From 3496c0647b179ea5dae6e9e75e6902fe0af1463e Mon Sep 17 00:00:00 2001 From: Kononnable Date: Sun, 17 Nov 2019 16:43:52 +0100 Subject: [PATCH] naming strategy use functions instead class --- src/AbstractNamingStrategy.ts | 18 --- src/ModelCustomization.ts | 75 +++++++++-- src/NamingStrategy.ts | 121 +++++++++--------- test/modelCustomization/testNamingStrategy.ts | 36 +++--- 4 files changed, 141 insertions(+), 109 deletions(-) delete mode 100644 src/AbstractNamingStrategy.ts diff --git a/src/AbstractNamingStrategy.ts b/src/AbstractNamingStrategy.ts deleted file mode 100644 index 547367f..0000000 --- a/src/AbstractNamingStrategy.ts +++ /dev/null @@ -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; -} diff --git a/src/ModelCustomization.ts b/src/ModelCustomization.ts index 2ad4f3b..4c7dbb2 100644 --- a/src/ModelCustomization.ts +++ b/src/ModelCustomization.ts @@ -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); diff --git a/src/NamingStrategy.ts b/src/NamingStrategy.ts index 6035634..fd077a0 100644 --- a/src/NamingStrategy.ts +++ b/src/NamingStrategy.ts @@ -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; +} diff --git a/test/modelCustomization/testNamingStrategy.ts b/test/modelCustomization/testNamingStrategy.ts index 970204d..b3ed507 100644 --- a/test/modelCustomization/testNamingStrategy.ts +++ b/test/modelCustomization/testNamingStrategy.ts @@ -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`; +}