Files
pmrx-gm/server/src/utils/db.util.js

35 lines
920 B
JavaScript
Raw Normal View History

2025-04-24 12:03:19 +08:00
const mysql = require('mysql');
const { cdkDbConfig,gameDbConfig } = require('../config/db.config');
// 创建连接池
const pool = mysql.createPool(gameDbConfig);
// 获取数据库连接
2025-04-24 12:03:19 +08:00
exports.getConnection = () => {
return new Promise((resolve, reject) => {
pool.getConnection((err, connection) => {
if (err) {
reject(err);
} else {
resolve(connection);
}
});
});
};
// 执行查询
2025-04-24 12:03:19 +08:00
exports.query = async (sql, values = [], connection) => {
if (!connection) {
2025-04-24 12:03:19 +08:00
connection = await exports.getConnection();
}
return new Promise((resolve, reject) => {
connection.query(sql, values, (err, results) => {
connection.release(); // 释放连接
if (err) {
reject(err);
} else {
resolve(results);
}
});
});
};