UNPKG

3.97 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const LogManager_1 = require("../log/LogManager");
4const ConnectionPool_1 = require("./ConnectionPool");
5const LOGGER = LogManager_1.LogManager.getLogger(__filename);
6class DBClient {
7 constructor(clientConfiguration) {
8 this.clientConfiguration = clientConfiguration;
9 }
10 /**
11 * Runs a function in a transaction. The function must receive one parameter that will be of class
12 * {MysqlTransaction} and that you need to use to run all queries in this transaction
13 *
14 * @param {boolean} readonly Whether the transaction needs to be readonly or not
15 * @param {Function} func A function that returns a promise that will execute all the queries wanted in this transaction
16 * @returns {Promise} A promise that will execute the whole transaction
17 */
18 async runInTransaction(readonly, func) {
19 const dbTransaction = this.getNewDBTransaction(readonly, this.getPoolForReadonly(readonly));
20 try {
21 await dbTransaction.begin();
22 const resp = await func(dbTransaction);
23 return resp;
24 }
25 catch (err) {
26 LOGGER.error(err, 'There was an error running in transaction');
27 dbTransaction.markError(err);
28 throw err;
29 }
30 finally {
31 await dbTransaction.end();
32 }
33 }
34 getPoolForReadonly(readonly) {
35 if (readonly && this.slavePool) {
36 return this.slavePool;
37 }
38 return this.masterPool;
39 }
40 /**
41 * Shorthand for a single readonly query
42 * @param sql query to run
43 * @param binds binds
44 */
45 async read(sql, ...binds) {
46 return this.executeSql(true, sql, ...binds);
47 }
48 /**
49 * Shorthand to run a single sql.
50 * Easy to mock
51 * @param readonly
52 * @param sql
53 * @param binds
54 */
55 async executeSql(readonly, sql, ...binds) {
56 return this.runInTransaction(readonly, (client) => client.query(sql, ...binds));
57 }
58 async initialise() {
59 const defaultConnectionConfig = this.getDefaultConnectionConfig();
60 if (this.clientConfiguration.master) {
61 // tslint:disable-next-line:prefer-object-spread
62 const fullMasterConfig = Object.assign({}, defaultConnectionConfig, this.clientConfiguration.master.connectionConfig);
63 this.masterPool = new ConnectionPool_1.InstrumentedConnectionPool(this.getConnectionFactory(`${this.clientConfiguration.name}_master`, fullMasterConfig), this.clientConfiguration.master, this.clientConfiguration.name, false);
64 await this.masterPool.start();
65 }
66 if (this.clientConfiguration.slave) {
67 // tslint:disable-next-line:prefer-object-spread
68 const fullSlaveConfig = Object.assign({}, defaultConnectionConfig, this.clientConfiguration.slave.connectionConfig);
69 this.slavePool = new ConnectionPool_1.InstrumentedConnectionPool(this.getConnectionFactory(`${this.clientConfiguration.name}_slave`, fullSlaveConfig), this.clientConfiguration.slave, this.clientConfiguration.name, true);
70 await this.slavePool.start();
71 }
72 if (!this.masterPool && !this.slavePool) {
73 throw new Error(`MysqlClient ${this.name} has no connections configured for either master or slave`);
74 }
75 }
76 async stop() {
77 if (this.masterPool) {
78 await this.masterPool.stop();
79 }
80 if (this.slavePool) {
81 await this.slavePool.stop();
82 }
83 }
84 async ping(readonly) {
85 LOGGER.trace('Doing ping');
86 const pool = this.getPoolForReadonly(readonly);
87 const dbTransaction = this.getNewDBTransaction(true, pool);
88 await dbTransaction.runQueryWithoutTransaction(this.getPingQuery());
89 }
90}
91DBClient.startMethod = 'initialise';
92DBClient.stopMethod = 'stop';
93exports.DBClient = DBClient;
94//# sourceMappingURL=DBClient.js.map
\No newline at end of file