#3 Support for connecting over SSL

This commit is contained in:
Kononnable 2017-10-15 18:58:17 +02:00
parent 9a9adf66a9
commit ddec66a863
12 changed files with 90 additions and 45 deletions

View File

@ -13,7 +13,7 @@ services:
- docker
env:
- MSSQL_Skip=0 MSSQL_Host=localhost MSSQL_Port=1433 MSSQL_Username=sa MSSQL_Password=!Passw0rd MSSQL_Database=typeorm_mg POSTGRES_Skip=0 POSTGRES_Host=localhost POSTGRES_Port=5432 POSTGRES_Username=postgres POSTGRES_Password=!Passw0rd POSTGRES_Database=typeorm_mg MYSQL_Skip=0 MYSQL_Host=localhost MYSQL_Port=3306 MYSQL_Username=root MYSQL_Password=!Passw0rd MYSQL_Database=typeorm_mg MARIADB_Skip=0 MARIADB_Host=localhost MARIADB_Port=3307 MARIADB_Username=root MARIADB_Password=!Passw0rd MARIADB_Database=typeorm_mg
- MSSQL_Skip=0 MSSQL_Host=localhost MSSQL_Port=1433 MSSQL_Username=sa MSSQL_Password=!Passw0rd MSSQL_Database=typeorm_mg MSSQL_SSL=1 POSTGRES_Skip=0 POSTGRES_Host=localhost POSTGRES_Port=5432 POSTGRES_Username=postgres POSTGRES_Password=!Passw0rd POSTGRES_Database=typeorm_mg POSTGRES_SSL=0 MYSQL_Skip=0 MYSQL_Host=localhost MYSQL_Port=3306 MYSQL_Username=root MYSQL_Password=!Passw0rd MYSQL_Database=typeorm_mg MYSQL_SSL=1 MARIADB_Skip=0 MARIADB_Host=localhost MARIADB_Port=3307 MARIADB_Username=root MARIADB_Password=!Passw0rd MARIADB_Database=typeorm_mg MARIADB_SSL=0
before_install:
- sudo service mysql stop

5
package-lock.json generated
View File

@ -3958,6 +3958,11 @@
}
}
},
"yn": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/yn/-/yn-2.0.0.tgz",
"integrity": "sha1-5a2ryKz0CPY4X8dklWhMiOavaJo="
},
"zip-object": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/zip-object/-/zip-object-0.1.0.tgz",

View File

@ -31,7 +31,8 @@
"reflect-metadata": "^0.1.10",
"typeorm": "^0.1.0-alpha.50",
"typescript": "^2.5.3",
"yargs": "^9.0.1"
"yargs": "^9.0.1",
"yn": "^2.0.0"
},
"devDependencies": {
"@types/mysql": "0.0.34",

View File

@ -11,7 +11,7 @@ export class Engine {
}
public async createModelFromDatabase(): Promise<boolean> {
let dbModel = await this.getEntitiesInfo(this.Options.databaseName, this.Options.host, this.Options.port, this.Options.user, this.Options.password, this.Options.schemaName);
let dbModel = await this.getEntitiesInfo(this.Options.databaseName, this.Options.host, this.Options.port, this.Options.user, this.Options.password, this.Options.schemaName, this.Options.ssl);
if (dbModel.entities.length > 0) {
this.createModelFromMetadata(dbModel);
} else {
@ -19,8 +19,8 @@ export class Engine {
}
return true;
}
private async getEntitiesInfo(database: string, server: string, port: number, user: string, password: string, schemaName:string): Promise<DatabaseModel> {
return await this.driver.GetDataFromServer(database, server, port, user, password,schemaName)
private async getEntitiesInfo(database: string, server: string, port: number, user: string, password: string, schemaName:string, ssl:boolean): Promise<DatabaseModel> {
return await this.driver.GetDataFromServer(database, server, port, user, password,schemaName,ssl)
}
private createModelFromMetadata(databaseModel: DatabaseModel) {
@ -82,5 +82,6 @@ export interface EngineOptions {
password: string,
resultsPath: string,
databaseType: string,
schemaName:string
schemaName:string,
ssl:boolean
}

View File

@ -4,9 +4,9 @@ import { DatabaseModel } from './../models/DatabaseModel'
* AbstractDriver
*/
export abstract class AbstractDriver {
async GetDataFromServer(database: string, server: string, port: number, user: string, password: string, schema:string): Promise<DatabaseModel> {
async GetDataFromServer(database: string, server: string, port: number, user: string, password: string, schema:string, ssl:boolean): Promise<DatabaseModel> {
let dbModel = <DatabaseModel>{};
await this.ConnectToServer(database, server, port, user, password);
await this.ConnectToServer(database, server, port, user, password,ssl);
dbModel.entities = await this.GetAllTables(schema);
await this.GetCoulmnsFromEntity(dbModel.entities,schema);
await this.GetIndexesFromEntity(dbModel.entities,schema);
@ -15,7 +15,7 @@ export abstract class AbstractDriver {
this.FindPrimaryColumnsFromIndexes(dbModel)
return dbModel;
}
abstract async ConnectToServer(database: string, server: string, port: number, user: string, password: string);
abstract async ConnectToServer(database: string, server: string, port: number, user: string, password: string,ssl:boolean);
abstract async GetAllTables(schema:string): Promise<EntityInfo[]>
abstract async GetCoulmnsFromEntity(entities: EntityInfo[],schema:string): Promise<EntityInfo[]>;
abstract async GetIndexesFromEntity(entities: EntityInfo[],schema:string): Promise<EntityInfo[]>;

View File

@ -353,13 +353,27 @@ export class MariaDbDriver extends AbstractDriver {
}
private Connection: MariaDb.IConnection;
async ConnectToServer(database: string, server: string, port: number, user: string, password: string) {
let config: MariaDb.IConnectionConfig = {
database: database,
host: server,
port: port,
user: user,
password: password,
async ConnectToServer(database: string, server: string, port: number, user: string, password: string,ssl:boolean) {
let config: MariaDb.IConnectionConfig
if (ssl) {
config = {
database: database,
host: server,
port: port,
user: user,
password: password,
ssl: {
rejectUnauthorized: false
}
}
} else {
config = {
database: database,
host: server,
port: port,
user: user,
password: password
}
}

View File

@ -358,7 +358,7 @@ order by
}
private Connection: MSSQL.ConnectionPool;
async ConnectToServer(database: string, server: string, port: number, user: string, password: string) {
async ConnectToServer(database: string, server: string, port: number, user: string, password: string,ssl:boolean) {
let config: MSSQL.config = {
database: database,
server: server,
@ -366,7 +366,7 @@ order by
user: user,
password: password,
options: {
encrypt: true, // Use this if you're on Windows Azure
encrypt: ssl, // Use this if you're on Windows Azure
appName: 'typeorm-model-generator'
}
}

View File

@ -21,7 +21,7 @@ export class MysqlDriver extends AbstractDriver {
});
}
async GetAllTables(schema:string): Promise<EntityInfo[]> {
async GetAllTables(schema: string): Promise<EntityInfo[]> {
let response = await this.ExecQuery<{ TABLE_SCHEMA: string, TABLE_NAME: string }>(`SELECT TABLE_SCHEMA, TABLE_NAME
FROM information_schema.tables
@ -37,7 +37,7 @@ export class MysqlDriver extends AbstractDriver {
})
return ret;
}
async GetCoulmnsFromEntity(entities: EntityInfo[],schema:string): Promise<EntityInfo[]> {
async GetCoulmnsFromEntity(entities: EntityInfo[], schema: string): Promise<EntityInfo[]> {
let response = await this.ExecQuery<{
TABLE_NAME: string, COLUMN_NAME: string, COLUMN_DEFAULT: string,
IS_NULLABLE: string, DATA_TYPE: string, CHARACTER_MAXIMUM_LENGTH: number,
@ -68,8 +68,8 @@ export class MysqlDriver extends AbstractDriver {
} else {
colInfo.ts_type = "number"
colInfo.sql_type = "smallint"
colInfo.char_max_lenght = resp.CHARACTER_MAXIMUM_LENGTH > 0 ? resp.CHARACTER_MAXIMUM_LENGTH : null;
}
colInfo.char_max_lenght = resp.CHARACTER_MAXIMUM_LENGTH > 0 ? resp.CHARACTER_MAXIMUM_LENGTH : null;
}
break;
case "smallint":
colInfo.ts_type = "number"
@ -162,7 +162,7 @@ export class MysqlDriver extends AbstractDriver {
})
return entities;
}
async GetIndexesFromEntity(entities: EntityInfo[],schema:string): Promise<EntityInfo[]> {
async GetIndexesFromEntity(entities: EntityInfo[], schema: string): Promise<EntityInfo[]> {
let response = await this.ExecQuery<{
TableName: string, IndexName: string, ColumnName: string, is_unique: number,
is_primary_key: number//, is_descending_key: number//, is_included_column: number
@ -200,7 +200,7 @@ export class MysqlDriver extends AbstractDriver {
return entities;
}
async GetRelations(entities: EntityInfo[],schema:string): Promise<EntityInfo[]> {
async GetRelations(entities: EntityInfo[], schema: string): Promise<EntityInfo[]> {
let response = await this.ExecQuery<{
TableWithForeignKey: string, FK_PartNo: number, ForeignKeyColumn: string,
TableReferenced: string, ForeignKeyColumnReferenced: string,
@ -353,13 +353,27 @@ export class MysqlDriver extends AbstractDriver {
}
private Connection: MYSQL.IConnection;
async ConnectToServer(database: string, server: string, port: number, user: string, password: string) {
let config: MYSQL.IConnectionConfig = {
database: database,
host: server,
port: port,
user: user,
password: password,
async ConnectToServer(database: string, server: string, port: number, user: string, password: string, ssl: boolean) {
let config: MYSQL.IConnectionConfig
if (ssl) {
config = {
database: database,
host: server,
port: port,
user: user,
password: password,
ssl: {
rejectUnauthorized: false
}
}
} else {
config = {
database: database,
host: server,
port: port,
user: user,
password: password
}
}

View File

@ -371,13 +371,14 @@ export class PostgresDriver extends AbstractDriver {
}
}
async ConnectToServer(database: string, server: string, port: number, user: string, password: string) {
async ConnectToServer(database: string, server: string, port: number, user: string, password: string,ssl:boolean) {
this.Connection = new PG.Client({
database: database,
host: server,
port: port,
user: user,
password: password
password: password,
ssl:ssl
})

View File

@ -50,6 +50,10 @@ var argv = Yargs
alias: 'schema',
describe: 'Schema name to create model from. Only for mssql and postgres.'
})
.option('ssl',{
boolean:true,
default:false
})
.argv;
@ -81,7 +85,6 @@ switch (argv.e) {
throw new Error('Database engine not recognized.');
}
let engine = new Engine(
driver, {
host: argv.h,
@ -91,7 +94,8 @@ let engine = new Engine(
password: argv.x,
databaseType: argv.e,
resultsPath: argv.o,
schemaName: argv.s || standardSchema
schemaName: argv.s || standardSchema,
ssl:argv.ssl
});
console.log(`[${new Date().toLocaleTimeString()}] Starting creation of model classes.`);

View File

@ -15,7 +15,7 @@ import * as ts from "typescript";
import { PostgresDriver } from "../../src/drivers/PostgresDriver";
import { MysqlDriver } from "../../src/drivers/MysqlDriver";
import { MariaDbDriver } from "../../src/drivers/MariaDbDriver";
import * as yn from "yn"
chai.use(chaiSubset);
@ -106,7 +106,7 @@ async function createMSSQLModels(filesOrgPath: string, resultsPath: string): Pro
let driver: AbstractDriver;
driver = new MssqlDriver();
await driver.ConnectToServer(`master`, String(process.env.MSSQL_Host), Number(process.env.MSSQL_Port), String(process.env.MSSQL_Username), String(process.env.MSSQL_Password));
await driver.ConnectToServer(`master`, String(process.env.MSSQL_Host), Number(process.env.MSSQL_Port), String(process.env.MSSQL_Username), String(process.env.MSSQL_Password), yn(process.env.MSSQL_SSL));
if (! await driver.CheckIfDBExists(String(process.env.MSSQL_Database)))
await driver.CreateDB(String(process.env.MSSQL_Database));
@ -140,7 +140,8 @@ async function createMSSQLModels(filesOrgPath: string, resultsPath: string): Pro
password: String(process.env.MSSQL_Password),
databaseType: 'mssql',
resultsPath: resultsPath,
schemaName:'dbo'
schemaName:'dbo',
ssl:yn(process.env.MSSQL_SSL)
});
@ -150,7 +151,7 @@ async function createMSSQLModels(filesOrgPath: string, resultsPath: string): Pro
async function createPostgresModels(filesOrgPath: string, resultsPath: string): Promise<Engine> {
let driver: AbstractDriver;
driver = new PostgresDriver();
await driver.ConnectToServer(`postgres`, String(process.env.POSTGRES_Host), Number(process.env.POSTGRES_Port), String(process.env.POSTGRES_Username), String(process.env.POSTGRES_Password));
await driver.ConnectToServer(`postgres`, String(process.env.POSTGRES_Host), Number(process.env.POSTGRES_Port), String(process.env.POSTGRES_Username), String(process.env.POSTGRES_Password),yn(process.env.POSTGRES_SSL));
if (! await driver.CheckIfDBExists(String(process.env.POSTGRES_Database)))
await driver.CreateDB(String(process.env.POSTGRES_Database));
@ -182,7 +183,8 @@ async function createPostgresModels(filesOrgPath: string, resultsPath: string):
password: String(process.env.POSTGRES_Password),
databaseType: 'postgres',
resultsPath: resultsPath,
schemaName:'public'
schemaName:'public',
ssl:yn(process.env.POSTGRES_SSL)
});
@ -193,7 +195,7 @@ async function createPostgresModels(filesOrgPath: string, resultsPath: string):
async function createMysqlModels(filesOrgPath: string, resultsPath: string): Promise<Engine> {
let driver: AbstractDriver;
driver = new MysqlDriver();
await driver.ConnectToServer(`mysql`, String(process.env.MYSQL_Host), Number(process.env.MYSQL_Port), String(process.env.MYSQL_Username), String(process.env.MYSQL_Password));
await driver.ConnectToServer(`mysql`, String(process.env.MYSQL_Host), Number(process.env.MYSQL_Port), String(process.env.MYSQL_Username), String(process.env.MYSQL_Password),yn(process.env.MYSQL_SSL));
if (! await driver.CheckIfDBExists(String(process.env.MYSQL_Database)))
await driver.CreateDB(String(process.env.MYSQL_Database));
@ -225,7 +227,8 @@ async function createMysqlModels(filesOrgPath: string, resultsPath: string): Pro
password: String(process.env.MYSQL_Password),
databaseType: 'mysql',
resultsPath: resultsPath,
schemaName:'ignored'
schemaName:'ignored',
ssl:yn(process.env.MYSQL_SSL)
});
@ -235,7 +238,7 @@ async function createMysqlModels(filesOrgPath: string, resultsPath: string): Pro
async function createMariaDBModels(filesOrgPath: string, resultsPath: string): Promise<Engine> {
let driver: AbstractDriver;
driver = new MariaDbDriver();
await driver.ConnectToServer(`mysql`, String(process.env.MARIADB_Host), Number(process.env.MARIADB_Port), String(process.env.MARIADB_Username), String(process.env.MARIADB_Password));
await driver.ConnectToServer(`mysql`, String(process.env.MARIADB_Host), Number(process.env.MARIADB_Port), String(process.env.MARIADB_Username), String(process.env.MARIADB_Password),yn(process.env.MARIADB_SSL));
if (! await driver.CheckIfDBExists(String(process.env.MARIADB_Database)))
await driver.CreateDB(String(process.env.MARIADB_Database));
@ -268,7 +271,8 @@ async function createMariaDBModels(filesOrgPath: string, resultsPath: string): P
password: String(process.env.MARIADB_Password),
databaseType: 'mariadb',
resultsPath: resultsPath,
schemaName:'ignored'
schemaName:'ignored',
ssl:yn(process.env.MARIADB_SSL)
});

View File

@ -4,6 +4,7 @@
},
"dependencies": {
"mssql": "registry:dt/mssql#3.3.0+20170311011547",
"yargs": "registry:npm/yargs#5.0.0+20160907000723"
"yargs": "registry:npm/yargs#5.0.0+20160907000723",
"yn": "registry:npm/yn#1.3.0+20170508185912"
}
}