|
| 1 | +/* istanbul ignore file */ |
| 2 | +/* eslint-disable no-restricted-syntax */ |
| 3 | +/* eslint-disable no-await-in-loop */ |
| 4 | +/* eslint-disable no-else-return */ |
| 5 | +const mysql = require("mysql"); |
| 6 | +const fs = require('fs'); |
| 7 | + |
| 8 | +let pools = {}; |
| 9 | +let config = {}; |
| 10 | +const genericError = 'An error occurred communicating with the database.'; |
| 11 | + |
| 12 | +exports.init = async (cfg) => { |
| 13 | + config = cfg; |
| 14 | +}; |
| 15 | + |
| 16 | +exports.createPool = async (poolName) => { |
| 17 | + try { |
| 18 | + const srcCfg = config.DATASOURCES[poolName]; |
| 19 | + if (srcCfg) { |
| 20 | + const options = { |
| 21 | + connectionLimit: srcCfg.DB_CONNECTION_LIMIT || 5, |
| 22 | + host: srcCfg.DB_HOST, |
| 23 | + user: srcCfg.DB_USER, |
| 24 | + password: srcCfg.DB_PASSWORD, |
| 25 | + database: srcCfg.DB_DATABASE, |
| 26 | + port: srcCfg.PORT, |
| 27 | + multipleStatements: srcCfg.ALLOW_MULTI_STATEMENTS || false, |
| 28 | + timezone: srcCfg.TIMEZONE || 'local', |
| 29 | + typeCast: srcCfg.TYPE_CAST || true, |
| 30 | + dateStrings: srcCfg.DATE_STRINGS || false |
| 31 | + }; |
| 32 | + |
| 33 | + if (srcCfg.SSL) { |
| 34 | + const sslConfig = {}; |
| 35 | + |
| 36 | + if (srcCfg.SSL.CUSTOM_CERT) { |
| 37 | + sslConfig.ca = srcCfg.SSL.CUSTOM_CERT; |
| 38 | + } else { |
| 39 | + sslConfig.rejectUnauthorized = srcCfg.SSL.hasOwnProperty('REJECT_UNAUTHORIZED') ? srcCfg.SSL.REJECT_UNAUTHORIZED : true; |
| 40 | + } |
| 41 | + |
| 42 | + options.ssl = sslConfig; |
| 43 | + } |
| 44 | + |
| 45 | + pools[poolName] = mysql.createPool(options); |
| 46 | + console.debug(`MySQL Adapter: Pool ${poolName} created`); |
| 47 | + return true; |
| 48 | + } else { |
| 49 | + console.error(`MySQL Adapter: Missing configuration for ${poolName}`); |
| 50 | + return false; |
| 51 | + } |
| 52 | + } catch (err) { |
| 53 | + console.error("MySQL Adapter: Error while closing connection", err); |
| 54 | + return false; |
| 55 | + } |
| 56 | +}; |
| 57 | + |
| 58 | +exports.connect = async (poolName) => { |
| 59 | + try { |
| 60 | + if (!pools[poolName]) { |
| 61 | + await this.createPool(poolName); |
| 62 | + } |
| 63 | + return pools[poolName]; |
| 64 | + } catch (err) { |
| 65 | + console.error("MySQL Adapter: Error while retrieving a connection", err); |
| 66 | + throw new Error(err.message); |
| 67 | + } |
| 68 | +}; |
| 69 | + |
| 70 | +this.query = async (conn, query, params) => { |
| 71 | + return new Promise((resolve, reject) => { |
| 72 | + try { |
| 73 | + conn.query(query, params, (error, results) => { |
| 74 | + if (error) { |
| 75 | + console.error("MySQL Adapter: Failure in query: ", error); |
| 76 | + this.handleError(reject, error); |
| 77 | + } else { |
| 78 | + resolve(results); |
| 79 | + } |
| 80 | + }); |
| 81 | + } catch (err) { |
| 82 | + console.error("MySQL Adapter: Failure in query: ", err); |
| 83 | + this.handleError(reject, err); |
| 84 | + } |
| 85 | + }); |
| 86 | +}; |
| 87 | + |
| 88 | +this.handleError = (reject, error) => { |
| 89 | + const errorReturn = (config && config.HIDE_DB_ERRORS) ? new Error(genericError) : error; |
| 90 | + reject(errorReturn); |
| 91 | +}; |
| 92 | + |
| 93 | +exports.execute = async (srcName, query, params = {}) => { |
| 94 | + try { |
| 95 | + console.debug(query); |
| 96 | + if (params) { |
| 97 | + console.debug(JSON.stringify(params)); |
| 98 | + } |
| 99 | + |
| 100 | + const start = process.hrtime(); |
| 101 | + const conn = await this.connect(srcName); |
| 102 | + |
| 103 | + console.debug( |
| 104 | + `MySQL Adapter: Connection secured: ${process.hrtime(start)[0]}s ${ |
| 105 | + process.hrtime(start)[1] / 1000000 |
| 106 | + }ms` |
| 107 | + ); |
| 108 | + const results = await this.query(conn, query, params); |
| 109 | + |
| 110 | + console.debug( |
| 111 | + `MySQL Adapter: Query executed: ${process.hrtime(start)[0]}s ${ |
| 112 | + process.hrtime(start)[1] / 1000000 |
| 113 | + }ms` |
| 114 | + ); |
| 115 | + |
| 116 | + return results; |
| 117 | + } catch (err) { |
| 118 | + console.error("MySQL Adapter: Error while executing query", err); |
| 119 | + throw new Error(err.message); |
| 120 | + } |
| 121 | +}; |
| 122 | + |
| 123 | +exports.closeAllPools = async () => { |
| 124 | + try { |
| 125 | + for (const poolAlias of Object.keys(pools)) { |
| 126 | + await this.closePool(poolAlias); |
| 127 | + delete pools[poolAlias]; |
| 128 | + console.debug(`MySQL Adapter: Pool ${poolAlias} closed`); |
| 129 | + } |
| 130 | + return true; |
| 131 | + } catch (err) { |
| 132 | + console.error("MySQL Adapter: Error while closing connection", err); |
| 133 | + return false; |
| 134 | + } |
| 135 | +}; |
| 136 | + |
| 137 | +exports.closePool = async (poolAlias) => { |
| 138 | + try { |
| 139 | + if (pools[poolAlias]) { |
| 140 | + const poolConn = pools[poolAlias]; |
| 141 | + delete pools[poolAlias]; |
| 142 | + await poolConn.end((err) => { |
| 143 | + if (err) { |
| 144 | + console.error( |
| 145 | + `Error while closing connection pool ${poolAlias}`, |
| 146 | + err |
| 147 | + ); |
| 148 | + } |
| 149 | + }); |
| 150 | + return true; |
| 151 | + } else { |
| 152 | + return true; |
| 153 | + } |
| 154 | + } catch (err) { |
| 155 | + console.error("MySQL Adapter: Error while closing connection", err); |
| 156 | + return false; |
| 157 | + } |
| 158 | +}; |
0 commit comments