-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_mysql.js
59 lines (46 loc) · 1.13 KB
/
db_mysql.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Wrapper for the mysql library
var mysql = require('mysql');
var config = {
host: 'localhost',
user: 'user',
password: 'password',
database: 'database',
port: 3306,
connectionLimit: 10,
multipleStatements: true
};
function Db() {
var self = this;
var _pool = null;
this.init = function() {
_pool = mysql.createPool(config);
return self;
}
this.escape = function(input) {
return mysql.escape(input);
}
this.queryOne = function(sql, data) {
return this.query(sql, data, true);
}
this.query = function(sql, data, single) {
// return _pool.query(sql, params);
var single = single || false;
return new Promise((resolve, reject) => {
var cb = function(err, qr) {
if (err) {
console.log(">> Db.query() ERROR -> ", err);
return reject({ error: err, query: sql, data: data });
}
if (single && qr.length)
return resolve(qr[0]);
return resolve(qr);
}
if (data)
_pool.query(sql, data, cb);
else
_pool.query(sql, cb);
});
}
return this.init();
}
module.exports = new Db();