Skip to content
This repository has been archived by the owner on Oct 10, 2022. It is now read-only.

Commit

Permalink
Support skip-relationships
Browse files Browse the repository at this point in the history
Because currently there is a bug from TypeORM throwing execption when
using abstract class with relationship embedded-columns
typeorm/typeorm#3132
  • Loading branch information
ivawzh committed Aug 10, 2020
1 parent 692c5d0 commit b2ebeb2
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 4,603 deletions.
2 changes: 2 additions & 0 deletions src/IGenerationOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default interface IGenerationOptions {
propertyVisibility: "public" | "protected" | "private" | "none";
lazy: boolean;
activeRecord: boolean;
skipRelationships: boolean;
extendAbstractClass: string;
generateConstructor: boolean;
customNamingStrategyPath: string;
Expand Down Expand Up @@ -44,6 +45,7 @@ export function getDefaultGenerationOptions(): IGenerationOptions {
propertyVisibility: "none",
lazy: false,
activeRecord: false,
skipRelationships: false,
extendAbstractClass: "",
generateConstructor: false,
customNamingStrategyPath: "",
Expand Down
3 changes: 3 additions & 0 deletions src/ModelCustomization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ function addImportsAndGenerationOptions(
if (generationOptions.activeRecord) {
entity.activeRecord = true;
}
if (generationOptions.skipRelationships) {
entity.skipRelationships = true;
}
if (generationOptions.extendAbstractClass) {
entity.extendAbstractClass = generationOptions.extendAbstractClass;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ModelGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ function createHandlebarsHelpers(generationOptions: IGenerationOptions): void {
}
);
Handlebars.registerHelper("defaultExport", () =>
generationOptions.exportType === "default" ? "default" : ""
generationOptions.exportType === "default" ? " default" : ""
);
Handlebars.registerHelper("localImport", (entityName: string) =>
generationOptions.exportType === "default"
Expand Down
27 changes: 22 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,24 @@ function checkYargsParameters(options: options): options {
default: options.generationOptions.activeRecord,
describe: "Use ActiveRecord syntax for generated models",
},
skipRelationships: {
alias: "skip-relationships",
boolean: true,
default: options.generationOptions.skipRelationships,
describe: "Skip relationship declarations",
},
extendAbstractClass: {
alias: "extend-abstract-class",
string: true,
default: options.generationOptions.extendAbstractClass,
describe: "Make generated models extend a custom abstract class",
},
exportAbstractClass: {
alias: "export-abstract-class",
boolean: true,
default: options.generationOptions.exportAbstractClass,
describe: "Export generated models as abstract classes",
},
namingStrategy: {
describe: "Use custom naming strategy",
default: options.generationOptions.customNamingStrategyPath,
Expand Down Expand Up @@ -300,11 +312,6 @@ function checkYargsParameters(options: options): options {
default: options.generationOptions.exportType === "default",
describe: "Generate index file",
},
exportAbstractClass: {
boolean: true,
default: options.generationOptions.exportAbstractClass,
describe: "Export generated models as abstract classes",
},
});

options.connectionOptions.databaseName = argv.d;
Expand All @@ -327,6 +334,7 @@ function checkYargsParameters(options: options): options {
}
options.connectionOptions.skipTables = skipTables;
options.generationOptions.activeRecord = argv.a;
options.generationOptions.skipRelationships = argv.skipRelationships;
options.generationOptions.extendAbstractClass = argv.extendAbstractClass;
options.generationOptions.generateConstructor = argv.generateConstructor;
options.generationOptions.convertCaseEntity = argv.ce as IGenerationOptions["convertCaseEntity"];
Expand Down Expand Up @@ -520,6 +528,12 @@ async function useInquirer(options: options): Promise<options> {
value: "activeRecord",
checked: options.generationOptions.activeRecord,
},
{
name: "Skip relationship declarations",
value: "skipRelationships",
checked:
options.generationOptions.skipRelationships,
},
{
name:
"Generated models extend a custom abstract class",
Expand Down Expand Up @@ -635,6 +649,9 @@ async function useInquirer(options: options): Promise<options> {
options.generationOptions.activeRecord = customizations.includes(
"activeRecord"
);
options.generationOptions.skipRelationships = customizations.includes(
"skipRelationships"
);
if (customizations.includes("extendAbstractClass")) {
const { extendAbstractClass } = await inquirer.prompt([
{
Expand Down
1 change: 1 addition & 0 deletions src/models/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type Entity = {
// TODO: move to sub-object or use handlebars helpers(?)
fileImports: string[];
activeRecord?: true;
skipRelationships?: boolean;
extendAbstractClass?: string;
generateSuper?: boolean;
generateConstructor?: true;
Expand Down
6 changes: 5 additions & 1 deletion src/templates/entity.mst
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ import {{localImport (toEntityName .)}} from './{{toFileName .}}'
{ name: "{{name}}", referencedColumnName: "{{toPropertyName referencedColumnName}}" },
{{/inline}}
{{#*inline "Relation"}}
{{#unless skipRelationships}}

@{{relationType}}(()=>{{toEntityName relatedTable}},{{toPropertyName relatedTable}}=>{{toPropertyName relatedTable}}.{{toPropertyName relatedField}}{{#if relationOptions}},{ {{json relationOptions}} }{{/if}})
{{#if joinColumnOptions}}@JoinColumn([{{#joinColumnOptions}}{{> JoinColumnOptions}}{{/joinColumnOptions}}]){{/if}}
{{#joinTableOptions}}@JoinTable({ name:"{{name}}", joinColumns:[{{#joinColumns}}{{> JoinColumnOptions}}{{/joinColumns}}],inverseJoinColumns:[{{#inverseJoinColumns}}{{> JoinColumnOptions}}{{/inverseJoinColumns}}],{{#database}}database:"{{.}}",{{/database}}{{#schema}}schema:"{{.}}"{{/schema}} }){{/joinTableOptions}}
{{printPropertyVisibility}}{{toPropertyName fieldName}}{{strictMode}}:{{toRelation (toEntityName relatedTable) relationType}};

{{/unless}}
{{/inline}}

{{#*inline "RelationId"}}
@RelationId(({{toPropertyName entityName}}:{{toEntityName entityName}})=>{{toPropertyName entityName}}.{{toPropertyName relationField}})
{{printPropertyVisibility}}{{toPropertyName fieldName}}{{strictMode}}:{{fieldType}};
Expand All @@ -33,7 +37,7 @@ import {{localImport (toEntityName .)}} from './{{toFileName .}}'
{{#*inline "Entity"}}
{{#indices}}{{> Index}}{{/indices~}}
{{#unless exportAbstractClass}}@Entity("{{sqlName}}"{{#schema}} ,{schema:"{{.}}"{{#if ../database}}, database:"{{../database}}"{{/if}} } {{/schema}}){{/unless}}
export{{#if defaultExport}} {{.}}{{/if}}{{#exportAbstractClass}} abstract{{/exportAbstractClass}} class {{toEntityName tscName}}{{#activeRecord}} extends BaseEntity{{/activeRecord}}{{#extendAbstractClass}} extends BaseClass{{/extendAbstractClass}} {
export{{defaultExport}}{{#exportAbstractClass}} abstract{{/exportAbstractClass}} class {{toEntityName tscName}}{{#activeRecord}} extends BaseEntity{{/activeRecord}}{{#extendAbstractClass}} extends BaseClass{{/extendAbstractClass}} {

{{#columns}}{{> Column}}{{/columns~}}
{{#relations}}{{> Relation}}{{/relations~}}
Expand Down
36 changes: 36 additions & 0 deletions test/modelCustomization/modelCustomization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,42 @@ describe("Model customization phase", async () => {

compileGeneratedModel(generationOptions.resultsPath, [""]);
});
it("skipRelationships", async () => {
const data = generateSampleData();
const generationOptions = generateGenerationOptions();
clearGenerationDir();

generationOptions.skipRelationships = true;
const customizedModel = modelCustomizationPhase(
data,
generationOptions,
{}
);
modelGenerationPhase(
getDefaultConnectionOptions(),
generationOptions,
customizedModel
);
const filesGenPath = path.resolve(resultsPath, "entities");
const postContent = fs
.readFileSync(path.resolve(filesGenPath, "Post.ts"))
.toString();
const postAuthorContent = fs
.readFileSync(path.resolve(filesGenPath, "PostAuthor.ts"))
.toString();
expect(postContent).to.not.have.oneOf([
`@ManyToOne`,
`@OneToMany`,
`@JoinColumn`,
]);
expect(postAuthorContent).to.not.have.oneOf([
`@ManyToOne`,
`@OneToMany`,
`@JoinColumn`,
]);

compileGeneratedModel(generationOptions.resultsPath, [""]);
});
it("extendAbstractClass", async () => {
const data = generateSampleData();
const generationOptions = generateGenerationOptions();
Expand Down
Loading

0 comments on commit b2ebeb2

Please sign in to comment.