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

41 lines
1.1 KiB
JavaScript

//引入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'));
let databaseName = null;
function create({ database }) {
databaseName = database;
}
async function query(sql, params) {
let config = {
...globalConfig[global.dbConfig || 'mysql'],
database: databaseName,
};
// console.log(config);
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();
});
}
module.exports = {
setConnectionLimit: () => { },
create: create,
query: query,
close: () => { },
}