added @PrimaryGeneratedColumn, nullable, length on columns

This commit is contained in:
Kononnable 2017-04-08 00:34:50 +02:00
parent fcede22b48
commit 8f4520bb25
4 changed files with 32 additions and 12 deletions

View File

@ -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<EntityInfo[]>
abstract async GetCoulmnsFromEntity(entity: EntityInfo[]);
abstract async GetIndexesFromEntity(entity: EntityInfo[]);
abstract async GetCoulmnsFromEntity(entities: EntityInfo[]);
abstract async GetIndexesFromEntity(entities: EntityInfo[]);
abstract async GetRelations():Promise<RelationInfo[]>;
abstract async FindPrimaryColumnsFromIndexes(dbModel:DatabaseModel);
abstract async DisconnectFromServer();
}

View File

@ -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<EntityInfo[]> {
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;

View File

@ -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}}
}

View File

@ -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
}