naming strategy for entity file names(#236)
This commit is contained in:
parent
cc077dc5ac
commit
c011ed3957
@ -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;
|
||||
}
|
||||
|
@ -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");
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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: [],
|
||||
|
@ -64,6 +64,7 @@ export default class SqliteDriver extends AbstractDriver {
|
||||
relationIds: [],
|
||||
sqlName: val.tbl_name,
|
||||
tscName: val.tbl_name,
|
||||
fileName: val.tbl_name,
|
||||
fileImports: [],
|
||||
});
|
||||
});
|
||||
|
@ -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;
|
||||
};
|
||||
|
@ -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}} })
|
||||
|
@ -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);
|
||||
|
@ -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`;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user