From f701f6974f1ab92e28e6b892a8abdf4fc2c9c51a Mon Sep 17 00:00:00 2001 From: Maia Everett Date: Sat, 9 Nov 2019 16:33:36 +0700 Subject: [PATCH] Add joinColumn and inverseJoinColumn to @JoinTable properties (fixes #183) --- src/drivers/AbstractDriver.ts | 23 +++++++++++++++++++---- src/entity.mst | 2 +- src/models/RelationInfo.ts | 4 ++++ 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/drivers/AbstractDriver.ts b/src/drivers/AbstractDriver.ts index 171444b..5188d62 100644 --- a/src/drivers/AbstractDriver.ts +++ b/src/drivers/AbstractDriver.ts @@ -91,10 +91,15 @@ export default abstract class AbstractDriver { ); manyToManyEntities.forEach(entity => { let relations: RelationInfo[] = []; - relations = entity.Columns.reduce( - (prev: RelationInfo[], curr) => prev.concat(curr.relations), - relations - ); + const joinColumnMap = new Map(); + + entity.Columns.forEach(column => { + column.relations.forEach(relation => { + joinColumnMap.set(relation.relatedTable, column.tsName); + relations.push(relation); + }); + }); + const namesOfRelatedTables = relations .map(v => v.relatedTable) .filter((v, i, s) => s.indexOf(v) === i); @@ -138,6 +143,11 @@ export default abstract class AbstractDriver { col1Rel.isOwner = true; col1Rel.ownerColumn = firstRelatedTable; + col1Rel.joinColumn = joinColumnMap.get(namesOfRelatedTables[0]); + col1Rel.inverseJoinColumn = joinColumnMap.get( + namesOfRelatedTables[1] + ); + column1.relations.push(col1Rel); relatedTable1.Columns.push(column1); @@ -148,6 +158,11 @@ export default abstract class AbstractDriver { col2Rel.relatedTable = firstRelatedTable; col2Rel.relatedColumn = secondRelatedTable; + col2Rel.joinColumn = joinColumnMap.get(namesOfRelatedTables[1]); + col2Rel.inverseJoinColumn = joinColumnMap.get( + namesOfRelatedTables[0] + ); + col2Rel.relationType = "ManyToMany"; col2Rel.isOwner = false; column2.relations.push(col2Rel); diff --git a/src/entity.mst b/src/entity.mst index 49bd95f..8b333e8 100644 --- a/src/entity.mst +++ b/src/entity.mst @@ -27,7 +27,7 @@ import {BaseEntity,Column,Entity,Index,JoinColumn,JoinTable,ManyToMany,ManyToOne {{printPropertyVisibility}}{{toPropertyName tsName}}{{strictMode}}:{{tsType}}{{#options/nullable}} | null{{/options/nullable}}; {{/relations}}{{#relations}} @{{relationType}}(()=>{{toEntityName relatedTable}}, ({{toPropertyName relatedTable}}: {{toEntityName relatedTable}})=>{{toPropertyName relatedTable}}.{{#if isOwner}}{{toPropertyName ownerColumn}},{ {{#../options/primary}}primary:true,{{/../options/primary}}{{^../options/nullable}} nullable:false,{{/../options/nullable}}{{#actionOnDelete}}onDelete: '{{.}}',{{/actionOnDelete}}{{#actionOnUpdate}}onUpdate: '{{.}}'{{/actionOnUpdate}} }{{else}}{{toPropertyName relatedColumn}}{{#if (or actionOnDelete actionOnUpdate ) }},{ {{#actionOnDelete}}onDelete: '{{.}}' ,{{/actionOnDelete}}{{#actionOnUpdate}}onUpdate: '{{.}}'{{/actionOnUpdate}} }{{/if}}{{/if}}){{#isOwner}} - {{#if isManyToMany}}@JoinTable({ name:'{{ ../options/name}}'}){{else}}@JoinColumn({ name:'{{ ../options/name}}'}){{/if}}{{/isOwner}} + {{#if isManyToMany}}@JoinTable({ name:'{{ ../options/name}}'{{#if joinColumn}},joinColumn:{name:'{{joinColumn}}'}{{/if}}{{#if inverseJoinColumn}},inverseJoinColumn:{name:'{{inverseJoinColumn}}'}{{/if}} }){{else}}@JoinColumn({ name:'{{ ../options/name}}'}){{/if}}{{/isOwner}} {{#if (or isOneToMany isManyToMany)}}{{printPropertyVisibility}}{{toPropertyName ../tsName}}{{strictMode}}:{{toLazy (concat (toEntityName relatedTable) "[]")}}; {{else}}{{printPropertyVisibility}}{{toPropertyName ../tsName}}{{strictMode}}:{{toLazy (concat (toEntityName relatedTable) ' | null')}}; {{/if}} diff --git a/src/models/RelationInfo.ts b/src/models/RelationInfo.ts index efaa10e..d9a0b17 100644 --- a/src/models/RelationInfo.ts +++ b/src/models/RelationInfo.ts @@ -11,6 +11,10 @@ export default class RelationInfo { public ownerColumn: string; + public joinColumn?: string; + + public inverseJoinColumn?: string; + public actionOnDelete: | "RESTRICT" | "CASCADE"