Files
pmrx-gm/server/src/utils/db.util.ts
kubbo 2d46b89d28 build: 更新项目结构和配置
- 移除 .gitignore 中的多个文件和目录
- 删除 README.md 文件
- 移除 app.vue 文件
- 删除 nuxt.config.ts 文件
2025-04-24 11:59:10 +08:00

35 lines
976 B
TypeScript

import mysql from 'mysql';
import { cdkDbConfig } from '../config/db.config';
// 创建连接池
const pool = mysql.createPool(cdkDbConfig);
// 获取数据库连接
export const getConnection = (): Promise<mysql.PoolConnection> => {
return new Promise((resolve, reject) => {
pool.getConnection((err, connection) => {
if (err) {
reject(err);
} else {
resolve(connection);
}
});
});
};
// 执行查询
export const query = async (sql: string, values?: any[], connection?: mysql.PoolConnection): Promise<any> => {
if (!connection) {
connection = await getConnection()
}
return new Promise((resolve, reject) => {
connection.query(sql, values, (err, results) => {
connection.release(); // 释放连接
if (err) {
reject(err);
} else {
resolve(results);
}
});
});
};