knex/lib/dialects/mysql2/index.js

54 lines
1.4 KiB
JavaScript
Raw Normal View History

2015-05-09 13:58:18 -04:00
// MySQL2 Client
// -------
const Client_MySQL = require('../mysql');
const Transaction = require('./transaction');
2015-05-09 13:58:18 -04:00
// Always initialize with the "QueryBuilder" and "QueryCompiler"
// objects, which extend the base 'lib/query/builder' and
// 'lib/query/compiler', respectively.
2021-01-31 13:40:13 +03:00
class Client_MySQL2 extends Client_MySQL {
2016-09-12 18:45:35 -04:00
transaction() {
return new Transaction(this, ...arguments);
2021-01-31 13:40:13 +03:00
}
2015-05-09 13:58:18 -04:00
_driver() {
return require('mysql2');
2021-01-31 13:40:13 +03:00
}
initializeDriver() {
try {
this.driver = this._driver();
} catch (e) {
let message = `Knex: run\n$ npm install ${this.driverName}`;
const nodeMajorVersion = process.version.replace(/^v/, '').split('.')[0];
if (nodeMajorVersion <= 12) {
message += `@3.2.0`;
this.logger.error(
'Mysql2 version 3.2.0 is the latest version to support Node.js 12 or lower.'
);
}
message += ` --save`;
this.logger.error(`${message}\n${e.message}\n${e.stack}`);
throw new Error(`${message}\n${e.message}`);
}
}
validateConnection(connection) {
return (
connection &&
!connection._fatalError &&
!connection._protocolError &&
!connection._closing &&
!connection.stream.destroyed
);
2021-01-31 13:40:13 +03:00
}
}
Object.assign(Client_MySQL2.prototype, {
// The "dialect", for reference elsewhere.
driverName: 'mysql2',
});
2015-05-09 13:58:18 -04:00
module.exports = Client_MySQL2;