diff --git a/.travis.yml b/.travis.yml index e761ce4..84de49a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/package-lock.json b/package-lock.json index 02843c5..3838447 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 028053d..22b97ae 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/Engine.ts b/src/Engine.ts index 2e891ba..09aa4fb 100644 --- a/src/Engine.ts +++ b/src/Engine.ts @@ -11,7 +11,7 @@ export class Engine { } public async createModelFromDatabase(): Promise { - 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 { - 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 { + 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 } \ No newline at end of file diff --git a/src/drivers/AbstractDriver.ts b/src/drivers/AbstractDriver.ts index 72329c5..707a86c 100644 --- a/src/drivers/AbstractDriver.ts +++ b/src/drivers/AbstractDriver.ts @@ -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 { + async GetDataFromServer(database: string, server: string, port: number, user: string, password: string, schema:string, ssl:boolean): Promise { let dbModel = {}; - 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 abstract async GetCoulmnsFromEntity(entities: EntityInfo[],schema:string): Promise; abstract async GetIndexesFromEntity(entities: EntityInfo[],schema:string): Promise; diff --git a/src/drivers/MariaDbDriver.ts b/src/drivers/MariaDbDriver.ts index 77b9a4d..d8d2222 100644 --- a/src/drivers/MariaDbDriver.ts +++ b/src/drivers/MariaDbDriver.ts @@ -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 + } } diff --git a/src/drivers/MssqlDriver.ts b/src/drivers/MssqlDriver.ts index c804111..d360b68 100644 --- a/src/drivers/MssqlDriver.ts +++ b/src/drivers/MssqlDriver.ts @@ -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' } } diff --git a/src/drivers/MysqlDriver.ts b/src/drivers/MysqlDriver.ts index 637bf85..6e33612 100644 --- a/src/drivers/MysqlDriver.ts +++ b/src/drivers/MysqlDriver.ts @@ -21,7 +21,7 @@ export class MysqlDriver extends AbstractDriver { }); } - async GetAllTables(schema:string): Promise { + async GetAllTables(schema: string): Promise { 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 { + async GetCoulmnsFromEntity(entities: EntityInfo[], schema: string): Promise { 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 { + async GetIndexesFromEntity(entities: EntityInfo[], schema: string): Promise { 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 { + async GetRelations(entities: EntityInfo[], schema: string): Promise { 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 + } } diff --git a/src/drivers/PostgresDriver.ts b/src/drivers/PostgresDriver.ts index 4895f74..b013eb6 100644 --- a/src/drivers/PostgresDriver.ts +++ b/src/drivers/PostgresDriver.ts @@ -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 }) diff --git a/src/index.ts b/src/index.ts index 43810df..e0f9090 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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.`); diff --git a/test/integration/integration.test.ts b/test/integration/integration.test.ts index d30cea9..0138094 100644 --- a/test/integration/integration.test.ts +++ b/test/integration/integration.test.ts @@ -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 { 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 { 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 { 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) }); diff --git a/typings.json b/typings.json index 8203879..82bf57c 100644 --- a/typings.json +++ b/typings.json @@ -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" } }