From 7ef9bb109cbd83fa39e2e865360de06e369f2b60 Mon Sep 17 00:00:00 2001 From: Kononnable Date: Wed, 26 Apr 2017 19:05:12 +0200 Subject: [PATCH] added self referencing tables support --- src/Engine.ts | 1 + src/drivers/AbstractDriver.ts | 2 ++ src/drivers/MssqlDriver.ts | 8 +++++--- src/entity.mst | 3 +-- src/models/ColumnInfo.ts | 27 ++++++++++++++++----------- src/models/DatabaseModel.ts | 12 ++++++++++-- src/models/EntityInfo.ts | 30 ++++++++++++++++++++++++++---- 7 files changed, 61 insertions(+), 22 deletions(-) diff --git a/src/Engine.ts b/src/Engine.ts index 0f1fd20..a220b12 100644 --- a/src/Engine.ts +++ b/src/Engine.ts @@ -1,5 +1,6 @@ import { MssqlDriver } from './drivers/MssqlDriver' import { AbstractDriver } from "./drivers/AbstractDriver"; +import {DatabaseModel} from './models/DatabaseModel' import * as Mustache from 'mustache' import fs = require('fs'); import path = require('path') diff --git a/src/drivers/AbstractDriver.ts b/src/drivers/AbstractDriver.ts index b9ff832..6a28051 100644 --- a/src/drivers/AbstractDriver.ts +++ b/src/drivers/AbstractDriver.ts @@ -1,3 +1,5 @@ +import {EntityInfo} from './../models/EntityInfo' +import {DatabaseModel} from './../models/DatabaseModel' /** * AbstractDriver */ diff --git a/src/drivers/MssqlDriver.ts b/src/drivers/MssqlDriver.ts index 0ea206b..98f603f 100644 --- a/src/drivers/MssqlDriver.ts +++ b/src/drivers/MssqlDriver.ts @@ -1,5 +1,8 @@ import { AbstractDriver } from './abstractDriver' import * as MSSQL from 'mssql' +import {ColumnInfo} from './../models/ColumnInfo' +import {EntityInfo} from './../models/EntityInfo' +import {DatabaseModel} from './../models/DatabaseModel' /** * MssqlDriver */ @@ -24,7 +27,7 @@ export class MssqlDriver extends AbstractDriver { = await request.query("SELECT TABLE_SCHEMA,TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'"); let ret: EntityInfo[] = []; response.forEach((val) => { - let ent: EntityInfo = {}; + let ent: EntityInfo = new EntityInfo(); ent.EntityName = val.TABLE_NAME; ent.Columns = []; ent.Indexes = []; @@ -43,9 +46,8 @@ export class MssqlDriver extends AbstractDriver { response.filter((filterVal) => { return filterVal.TABLE_NAME == ent.EntityName; }).forEach((resp) => { - let colInfo: ColumnInfo = {}; + let colInfo: ColumnInfo = new 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) { diff --git a/src/entity.mst b/src/entity.mst index e2351ee..18cbb7a 100644 --- a/src/entity.mst +++ b/src/entity.mst @@ -1,6 +1,5 @@ import {Index,Entity, PrimaryColumn, Column, OneToOne, OneToMany, ManyToOne, JoinTable} from "typeorm"; -{{#Columns}}{{#relations}}import{ {{relatedTable}} } from "./{{relatedTable}}"; -{{/relations}}{{/Columns}} +{{{relationImports}}} @Entity() {{#Indexes}}{{^isPrimaryKey}}@Index("{{name}}",[{{#columns}}"{{name}}",{{/columns}}]{{#isUnique}},{unique:true}{{/isUnique}}) diff --git a/src/models/ColumnInfo.ts b/src/models/ColumnInfo.ts index e694469..6eaa028 100644 --- a/src/models/ColumnInfo.ts +++ b/src/models/ColumnInfo.ts @@ -1,18 +1,23 @@ /** * ColumnInfo */ -interface ColumnInfo { - name: string, - default: string|null, - is_nullable: boolean, - ts_type: 'number' | 'string' | 'boolean', +export class ColumnInfo { + name: string; + default: string|null; + is_nullable: boolean; + ts_type: 'number' | 'string' | 'boolean'; sql_type: "string" | "text" | "number" | "integer" | "int" | "smallint" | "bigint" | - "float" | "double" | "decimal" | "date" | "time" | "datetime" | "boolean" | "json", - char_max_lenght: number|null, - isPrimary:boolean, - numericPrecision:number|null, - numericScale:number|null, - relations:RelationInfo[] + "float" | "double" | "decimal" | "date" | "time" | "datetime" | "boolean" | "json"; + char_max_lenght: number|null; + isPrimary:boolean; + numericPrecision:number|null; + numericScale:number|null; + relations:RelationInfo[]; + + constructor() { + this.relations=[]; + } + } diff --git a/src/models/DatabaseModel.ts b/src/models/DatabaseModel.ts index a4ef6e3..c05eaff 100644 --- a/src/models/DatabaseModel.ts +++ b/src/models/DatabaseModel.ts @@ -1,8 +1,16 @@ -interface DatabaseModel { - entities: EntityInfo[], +import {EntityInfo} from './EntityInfo' +export class DatabaseModel { + entities: EntityInfo[]; config: { cascadeInsert: boolean, cascadeUpdate: boolean, cascadeRemove: boolean, } + relationImports():any{ + let that=this; + return function(text, render) { + if ('l'!=render(text)) return `import {${render(text)}} from "./${render(text)}"` + else return ''; + } + } } \ No newline at end of file diff --git a/src/models/EntityInfo.ts b/src/models/EntityInfo.ts index 2704100..68c4a3a 100644 --- a/src/models/EntityInfo.ts +++ b/src/models/EntityInfo.ts @@ -1,8 +1,30 @@ +import { ColumnInfo } from './ColumnInfo' /** * EntityInfo */ -interface EntityInfo { - EntityName:String; - Columns:ColumnInfo[]; - Indexes:IndexInfo[]; +export class EntityInfo { + EntityName: string; + Columns: ColumnInfo[]; + Indexes: IndexInfo[]; + + relationImports(): any { + var returnString = ""; + var imports: string[] = []; + this.Columns.forEach((column) => { + column.relations.forEach( + (relation) => { + if (this.EntityName!=relation.relatedTable) + imports.push(relation.relatedTable); + } + ) + }); + imports.filter(function (elem, index, self) { + return index == self.indexOf(elem); + }).forEach((imp)=>{ + returnString+=`import {${imp}} from './${imp}'\n` + }) + + return returnString; + } + } \ No newline at end of file