From 9f5d0eee03eed3fcb3bd3b7c4e073c2d318bf9dd Mon Sep 17 00:00:00 2001 From: Kononnable Date: Wed, 26 Apr 2017 16:17:00 +0200 Subject: [PATCH] added multiple relations on single column --- src/drivers/MssqlDriver.ts | 11 ++++++----- src/entity.mst | 8 ++++---- src/models/ColumnInfo.ts | 2 +- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/drivers/MssqlDriver.ts b/src/drivers/MssqlDriver.ts index dbc25a8..0ea206b 100644 --- a/src/drivers/MssqlDriver.ts +++ b/src/drivers/MssqlDriver.ts @@ -45,6 +45,7 @@ export class MssqlDriver extends AbstractDriver { }).forEach((resp) => { let colInfo: ColumnInfo = {}; colInfo.name = resp.COLUMN_NAME; + colInfo.relations=[]; colInfo.is_nullable = resp.IS_NULLABLE == 'YES' ? true : false; colInfo.default = resp.COLUMN_DEFAULT; switch (resp.DATA_TYPE) { @@ -276,21 +277,21 @@ order by isOneToMany=false; } - ownerColumn.relation={ + ownerColumn.relations.push({ actionOnDelete:relationTmp.actionOnDelete, isOwner:true, relatedColumn:relatedColumn.name, relatedTable:relationTmp.referencedTable, relationType:isOneToMany?"OneToMany":"OneToOne" - } - relatedColumn.relation={ + }) + relatedColumn.relations.push({ actionOnDelete:relationTmp.actionOnDelete, isOwner:false, relatedColumn:ownerColumn.name, relatedTable:relationTmp.ownerTable, relationType:isOneToMany?"ManyToOne":"OneToOne" - } - + }) + }) return entities; } diff --git a/src/entity.mst b/src/entity.mst index e8063b4..e2351ee 100644 --- a/src/entity.mst +++ b/src/entity.mst @@ -1,6 +1,6 @@ import {Index,Entity, PrimaryColumn, Column, OneToOne, OneToMany, ManyToOne, JoinTable} from "typeorm"; -{{#Columns}}{{#relation}}import{ {{relatedTable}} } from "./{{relatedTable}}"; -{{/relation}}{{/Columns}} +{{#Columns}}{{#relations}}import{ {{relatedTable}} } from "./{{relatedTable}}"; +{{/relations}}{{/Columns}} @Entity() {{#Indexes}}{{^isPrimaryKey}}@Index("{{name}}",[{{#columns}}"{{name}}",{{/columns}}]{{#isUnique}},{unique:true}{{/isUnique}}) @@ -8,9 +8,9 @@ import {Index,Entity, PrimaryColumn, Column, OneToOne, OneToMany, ManyToOne, Joi {{#Columns}} - @Column("{{sql_type}}",{ {{#is_nullable}}nullable:true,{{/is_nullable}}{{#char_max_lenght}}length:{{char_max_lenght}},{{/char_max_lenght}}{{#default}}default:{{default}},{{/default}}{{#numericPrecision}}precision:{numericPrecision},{{/numericPrecision}}{{#numericScale}}scale:{{numericScale}},{{/numericScale}}{{#isPrimary}}primary:{{isPrimary}},{{/isPrimary}}}){{#relation}} + @Column("{{sql_type}}",{ {{#is_nullable}}nullable:true,{{/is_nullable}}{{#char_max_lenght}}length:{{char_max_lenght}},{{/char_max_lenght}}{{#default}}default:{{default}},{{/default}}{{#numericPrecision}}precision:{numericPrecision},{{/numericPrecision}}{{#numericScale}}scale:{{numericScale}},{{/numericScale}}{{#isPrimary}}primary:{{isPrimary}},{{/isPrimary}}}){{#relations}} @{{relationType}}(type=>{{relatedTable}},x=>x.{{relatedColumn}}){{#isOwner}} - @JoinTable(){{/isOwner}}{{/relation}} + @JoinTable(){{/isOwner}}{{/relations}} {{name}}:{{ts_type}}; {{/Columns}} } \ No newline at end of file diff --git a/src/models/ColumnInfo.ts b/src/models/ColumnInfo.ts index 1cacc36..e694469 100644 --- a/src/models/ColumnInfo.ts +++ b/src/models/ColumnInfo.ts @@ -12,7 +12,7 @@ interface ColumnInfo { isPrimary:boolean, numericPrecision:number|null, numericScale:number|null, - relation:RelationInfo + relations:RelationInfo[] }