enumerating all columns and indexes

This commit is contained in:
Kononnable 2017-03-29 23:10:59 +02:00
parent 9f866fa99f
commit 29ab0e18cd
5 changed files with 94 additions and 13 deletions

View File

@ -4,6 +4,7 @@
"description": "Generates models for TypeORM from existing databases.",
"scripts": {
"tsc:w": "tsc -w",
"tsc": "tsc",
"typings-install": "typings install",
"setup": "npm install && npm run typings-install",
"start": "tsc && node ./src/index.js"

View File

@ -6,23 +6,91 @@ import * as MSSQL from 'mssql'
export class MssqlDriver extends AbstractDriver {
async GetAllTables(): Promise<EntityInfo[]> {
let request = new MSSQL.Request(this.Connection)
let response: { TABLE_SCHEMA: 'string', TABLE_NAME: 'string' }[]
let response: { TABLE_SCHEMA: string, TABLE_NAME: string }[]
= 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>{};
ent.EntityName=val.TABLE_NAME;
let ret: EntityInfo[] = <EntityInfo[]>[];
response.forEach((val) => {
let ent: EntityInfo = <EntityInfo>{};
ent.EntityName = val.TABLE_NAME;
ent.Columns = <ColumnInfo[]>[];
ent.Indexes = <IndexInfo[]>[];
ret.push(ent);
})
return ret;
}
GetCoulmnsFromEntity(entity: EntityInfo[]) {
throw new Error('Method not implemented.');
async GetCoulmnsFromEntity(entities: EntityInfo[]) {
let request = new MSSQL.Request(this.Connection)
let response: { TABLE_NAME: string, COLUMN_NAME: string, COLUMN_DEFAULT: string, IS_NULLABLE: string, DATA_TYPE: string, CHARACTER_MAXIMUM_LENGTH: number }[]
= await request.query("SELECT TABLE_NAME,COLUMN_NAME,COLUMN_DEFAULT,IS_NULLABLE,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH FROM INFORMATION_SCHEMA.COLUMNS");
entities.forEach((ent) => {
response.filter((filterVal) => {
return filterVal.TABLE_NAME == ent.EntityName;
}).forEach((resp) => {
let colInfo: ColumnInfo = <ColumnInfo>{};
colInfo.name = resp.COLUMN_NAME;
colInfo.is_nullable = resp.IS_NULLABLE == 'YES' ? true : false;
colInfo.default = resp.COLUMN_DEFAULT;
colInfo.data_type = resp.DATA_TYPE;//TODO:Parse to Typeorm types
colInfo.char_max_lenght = resp.CHARACTER_MAXIMUM_LENGTH;
ent.Columns.push(colInfo);
})
})
return entities;
}
GetIndexesFromEntity(entity: EntityInfo[]) {
throw new Error('Method not implemented.');
async GetIndexesFromEntity(entities: EntityInfo[]) {
let request = new MSSQL.Request(this.Connection)
let response: {
TableName: string, IndexName: string, ColumnName: string, is_unique: number,
is_primary_key: number, is_descending_key: number, is_included_column: number
}[]
= await request.query(`SELECT
TableName = t.name,
IndexName = ind.name,
ColumnName = col.name,
ind.is_unique,
ind.is_primary_key,
ic.is_descending_key,
ic.is_included_column
FROM
sys.indexes ind
INNER JOIN
sys.index_columns ic ON ind.object_id = ic.object_id and ind.index_id = ic.index_id
INNER JOIN
sys.columns col ON ic.object_id = col.object_id and ic.column_id = col.column_id
INNER JOIN
sys.tables t ON ind.object_id = t.object_id
WHERE
t.is_ms_shipped = 0
ORDER BY
t.name, ind.name, ind.index_id, ic.key_ordinal;`);
entities.forEach((ent) => {
response.filter((filterVal) => {
return filterVal.TableName == ent.EntityName;
}).forEach((resp) => {
let indexInfo: IndexInfo = <IndexInfo>{};
let indexColumnInfo: IndexColumnInfo = <IndexColumnInfo>{};
if (ent.Indexes.filter((filterVal) => {
return filterVal.name == resp.IndexName
}).length > 0) {
indexInfo = ent.Indexes.filter((filterVal) => {
return filterVal.name == resp.IndexName
})[0];
} else {
indexInfo.columns = <IndexColumnInfo[]>[];
indexInfo.name = resp.IndexName;
indexInfo.isUnique = resp.is_unique == 1 ? true : false;
indexInfo.isPrimaryKey = resp.is_primary_key == 1 ? true : false;
ent.Indexes.push(indexInfo);
}
indexColumnInfo.name = resp.ColumnName;
indexColumnInfo.isIncludedColumn = resp.is_included_column == 1 ? true : false;
indexColumnInfo.isDescending = resp.is_descending_key == 1 ? true : false;
indexInfo.columns.push(indexColumnInfo);
})
})
return entities;
}
GetForeignKeysFromEntity(entity: EntityInfo[]) {
GetForeignKeysFromEntity(entities: EntityInfo[]) {
throw new Error('Method not implemented.');
}
async DisconnectFromServer() {

View File

@ -2,5 +2,9 @@
* ColumnInfo
*/
interface ColumnInfo {
name:string,
default:string,
is_nullable:boolean,
data_type:string,
char_max_lenght:number
}

View File

@ -0,0 +1,5 @@
interface IndexColumnInfo{
name:string,
isDescending:boolean,
isIncludedColumn:boolean
}

View File

@ -2,5 +2,8 @@
* IndexInfo
*/
interface IndexInfo {
}
name:string,
columns:IndexColumnInfo[],
isUnique:boolean,
isPrimaryKey:boolean,
}