Last active
August 29, 2015 14:23
-
-
Save rreimi/5a3a125b19b50c60f5b0 to your computer and use it in GitHub Desktop.
Node mysql wrapper
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
//#db.js | |
var mysql = require('mysql'); | |
var pool = mysql.createPool({ | |
connectionLimit : 100, //important | |
host : 'localhost', | |
user : 'root', | |
password : 'admin', | |
database : 'fantasy', | |
debug : false | |
}); | |
function query(query, callback) { | |
pool.getConnection(function(err, conn){ | |
if (err) { | |
conn.release(); | |
callback(err); | |
} else { | |
conn.query(query, function(err, rows, fields) { | |
conn.release(); | |
if (err) throw err; | |
callback(err, rows, fields); | |
}); | |
} | |
}); | |
} | |
function statement(query, params, callback) { | |
pool.getConnection(function(err, conn){ | |
if (err) { | |
conn.release(); | |
callback(err); | |
} else { | |
conn.query(query, params, function(err, rows, fields) { | |
conn.release(); | |
if (err) throw err; | |
callback(err, rows, fields); | |
}); | |
} | |
}); | |
} | |
var db = {} | |
db.query = query; | |
db.statement = statement; | |
module.exports = db; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment