updated connection api and made mysql driver to require dynamically

This commit is contained in:
Umed Khudoiberdiev 2016-03-23 23:42:54 +05:00
parent 8302c8e575
commit 26a5112242
20 changed files with 50 additions and 42 deletions

View File

@ -101,7 +101,7 @@ import {Photo} from "./Photo";
const options: CreateConnectionOptions = {
driver: "mysql", // specify driver type here. Right now only "mysql" is supported
connectionOptions: {
connection: {
host: "localhost", // mysql host
port: 3306, // mysql port
username: "root", // mysql database username

View File

@ -3,7 +3,7 @@ import {Post} from "./entity/Post";
const options: CreateConnectionOptions = {
driver: "mysql",
connectionOptions: {
connection: {
host: "192.168.99.100",
port: 3306,
username: "root",

View File

@ -7,7 +7,7 @@ import {Category} from "./entity/Category";
const options: CreateConnectionOptions = {
driver: "mysql",
connectionOptions: {
connection: {
host: "192.168.99.100",
port: 3306,
username: "root",

View File

@ -9,7 +9,7 @@ import {PostAuthor} from "./entity/PostAuthor";
const options: CreateConnectionOptions = {
driver: "mysql",
connectionOptions: {
connection: {
host: "192.168.99.100",
port: 3306,
username: "root",

View File

@ -9,7 +9,7 @@ import {PostAuthor} from "./entity/PostAuthor";
const options: CreateConnectionOptions = {
driver: "mysql",
connectionOptions: {
connection: {
host: "192.168.99.100",
port: 3306,
username: "root",

View File

@ -4,7 +4,7 @@ import {PostDetails} from "./entity/PostDetails";
const options: CreateConnectionOptions = {
driver: "mysql",
connectionOptions: {
connection: {
host: "192.168.99.100",
port: 3306,
username: "root",

View File

@ -7,7 +7,7 @@ import {EverythingSubscriber} from "./subscriber/EverythingSubscriber";
// first create a connection
const options: CreateConnectionOptions = {
driver: "mysql",
connectionOptions: {
connection: {
host: "192.168.99.100",
port: 3306,
username: "root",

View File

@ -6,7 +6,7 @@ import {Blog} from "./entity/Blog";
const options: CreateConnectionOptions = {
driver: "mysql",
connectionOptions: {
connection: {
host: "192.168.99.100",
port: 3306,
username: "root",

View File

@ -5,7 +5,7 @@ import {PostAuthor} from "./entity/PostAuthor";
const options: CreateConnectionOptions = {
driver: "mysql",
connectionOptions: {
connection: {
host: "192.168.99.100",
port: 3306,
username: "root",

View File

@ -3,7 +3,7 @@ import {Category} from "./entity/Category";
const options: CreateConnectionOptions = {
driver: "mysql",
connectionOptions: {
connection: {
host: "192.168.99.100",
port: 3306,
username: "root",

View File

@ -5,7 +5,7 @@ import {PostAuthor} from "./entity/PostAuthor";
const options: CreateConnectionOptions = {
driver: "mysql",
connectionOptions: {
connection: {
host: "192.168.99.100",
port: 3306,
username: "root",

View File

@ -35,10 +35,11 @@ export class Connection {
// Constructor
// -------------------------------------------------------------------------
constructor(name: string, driver: Driver) {
constructor(name: string, driver: Driver, options: ConnectionOptions) {
this._name = name;
this._driver = driver;
this._driver.connection = this;
this._options = options;
}
// -------------------------------------------------------------------------
@ -101,15 +102,12 @@ export class Connection {
/**
* Performs connection to the database.
*/
connect(options: ConnectionOptions): Promise<void> {
connect(): Promise<void> {
const schemaCreator = new SchemaCreator(this);
this._options = options;
return this._driver
.connect(options)
.then(() => {
if (options.autoSchemaCreate === true)
return schemaCreator.create();
});
return this._driver.connect().then(() => {
if (this._options.autoSchemaCreate === true)
return schemaCreator.create();
});
}
/**

View File

@ -5,6 +5,7 @@ import {DefaultNamingStrategy} from "../naming-strategy/DefaultNamingStrategy";
import {ConnectionNotFoundError} from "./error/ConnectionNotFoundError";
import {EntityMetadataBuilder} from "../metadata-builder/EntityMetadataBuilder";
import {importClassesFromDirectories} from "../util/DirectoryExportedClassesLoader";
import {ConnectionOptions} from "tls";
/**
* Connection manager holds all connections made to the databases and providers helper management functions
@ -48,11 +49,12 @@ export class ConnectionManager {
/**
* Creates and adds a new connection with given driver.
*/
createConnection(driver: Driver): Connection;
createConnection(name: string, driver: Driver): Connection;
createConnection(name: string|Driver, driver?: Driver): Connection {
createConnection(driver: Driver, options: ConnectionOptions): Connection;
createConnection(name: string, driver: Driver, options: ConnectionOptions): Connection;
createConnection(name: string|Driver, driver?: Driver|ConnectionOptions, options?: ConnectionOptions): Connection {
if (typeof name === "object") {
driver = <Driver> name;
options = <ConnectionOptions> driver;
}
if (!name) {
name = "default";
@ -61,7 +63,7 @@ export class ConnectionManager {
if (existConnection)
this.connections.splice(this.connections.indexOf(existConnection), 1);
const connection = new Connection(<string> name, driver);
const connection = new Connection(<string> name, <Driver> driver, options);
this.connections.push(connection);
return connection;
}

View File

@ -36,7 +36,7 @@ export interface Driver {
/**
* Performs connection to the database based on given connection options.
*/
connect(options: ConnectionOptions): Promise<void>;
connect(): Promise<void>;
/**
* Closes connection with database.

View File

@ -57,8 +57,20 @@ export class MysqlDriver extends BaseDriver implements Driver {
// Constructor
// -------------------------------------------------------------------------
constructor(mysql: any) {
constructor(mysql?: any) {
super();
// if driver dependency is not given explicitly, then try to load it via "require"
if (!mysql && require) {
try {
mysql = require("mysql");
} catch (e) {
throw new Error("Mysql package was not found installed. Try to install it: npm install mysql --save");
}
} else {
throw new Error("Cannot load mysql driver dependencies. Try to install all required dependencies.");
}
this.mysql = mysql;
}
@ -83,12 +95,12 @@ export class MysqlDriver extends BaseDriver implements Driver {
/**
* Performs connection to the database based on given connection options.
*/
connect(options: ConnectionOptions): Promise<void> {
connect(): Promise<void> {
this.mysqlConnection = this.mysql.createConnection({
host: options.host,
user: options.username,
password: options.password,
database: options.database
host: this.connection.options.host,
user: this.connection.options.username,
password: this.connection.options.password,
database: this.connection.options.database
});
return new Promise<void>((ok, fail) => {
this.mysqlConnection.connect((err: any) => err ? fail(err) : ok());

View File

@ -2,8 +2,6 @@ import {ConnectionOptions} from "./connection/ConnectionOptions";
import {ConnectionManager} from "./connection/ConnectionManager";
import {Connection} from "./connection/Connection";
import {MysqlDriver} from "./driver/MysqlDriver";
// import * as mysql from "mysql";
let mysql = require("mysql");
/**
* Global connection manager.
@ -23,7 +21,7 @@ export interface CreateConnectionOptions {
/**
* Database connection options.
*/
connectionOptions: ConnectionOptions;
connection: ConnectionOptions;
/**
* Connection name. By default its called "default". Different connections must have different names.
@ -59,7 +57,7 @@ export function createConnection(options: CreateConnectionOptions): Promise<Conn
let connection: Connection;
switch (options.driver) {
case "mysql":
connection = connectionManager.createConnection(options.connectionName, new MysqlDriver(mysql));
connection = connectionManager.createConnection(options.connectionName, new MysqlDriver(), options.connection);
break;
default:
throw new Error(`Wrong driver ${options.driver} given. Supported drivers are: "mysql"`);
@ -77,7 +75,5 @@ export function createConnection(options: CreateConnectionOptions): Promise<Conn
if (options.subscribers)
connectionManager.importSubscribers(options.subscribers);
return connection
.connect(options.connectionOptions)
.then(() => connection);
return connection.connect().then(() => connection);
}

View File

@ -15,7 +15,7 @@ describe("insertion", function() {
const parameters: CreateConnectionOptions = {
driver: "mysql",
connectionOptions: {
connection: {
host: "192.168.99.100",
port: 3306,
username: "root",

View File

@ -21,7 +21,7 @@ describe("one-to-one", function() {
const options: CreateConnectionOptions = {
driver: "mysql",
connectionOptions: {
connection: {
host: "192.168.99.100",
port: 3306,
username: "root",

View File

@ -21,7 +21,7 @@ describe("many-to-one", function() {
const options: CreateConnectionOptions = {
driver: "mysql",
connectionOptions: {
connection: {
host: "192.168.99.100",
port: 3306,
username: "root",

View File

@ -19,7 +19,7 @@ describe("many-to-many", function() {
const options: CreateConnectionOptions = {
driver: "mysql",
connectionOptions: {
connection: {
host: "192.168.99.100",
port: 3306,
username: "root",