some logic in mssqldriver

problem with connecting to mssql
This commit is contained in:
Kononnable 2017-03-27 00:11:26 +02:00
parent a93b972f41
commit 35b230731a
6 changed files with 87 additions and 30 deletions

View File

@ -4,19 +4,20 @@ import { AbstractDriver } from "./drivers/AbstractDriver";
* Engine
*/
export class Engine {
constructor(public Options: EngineOptions, private driver:AbstractDriver) {
constructor(private driver: AbstractDriver, public Options: EngineOptions) {
}
public createModelFromDatabase(): boolean {
let entities = this.getEntitiesInfo();
if (entities.length>0) {
public async createModelFromDatabase(): Promise<boolean> {
let entities = await this.getEntitiesInfo(this.Options.databaseName, this.Options.host, this.Options.port, this.Options.user, this.Options.password);
if (entities.length > 0) {
this.createModelFromMetadata(entities);
} else {
console.error('Tables not found in selected database. Skipping creation of typeorm model.');
}
return true;
}
private getEntitiesInfo(): EntityInfo[] {
private getEntitiesInfo(database: string, server: string, port: number, user: string, password: string): EntityInfo[] {
this.driver.GetDataFromServer(database, server, port, user, password)
return [];
}
private createModelFromMetadata(entities: EntityInfo[]) {
@ -24,9 +25,9 @@ export class Engine {
}
}
export interface EngineOptions {
host:string,
port:string,
databaseName:string,
user:string,
password:string
host: string,
port: number,
databaseName: string,
user: string,
password: string
}

View File

@ -2,5 +2,19 @@
* AbstractDriver
*/
export abstract class AbstractDriver {
async GetDataFromServer(database:string,server:string,port:number,user:string,password:string): Promise<EntityInfo[]> {
await this.ConnectToServer(database,server,port,user,password);
let entities = await this.GetAllTables();
await this.GetCoulmnsFromEntity(entities);
await this.GetIndexesFromEntity(entities);
await this.GetForeignKeysFromEntity(entities);
await this.DisconnectFromServer();
return entities;
}
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 GetForeignKeysFromEntity(entity: EntityInfo[]);
abstract async DisconnectFromServer();
}

View File

@ -1,10 +1,50 @@
import {AbstractDriver} from './abstractDriver'
import { AbstractDriver } from './abstractDriver'
import * as MSSQL from 'mssql'
/**
* MssqlDriver
*/
export class MssqlDriver extends AbstractDriver {
constructor() {
super();
async GetAllTables(): Promise<EntityInfo[]> {
let request = new MSSQL.Request(this.Connection)
let response = await request.query("SELECT TABLE_SCHEMA,TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'");
let x=1;
throw new Error('Method not implemented.');
}
GetCoulmnsFromEntity(entity: EntityInfo[]) {
throw new Error('Method not implemented.');
}
GetIndexesFromEntity(entity: EntityInfo[]) {
throw new Error('Method not implemented.');
}
GetForeignKeysFromEntity(entity: EntityInfo[]) {
throw new Error('Method not implemented.');
}
async DisconnectFromServer() {
if (this.Connection)
await this.Connection.close();
}
private Connection: MSSQL.Connection;
async ConnectToServer(database: string, server: string, port: number, user: string, password: string) {
let config: MSSQL.config = {
database: database,
server: server,
port: port,
user: user,
password: password,
// driver: 'msnodesqlv8'
}
this.Connection = new MSSQL.Connection(config)
try {
var con:any = this.Connection;
let x = await con.connect("mssql://sa:password@localhost/AdventureWorksDW2014")
// await this.Connection.connect( (err)=>{console.log(err);console.log('a');})
} catch (error) {
//TODO: errors on Connection
console.error(error);
process.abort();
}
}
}

View File

@ -44,27 +44,27 @@ var argv = Yargs
var driver: AbstractDriver;
var standardPort:string;
var standardPort: number;
switch (argv.e) {
case 'mssql':
driver = new MssqlDriver();
standardPort=''
standardPort = 1433;
break;
default:
driver = new MssqlDriver();
standardPort=''
break;
console.error('Database engine not recognized.')
process.abort();
throw new Error('Database engine not recognized.');
}
let engine = new Engine(
{
host:argv.h,
port:argv.p || standardPort,
databaseName:argv.d,
user:argv.u,
password:argv.x
}, driver);
// if(Yargs.)
engine.createModelFromDatabase();
driver,{
host: argv.h,
port: parseInt(argv.p) || standardPort,
databaseName: argv.d,
user: argv.u,
password: argv.x
});
engine.createModelFromDatabase().then( ()=>{
process.abort();
})

View File

@ -2,6 +2,7 @@
* EntityInfo
*/
interface EntityInfo {
EntityName:String;
Columns:ColumnInfo[];
Indexes:IndexInfo[];
}

View File

@ -3,6 +3,7 @@
"mustache": "registry:dt/mustache#0.8.2+20160510002910"
},
"dependencies": {
"mssql": "registry:dt/mssql#3.3.0+20170311011547",
"yargs": "registry:npm/yargs#5.0.0+20160907000723"
}
}