A useful little snippet of code to manage connections within a Node / MySQL setup
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | function query(sql, args = null) { return new Promise((resolve, reject) => { logger.trace('query' + sql); logger.debug('Acquiring connection...'); pool.getConnection((err, connection) => { if (err) { logger.error(err, 'query connection error!'); } else { connection.query(sql, args, (err, rows) => { if (err) return reject(err); resolve(rows); }); } logger.debug('Releasing connection...'); connection.release(); }); }); } // Use const result = await query ("SELECT * FROM XXXX"); |