-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql.js
73 lines (63 loc) · 1.72 KB
/
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var mysql = require('mysql');
var connection = mysql.createConnection({
host : process.env.MYSQL_HOST,
user : process.env.MYSQL_USER,
password : process.env.MYSQL_PASSWORD,
database : process.env.MYSQL_DATABASE,
dateStrings : 'true',
port : process.env.MYSQL_PORT,
acquireTimeout: 30000, //30 secs
});
connection.connect();
module.exports = {
async do(query,value) {
return new Promise((resolve, reject) => {
connection.query(query,value, function(err, rows, fields)
{
if (err) {
console.log(err);
return reject(err);
}
return resolve(rows);
})
});
},
async beginTransaction(){
return new Promise((resolve, reject) => {
connection.beginTransaction(function(err){
if(err) return reject(err);
return resolve();
})
});
},
async commitTransaction(){
return new Promise((resolve, reject) => {
connection.commit();
return resolve();
});
},
async rollbackTransaction(){
return new Promise((resolve, reject) => {
connection.rollback();
return resolve();
});
},
nest(key,values,rows){
for(var i=0;i<rows.length;i++){
var str = new Object();
str.id = rows[i][key];
for(var j=0;j<values.length;j++){
str[values[j]] = rows[i][values[j]];
delete rows[i][values[j]];
}
rows[i][key] = str;
}
return rows;
},
jsonify(key,rows){
for(var i=0;i<rows.length;i++){
rows[i][key] = JSON.parse(rows[i][key]);
}
return rows;
}
}