Last active
March 7, 2018 19:10
-
-
Save mkhizeryounas/647aebf07f37aabd5cddce66c45dcb25 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
/** | |
* Auther: mkhizeryounas | |
* Usage: db.query(queryString, paramArray, (err, res) => { console.log(res) }); | |
*/ | |
var mysql = require("mysql"); | |
var keys = require('./keys'); | |
var pool = mysql.createPool({ | |
host : keys.mysql.host, | |
port : keys.mysql.port, | |
user : keys.mysql.user, | |
password : keys.mysql.password, | |
database : keys.mysql.database, | |
}); | |
module.exports = { | |
getConnection (callback) { | |
pool.getConnection((err, conn) => { | |
if(err) { | |
return callback(err); | |
} | |
callback(err, conn); | |
}); | |
}, | |
query (query, params=[], cb) { | |
this.getConnection((err, conn) => { | |
conn.release(); | |
if(err) return cb(err, null); | |
conn.query(query, params, (err, res) => { | |
if(err) return cb(err, null); | |
return cb(null, res); | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Express (NodeJS) Mysql connection implementation
Usage
Setup mysql DB connection host, port, username, password & DB name in the dist file.