Last active
August 17, 2017 14:14
-
-
Save jazzyjackson/bf494c79606b0bbd78a56922f30c288e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mysql = require('mysql') | |
const config = ini.parse(fs.readFileSync('../credentials.ini', 'utf-8')) /* only happens once on server start */ | |
const db = mysql.createPool(config.labsdb) | |
tryMySQL(input){ | |
return new Promise((resolve, reject) => { | |
if(!input.mysql) return resolve(input) | |
db.getConnection((err, conn) => { | |
if(err) reject({dbError: err}) | |
conn.query(input.mysql) | |
.on('end', () => conn.release() || resolve(input)) | |
.on('error', err => reject({queryError: err})) | |
.on('fields', fields => this.send({fields})) | |
.on('result', row => this.send({row})) | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very good for this.send to return the result as well so these try-promises can go downstream