inceptum
Version:
hipages take on the foundational library for enterprise-grade apps written in NodeJS
113 lines • 3.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MySQLClient = exports.MySQLTransaction = void 0;
const mysql = require("mysql");
const DBTransaction_1 = require("../db/DBTransaction");
const LogManager_1 = require("../log/LogManager");
const DBClient_1 = require("../db/DBClient");
const LOGGER = LogManager_1.LogManager.getLogger(__filename);
const MARKED_FOR_DELETION = 'MARKED_FOR_DELETION';
class MySQLTransaction extends DBTransaction_1.DBTransaction {
runQueryInConnection(sql, bindsArr) {
return new Promise((resolve, reject) => this.connection.query(sql, bindsArr, (err, rows) => {
if (err) {
LOGGER.error(err, {
sql,
connectionId: this.connection.threadId,
host: this.connection.config.host,
readonly: this.isReadonly(),
user: this.connection.config.user,
});
this.connection[MARKED_FOR_DELETION] = true;
return reject(err);
}
return resolve(rows);
}));
}
getTransactionBeginSQL() {
return (this.isReadonly()) ? 'START TRANSACTION READ ONLY' : 'START TRANSACTION READ WRITE';
}
}
exports.MySQLTransaction = MySQLTransaction;
class MySQLConnectionFactory {
constructor(name, connectionConfig) {
this.connConfig = connectionConfig;
this.name = name;
}
create() {
LOGGER.trace(`Creating new connection for pool ${this.name}`);
const connection = mysql.createConnection(this.connConfig);
/*
* There isnt any good reason why a connection should ever error, so we mark any connection
* that throws as error as bad, the pool will then check before giving this connection back out
* and throw it away.
*/
connection.on('error', (error) => {
LOGGER.error(error, 'Connection Error', {
connectionId: connection.threadId,
host: this.connConfig.host,
user: this.connConfig.user,
});
connection[MARKED_FOR_DELETION] = true;
});
return new Promise((resolve, reject) => connection.connect((err) => {
if (err) {
reject(err);
}
else {
resolve(connection);
}
}));
}
destroy(connection) {
LOGGER.trace(`Destroying connection for pool ${this.name}`);
return new Promise((resolve, reject) => {
connection.end((err) => {
if (err) {
reject(err);
}
else {
resolve();
}
});
});
}
async validate(connection) {
LOGGER.trace(`Validating connection for pool ${this.name}`);
return !connection[MARKED_FOR_DELETION];
}
}
/**
* A MySQL client you can use to execute queries against MySQL
*/
class MySQLClient extends DBClient_1.DBClient {
constructor(clientConfig) {
super(clientConfig);
this.enable57Mode = false;
}
async initialise() {
this.enable57Mode = this.clientConfiguration.enable57Mode || false;
await super.initialise();
}
getConnectionFactory(name, connectionConfig) {
return new MySQLConnectionFactory(name, connectionConfig);
}
getNewDBTransaction(readonly, connectionPool) {
return new MySQLTransaction(this.clientConfiguration.name, readonly, connectionPool);
}
getPingQuery() {
return 'SELECT 1';
}
getDefaultConnectionConfig() {
return {
host: 'localhost',
port: 3306,
user: 'root',
password: '',
database: '',
charset: 'UTF8',
};
}
}
exports.MySQLClient = MySQLClient;
//# sourceMappingURL=MySQLClient.js.map