1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const LogManager_1 = require("../log/LogManager");
|
4 | const ConnectionPool_1 = require("./ConnectionPool");
|
5 | const LOGGER = LogManager_1.LogManager.getLogger(__filename);
|
6 | class DBClient {
|
7 | constructor(clientConfiguration) {
|
8 | this.clientConfiguration = clientConfiguration;
|
9 | }
|
10 | |
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
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 |
|
42 |
|
43 |
|
44 |
|
45 | async read(sql, ...binds) {
|
46 | return this.executeSql(true, sql, ...binds);
|
47 | }
|
48 | |
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
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 |
|
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 |
|
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 | }
|
91 | DBClient.startMethod = 'initialise';
|
92 | DBClient.stopMethod = 'stop';
|
93 | exports.DBClient = DBClient;
|
94 |
|
\ | No newline at end of file |