mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
added mysql2 support
This commit is contained in:
parent
1b4295bbe9
commit
9e1234765a
@ -45,6 +45,7 @@
|
||||
"mocha": "^3.0.2",
|
||||
"mssql": "^3.3.0",
|
||||
"mysql": "^2.11.1",
|
||||
"mysql2": "^1.0.0",
|
||||
"oracledb": "^1.11.0",
|
||||
"pg": "^6.1.0",
|
||||
"remap-istanbul": "^0.6.4",
|
||||
|
||||
@ -411,6 +411,8 @@ export class ConnectionManager {
|
||||
switch (options.type) {
|
||||
case "mysql":
|
||||
return new MysqlDriver(options, logger);
|
||||
case "mysql2":
|
||||
return new MysqlDriver(options, logger, undefined, "mysql2");
|
||||
case "postgres":
|
||||
return new PostgresDriver(options, logger);
|
||||
case "mariadb":
|
||||
|
||||
@ -8,7 +8,7 @@ export class MissingDriverError extends Error {
|
||||
|
||||
constructor(driverType: string) {
|
||||
super();
|
||||
this.message = `Wrong driver ${driverType} given. Supported drivers are: "mysql", "postgres", "mssql", "oracle", "mariadb", "sqlite".`;
|
||||
this.message = `Wrong driver ${driverType} given. Supported drivers are: "mysql", "mysql2", "postgres", "mssql", "oracle", "mariadb", "sqlite".`;
|
||||
this.stack = new Error().stack;
|
||||
}
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@ export interface DriverOptions {
|
||||
/**
|
||||
* Database type.
|
||||
*/
|
||||
readonly type: "mysql"|"postgres"|"mariadb"|"sqlite"|"oracle"|"mssql";
|
||||
readonly type: "mysql"|"mysql2"|"postgres"|"mariadb"|"sqlite"|"oracle"|"mssql";
|
||||
|
||||
/**
|
||||
* Connection url to where perform connection to.
|
||||
|
||||
@ -57,15 +57,21 @@ export class MysqlDriver implements Driver {
|
||||
*/
|
||||
protected logger: Logger;
|
||||
|
||||
/**
|
||||
* Driver type's version. node-mysql and mysql2 are supported.
|
||||
*/
|
||||
protected version: "mysql"|"mysql2" = "mysql";
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Constructor
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
constructor(options: DriverOptions, logger: Logger, mysql?: any) {
|
||||
constructor(options: DriverOptions, logger: Logger, mysql?: any, mysqlVersion: "mysql"|"mysql2" = "mysql") {
|
||||
|
||||
this.options = DriverUtils.buildDriverOptions(options);
|
||||
this.logger = logger;
|
||||
this.mysql = mysql;
|
||||
this.version = mysqlVersion;
|
||||
|
||||
// validate options to make sure everything is set
|
||||
if (!this.options.host)
|
||||
@ -124,7 +130,7 @@ export class MysqlDriver implements Driver {
|
||||
*/
|
||||
disconnect(): Promise<void> {
|
||||
if (!this.databaseConnection && !this.pool)
|
||||
throw new ConnectionIsNotSetError("mysql");
|
||||
throw new ConnectionIsNotSetError(this.version);
|
||||
|
||||
return new Promise<void>((ok, fail) => {
|
||||
const handler = (err: any) => err ? fail(err) : ok();
|
||||
@ -149,7 +155,7 @@ export class MysqlDriver implements Driver {
|
||||
*/
|
||||
async createQueryRunner(): Promise<QueryRunner> {
|
||||
if (!this.databaseConnection && !this.pool)
|
||||
return Promise.reject(new ConnectionIsNotSetError("mysql"));
|
||||
return Promise.reject(new ConnectionIsNotSetError(this.version));
|
||||
|
||||
const databaseConnection = await this.retrieveDatabaseConnection();
|
||||
return new MysqlQueryRunner(databaseConnection, this, this.logger);
|
||||
@ -312,7 +318,7 @@ export class MysqlDriver implements Driver {
|
||||
if (this.databaseConnection)
|
||||
return Promise.resolve(this.databaseConnection);
|
||||
|
||||
throw new ConnectionIsNotSetError("mysql");
|
||||
throw new ConnectionIsNotSetError(this.version);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -323,9 +329,9 @@ export class MysqlDriver implements Driver {
|
||||
throw new DriverPackageLoadError();
|
||||
|
||||
try {
|
||||
this.mysql = require("mysql");
|
||||
this.mysql = require(this.version);
|
||||
} catch (e) {
|
||||
throw new DriverPackageNotInstalledError("Mysql", "mysql");
|
||||
throw new DriverPackageNotInstalledError("Mysql", this.version);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user