From 8f4520bb252fc6933d7c69ec49584c36f26574d0 Mon Sep 17 00:00:00 2001 From: Kononnable Date: Sat, 8 Apr 2017 00:34:50 +0200 Subject: [PATCH] added @PrimaryGeneratedColumn, nullable, length on columns --- src/drivers/AbstractDriver.ts | 6 ++++-- src/drivers/MssqlDriver.ts | 22 ++++++++++++++++++++-- src/entity.mst | 13 ++++++------- src/models/ColumnInfo.ts | 3 ++- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/drivers/AbstractDriver.ts b/src/drivers/AbstractDriver.ts index 29fdf15..d1b3958 100644 --- a/src/drivers/AbstractDriver.ts +++ b/src/drivers/AbstractDriver.ts @@ -10,12 +10,14 @@ export abstract class AbstractDriver { await this.GetIndexesFromEntity(dbModel.entities); dbModel.relations = await this.GetRelations(); await this.DisconnectFromServer(); + this.FindPrimaryColumnsFromIndexes(dbModel) return dbModel; } abstract async ConnectToServer(database:string,server:string,port:number,user:string,password:string); abstract async GetAllTables(): Promise - abstract async GetCoulmnsFromEntity(entity: EntityInfo[]); - abstract async GetIndexesFromEntity(entity: EntityInfo[]); + abstract async GetCoulmnsFromEntity(entities: EntityInfo[]); + abstract async GetIndexesFromEntity(entities: EntityInfo[]); abstract async GetRelations():Promise; + abstract async FindPrimaryColumnsFromIndexes(dbModel:DatabaseModel); abstract async DisconnectFromServer(); } \ No newline at end of file diff --git a/src/drivers/MssqlDriver.ts b/src/drivers/MssqlDriver.ts index ef1fc33..f0c88d5 100644 --- a/src/drivers/MssqlDriver.ts +++ b/src/drivers/MssqlDriver.ts @@ -4,6 +4,23 @@ import * as MSSQL from 'mssql' * MssqlDriver */ export class MssqlDriver extends AbstractDriver { + FindPrimaryColumnsFromIndexes(dbModel: DatabaseModel) { + dbModel.entities.forEach(entity => { + let primaryIndex = entity.Indexes.find(v=>v.isPrimaryKey); + if (!primaryIndex){ + console.error(`Table ${entity.EntityName} has no PK.`) + return; + } + let pIndex=primaryIndex //typescript error? pIndex:IndexInfo; primaryIndex:IndexInfo|undefined + let Column = entity.Columns.find(col=>col.name==pIndex.columns[0].name) + if(!Column){ + console.error(`Not found ${entity.EntityName} PK column.`) + return; + } + Column.isPrimary=true; + }); + } + async GetAllTables(): Promise { let request = new MSSQL.Request(this.Connection) let response: { TABLE_SCHEMA: string, TABLE_NAME: string }[] @@ -99,10 +116,10 @@ export class MssqlDriver extends AbstractDriver { // colInfo.ts_type = "number" // break; default: - console.log("Unknown column type:" + resp.DATA_TYPE); + console.error("Unknown column type:" + resp.DATA_TYPE); break; } - colInfo.char_max_lenght = resp.CHARACTER_MAXIMUM_LENGTH; + colInfo.char_max_lenght = resp.CHARACTER_MAXIMUM_LENGTH>0?resp.CHARACTER_MAXIMUM_LENGTH:null; if (colInfo.sql_type) ent.Columns.push(colInfo); }) }) @@ -157,6 +174,7 @@ ORDER BY indexColumnInfo.isIncludedColumn = resp.is_included_column == 1 ? true : false; indexColumnInfo.isDescending = resp.is_descending_key == 1 ? true : false; indexInfo.columns.push(indexColumnInfo); + }) }) return entities; diff --git a/src/entity.mst b/src/entity.mst index 1ba2432..e8c9dcb 100644 --- a/src/entity.mst +++ b/src/entity.mst @@ -1,14 +1,13 @@ -import {Index,Entity, PrimaryColumn, Column, OneToMany, ManyToOne, JoinTable} from "typeorm"; +import {Index,Entity, PrimaryColumn,PrimaryGeneratedColumn, Column, OneToMany, ManyToOne, JoinTable} from "typeorm"; @Entity() -{{#Indexes}} -@Index("{{name}}",[{{#columns}}"{{name}}",{{/columns}}]{{#isUnique}},{unique:true}{{/isUnique}}) -{{/Indexes}} -export class {{EntityName}} { +{{#Indexes}}{{^isPrimaryKey}}@Index("{{name}}",[{{#columns}}"{{name}}",{{/columns}}]{{#isUnique}},{unique:true}{{/isUnique}}) +{{/isPrimaryKey}}{{/Indexes}}export class {{EntityName}} { {{#Columns}} - @Column("{{sql_type}}") - {{name}}:{{ts_type}}; + {{^isPrimary}}@Column("{{sql_type}}",{ {{#is_nullable}}nullable:true,{{/is_nullable}}{{#char_max_lenght}}length:{{char_max_lenght}},{{/char_max_lenght}}}) + {{/isPrimary}}{{#isPrimary}}@PrimaryGeneratedColumn("{{sql_type}}") + {{/isPrimary}}{{name}}:{{ts_type}}; {{/Columns}} } \ No newline at end of file diff --git a/src/models/ColumnInfo.ts b/src/models/ColumnInfo.ts index df31896..c9f4f71 100644 --- a/src/models/ColumnInfo.ts +++ b/src/models/ColumnInfo.ts @@ -8,7 +8,8 @@ interface ColumnInfo { 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, + char_max_lenght: number|null, + isPrimary:boolean }