Files
pmrx-gm/server/src/utils/db.util.js
kubbo c179401357 refactor(server): 更新数据库连接配置
- 引入 gameDbConfig 替代 cdkDbConfig
- 修改数据库连接池初始化,使用新的配置
2025-04-24 12:14:09 +08:00

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);
}
});
});
};