From c011ed3957fd5150207fec10b6dc41d9a0feabfb Mon Sep 17 00:00:00 2001 From: kononnable Date: Sat, 28 Nov 2020 00:48:29 +0100 Subject: [PATCH] naming strategy for entity file names(#236) --- src/ModelCustomization.ts | 31 +++++++++++++++++-- src/ModelGeneration.ts | 8 ++--- src/NamingStrategy.ts | 4 +++ src/drivers/AbstractDriver.ts | 1 + src/drivers/SqliteDriver.ts | 1 + src/models/Entity.ts | 6 +++- src/templates/entity.mst | 2 +- .../modelCustomization.test.ts | 10 +++--- test/modelCustomization/testNamingStrategy.ts | 4 +++ 9 files changed, 55 insertions(+), 12 deletions(-) diff --git a/src/ModelCustomization.ts b/src/ModelCustomization.ts index 5973f02..711632a 100644 --- a/src/ModelCustomization.ts +++ b/src/ModelCustomization.ts @@ -16,6 +16,7 @@ export default function modelCustomizationPhase( entityName: NamingStrategy.entityName, relationIdName: NamingStrategy.relationIdName, relationName: NamingStrategy.relationName, + fileName: NamingStrategy.fileName, }; if ( generationOptions.customNamingStrategyPath && @@ -65,6 +66,16 @@ export default function modelCustomizationPhase( `[${new Date().toLocaleTimeString()}] Using standard naming strategy for relation field names.` ); } + if (req.fileName) { + console.log( + `[${new Date().toLocaleTimeString()}] Using custom naming strategy for entity file names.` + ); + namingStrategy.fileName = req.fileName; + } else { + console.log( + `[${new Date().toLocaleTimeString()}] Using standard naming strategy for entity file names.` + ); + } if (req.enablePluralization) { console.log( `[${new Date().toLocaleTimeString()}] Using custom pluralization method for OneToMany, ManyToMany relation field names.` @@ -189,9 +200,17 @@ function findFileImports(dbModel: Entity[]) { entity.relations.forEach((relation) => { if ( relation.relatedTable !== entity.tscName && - !entity.fileImports.some((v) => v === relation.relatedTable) + !entity.fileImports.some( + (v) => v.entityName === relation.relatedTable + ) ) { - entity.fileImports.push(relation.relatedTable); + let relatedTable = dbModel.find( + (related) => related.tscName == relation.relatedTable + )!; + entity.fileImports.push({ + entityName: relatedTable.tscName, + fileName: relatedTable.fileName, + }); } }); }); @@ -234,6 +253,7 @@ function applyNamingStrategy( retVal = changeRelationIdNames(retVal); retVal = changeEntityNames(retVal); retVal = changeColumnNames(retVal); + retVal = changeFileNames(retVal); return retVal; function changeRelationIdNames(model: Entity[]): Entity[] { @@ -336,6 +356,13 @@ function applyNamingStrategy( }); }); entity.tscName = newName; + entity.fileName = newName; + }); + return entities; + } + function changeFileNames(entities: Entity[]): Entity[] { + entities.forEach((entity) => { + entity.fileName = namingStrategy.fileName(entity.fileName); }); return entities; } diff --git a/src/ModelGeneration.ts b/src/ModelGeneration.ts index 2a544c4..1a5ceda 100644 --- a/src/ModelGeneration.ts +++ b/src/ModelGeneration.ts @@ -58,16 +58,16 @@ function generateModels( let casedFileName = ""; switch (generationOptions.convertCaseFile) { case "camel": - casedFileName = changeCase.camelCase(element.tscName); + casedFileName = changeCase.camelCase(element.fileName); break; case "param": - casedFileName = changeCase.paramCase(element.tscName); + casedFileName = changeCase.paramCase(element.fileName); break; case "pascal": - casedFileName = changeCase.pascalCase(element.tscName); + casedFileName = changeCase.pascalCase(element.fileName); break; case "none": - casedFileName = element.tscName; + casedFileName = element.fileName; break; default: throw new Error("Unknown case style"); diff --git a/src/NamingStrategy.ts b/src/NamingStrategy.ts index e9edbba..dc6110d 100644 --- a/src/NamingStrategy.ts +++ b/src/NamingStrategy.ts @@ -77,3 +77,7 @@ export function entityName(oldEntityName: string, entity?: Entity): string { export function columnName(oldColumnName: string, column?: Column): string { return oldColumnName; } + +export function fileName(oldFileName: string): string { + return oldFileName; +} diff --git a/src/drivers/AbstractDriver.ts b/src/drivers/AbstractDriver.ts index 7023da7..c6deff0 100644 --- a/src/drivers/AbstractDriver.ts +++ b/src/drivers/AbstractDriver.ts @@ -246,6 +246,7 @@ export default abstract class AbstractDriver { relationIds: [], sqlName: val.TABLE_NAME, tscName: val.TABLE_NAME, + fileName: val.TABLE_NAME, database: dbNames.includes(",") ? val.DB_NAME : "", schema: val.TABLE_SCHEMA, fileImports: [], diff --git a/src/drivers/SqliteDriver.ts b/src/drivers/SqliteDriver.ts index abe1fa2..6983598 100644 --- a/src/drivers/SqliteDriver.ts +++ b/src/drivers/SqliteDriver.ts @@ -64,6 +64,7 @@ export default class SqliteDriver extends AbstractDriver { relationIds: [], sqlName: val.tbl_name, tscName: val.tbl_name, + fileName: val.tbl_name, fileImports: [], }); }); diff --git a/src/models/Entity.ts b/src/models/Entity.ts index 8c189f9..d604ada 100644 --- a/src/models/Entity.ts +++ b/src/models/Entity.ts @@ -15,7 +15,11 @@ export type Entity = { relations: Relation[]; indices: Index[]; // TODO: move to sub-object or use handlebars helpers(?) - fileImports: string[]; + fileName: string; + fileImports: { + entityName: string; + fileName: string; + }[]; activeRecord?: true; generateConstructor?: true; }; diff --git a/src/templates/entity.mst b/src/templates/entity.mst index c9b57e8..7282fb0 100644 --- a/src/templates/entity.mst +++ b/src/templates/entity.mst @@ -2,7 +2,7 @@ @Index("{{name}}",[{{#columns}}"{{toPropertyName .}}",{{/columns~}}],{ {{json options}} }) {{/inline}} {{#*inline "Import"}} -import {{localImport (toEntityName .)}} from './{{toFileName .}}' +import {{localImport (toEntityName entityName)}} from './{{toFileName fileName}}' {{/inline}} {{#*inline "Column"}} {{#generated}}@PrimaryGeneratedColumn({ type:"{{type}}", {{/generated}}{{^generated}}@Column("{{type}}",{ {{#primary}}primary:{{primary}},{{/primary}}{{/generated}}{{json options}}{{#default}},default: {{.}},{{/default}} }) diff --git a/test/modelCustomization/modelCustomization.test.ts b/test/modelCustomization/modelCustomization.test.ts index e42aaa0..b7defd9 100644 --- a/test/modelCustomization/modelCustomization.test.ts +++ b/test/modelCustomization/modelCustomization.test.ts @@ -57,6 +57,7 @@ describe("Model customization phase", async () => { relationIds: [], sqlName: "PostAuthor", tscName: "PostAuthor", + fileName: "PostAuthor", database: "", schema: "public", fileImports: [] @@ -111,6 +112,7 @@ describe("Model customization phase", async () => { relationIds: [], sqlName: "Post", tscName: "Post", + fileName: "Post", database: "", schema: "public", fileImports: [] @@ -674,23 +676,23 @@ describe("Model customization phase", async () => { ); const filesGenPath = path.resolve(resultsPath, "entities"); const postContent = fs - .readFileSync(path.resolve(filesGenPath, "Post_B.ts")) + .readFileSync(path.resolve(filesGenPath, "Post_B_D.ts")) .toString(); const postAuthorContent = fs - .readFileSync(path.resolve(filesGenPath, "PostAuthor_B.ts")) + .readFileSync(path.resolve(filesGenPath, "PostAuthor_B_D.ts")) .toString(); expect(postContent).to.have.string(`@Entity("Post"`); expect(postContent).to.have.string(`class Post_B {`); expect(postContent).to.have.string(`id_C: number;`); expect(postContent).to.have.string(`author_A: PostAuthor_B`); expect(postContent).to.have.string( - `import { PostAuthor_B } from "./PostAuthor_B";` + `import { PostAuthor_B } from "./PostAuthor_B_D";` ); expect(postAuthorContent).to.have.string(`@Entity("PostAuthor"`); expect(postAuthorContent).to.have.string(`class PostAuthor_B`); expect(postAuthorContent).to.have.string(`id_C: number;`); expect(postAuthorContent).to.have.string( - `import { Post_B } from "./Post_B";` + `import { Post_B } from "./Post_B_D";` ); compileGeneratedModel(generationOptions.resultsPath, [""], false); diff --git a/test/modelCustomization/testNamingStrategy.ts b/test/modelCustomization/testNamingStrategy.ts index b3ed507..22b22b8 100644 --- a/test/modelCustomization/testNamingStrategy.ts +++ b/test/modelCustomization/testNamingStrategy.ts @@ -20,3 +20,7 @@ export function entityName(oldEntityName: string): string { export function columnName(oldColumnName: string): string { return `${NamingStrategy.columnName(oldColumnName)}_C`; } + +export function fileName(oldFileName: string): string { + return `${NamingStrategy.fileName(oldFileName)}_D`; +}