-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
44 lines (38 loc) · 1.12 KB
/
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
36
37
38
39
40
41
42
43
44
const mysql = require('mysql');
const {dbConfig} = require('./config')
var pool = mysql.createPool(dbConfig);
let obj = {};
// 封装一个简易q函数
/**
* 返回一个promise对象, async await使用
* @param {[type]} sql [description]
* @param {[type]} dataArr [description]
* @return {[type]} [description]
*/
obj.q = function(sql,dataArr) {
return new Promise(function(resolve,reject) {
pool.getConnection(function(err, connection) {
// 连接异常
if (err) {
reject(err);
return;
}
// 调试语句
console.log(sql,dataArr);
// 使用连接
connection.query(sql, dataArr, function(error, results, fields) {
// 释放连接会连接池
connection.release();
// CRUD相关异常
if (error) {
reject(error);
return;
}
console.log(results);
resolve(results);
});
});
});
}
// 返回obj对象
module.exports = obj;