1
0
Code Issues Pull Requests Projects Releases Wiki Activity GitHub Gitee
tools/utils/dbUtils.js

41 lines
1.1 KiB
JavaScript
Raw Normal View History

2022-09-30 00:51:08 +08:00
//引入MySQL
const mysql = require('mysql');
const fs = require('fs');
const path = require('path');
let globalConfig = JSON.parse(fs.readFileSync(path.join(__dirname, '../config.json'), 'utf8'));
2022-10-01 19:45:49 +08:00
let databaseName = null;
2022-09-30 00:51:08 +08:00
2022-10-01 19:45:49 +08:00
function create({ database }) {
databaseName = database;
2022-09-30 08:06:14 +08:00
}
async function query(sql, params) {
2022-09-30 00:52:56 +08:00
let config = {
2022-10-05 11:41:30 +08:00
...globalConfig[global.dbConfig || 'mysql'],
2022-10-01 19:45:49 +08:00
database: databaseName,
2022-09-30 00:52:56 +08:00
};
2022-09-30 08:06:14 +08:00
// console.log(config);
2022-09-30 00:52:56 +08:00
2022-09-30 08:06:14 +08:00
return await new Promise(function (resolve, reject) {
//通过MySQL中方法创建连接对象
var connection = mysql.createConnection(config);
//开始连接
connection.connect();
//执行SQL语句 (添加、删除、更新、查询)
connection.query(sql, params, (err, data) => {
if (err) reject(err);
// console.log(data);
resolve(data);
})
//最后需要关闭连接
connection.end();
});
2022-09-30 00:51:08 +08:00
}
module.exports = {
2022-10-01 19:45:49 +08:00
setConnectionLimit: () => { },
2022-09-30 08:06:14 +08:00
create: create,
2022-09-30 00:51:08 +08:00
query: query,
2022-10-01 19:45:49 +08:00
close: () => { },
2022-09-30 00:51:08 +08:00
}