added self referencing tables support
This commit is contained in:
parent
9f5d0eee03
commit
7ef9bb109c
@ -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')
|
||||
|
@ -1,3 +1,5 @@
|
||||
import {EntityInfo} from './../models/EntityInfo'
|
||||
import {DatabaseModel} from './../models/DatabaseModel'
|
||||
/**
|
||||
* AbstractDriver
|
||||
*/
|
||||
|
@ -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[] = <EntityInfo[]>[];
|
||||
response.forEach((val) => {
|
||||
let ent: EntityInfo = <EntityInfo>{};
|
||||
let ent: EntityInfo = new EntityInfo();
|
||||
ent.EntityName = val.TABLE_NAME;
|
||||
ent.Columns = <ColumnInfo[]>[];
|
||||
ent.Indexes = <IndexInfo[]>[];
|
||||
@ -43,9 +46,8 @@ export class MssqlDriver extends AbstractDriver {
|
||||
response.filter((filterVal) => {
|
||||
return filterVal.TABLE_NAME == ent.EntityName;
|
||||
}).forEach((resp) => {
|
||||
let colInfo: ColumnInfo = <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) {
|
||||
|
@ -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}})
|
||||
|
@ -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=[];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -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 '';
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user