35 lines
920 B
JavaScript
35 lines
920 B
JavaScript
const mysql = require('mysql');
|
|
const { cdkDbConfig,gameDbConfig } = require('../config/db.config');
|
|
|
|
// 创建连接池
|
|
const pool = mysql.createPool(gameDbConfig);
|
|
|
|
// 获取数据库连接
|
|
exports.getConnection = () => {
|
|
return new Promise((resolve, reject) => {
|
|
pool.getConnection((err, connection) => {
|
|
if (err) {
|
|
reject(err);
|
|
} else {
|
|
resolve(connection);
|
|
}
|
|
});
|
|
});
|
|
};
|
|
|
|
// 执行查询
|
|
exports.query = async (sql, values = [], connection) => {
|
|
if (!connection) {
|
|
connection = await exports.getConnection();
|
|
}
|
|
return new Promise((resolve, reject) => {
|
|
connection.query(sql, values, (err, results) => {
|
|
connection.release(); // 释放连接
|
|
if (err) {
|
|
reject(err);
|
|
} else {
|
|
resolve(results);
|
|
}
|
|
});
|
|
});
|
|
}; |