@breautek/storm
Version:
Object-Oriented REST API framework
100 lines (97 loc) • 4 kB
JavaScript
;
/*
Copyright 2017-2021 Norman Breau
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.MySQLDatabase = void 0;
const tslib_1 = require("tslib");
const Database_1 = require("./Database");
const MySQLConnection_1 = require("./MySQLConnection");
const MySQL = tslib_1.__importStar(require("mysql"));
const instance_1 = require("./instance");
const ConnectionReplicationWaiter_1 = require("./private/ConnectionReplicationWaiter");
const TAG = 'MySQLDatabase';
class MySQLDatabase extends Database_1.Database {
constructor() {
super();
// TODO: Maybe one day this may be exposed via a bt config setting.
this.$cluster = MySQL.createPoolCluster({
removeNodeErrorCount: Infinity,
restoreNodeTimeout: 1000
});
this.$cluster.on('enqueue', () => {
(0, instance_1.getInstance)().getLogger().warn(TAG, 'Waiting for available connection...');
});
}
escape(value) {
return MySQLDatabase.escape(value);
}
static escape(value) {
return MySQL.escape(value);
}
_addNode(nodeID, config) {
(0, instance_1.getInstance)().getLogger().trace(TAG, `Adding node to connection pool: "${nodeID}"`);
this.$cluster.add(nodeID, config);
}
_removeNode(nodeID) {
(0, instance_1.getInstance)().getLogger().trace(TAG, `Removing node to connection pool: "${nodeID}"`);
this.$cluster.remove(nodeID);
}
_destroy() {
return new Promise((resolve, reject) => {
this.$cluster.end((err) => {
if (err) {
reject(err);
return;
}
resolve();
});
});
}
$getConnectionFromPool(query, requireWriteAccess, instantationStack) {
return new Promise((resolve, reject) => {
this.$cluster.getConnection(query, (error, connection) => {
if (error) {
reject(error);
return;
}
resolve(new MySQLConnection_1.MySQLConnection(connection, instantationStack, !requireWriteAccess));
});
});
}
async _getConnection(query, requireWriteAccess, position) {
(0, instance_1.getInstance)().getLogger().trace(TAG, `Querying connection pool for "${query}".`);
let instantationStack = new Error().stack;
let conn = await this.$getConnectionFromPool(query, requireWriteAccess, instantationStack);
await conn.__internal_init();
if (conn.isReplication()) {
// master connections will not wait on database positions
// they are guarenteed to be at the tip.
// readonly, or otherwise known as replication connections
// may have replication lag. If we have a desired position target,
// then await for the connection to catch up to that target.
if (position && position.page && position.position) {
let waiter = new ConnectionReplicationWaiter_1.ConnectionReplicationWaiter(conn);
try {
await waiter.wait(position);
}
catch (ex) {
conn.close(true);
throw ex;
}
}
}
return conn;
}
}
exports.MySQLDatabase = MySQLDatabase;
//# sourceMappingURL=MySQLDatabase.js.map