-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
35 lines (32 loc) · 783 Bytes
/
db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit: 10,
database: 'pollposition',
host: 'localhost',
user: 'pollposition',
password: 'pollposition'
});
/**
* Simple wrapper to query the db.
* Will use the underlying connection pooling.
* @param {string} q - the SQL-query
* @param {function} cb - the callback if the query is successful. Receives the result as argument.
* @param {function} errCb - the callback if the query is not successful. Error is the first argument.
*/
function query(q, cb, errCb) {
'use strict';
pool.query(q, function(err, rows, fields) {
if (err) {
if (errCb) {
errCb(err);
}
else {
console.warn('sql error', err);
}
}
else {
cb(rows, fields);
}
});
}
module.exports = {query:query};