| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- "use strict";
- var __importDefault = (this && this.__importDefault) || function (mod) {
- return (mod && mod.__esModule) ? mod : { "default": mod };
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.closePool = exports.testConnection = exports.executeQuery = void 0;
- const promise_1 = __importDefault(require("mysql2/promise"));
- const loggerService_1 = require("../services/loggerService");
- let pool = null;
- const getDbConfig = () => ({
- host: process.env.DB_HOST || '192.168.1.17',
- port: parseInt(process.env.DB_PORT || '3306'),
- user: process.env.DB_USER || 'root',
- password: process.env.DB_PASSWORD || '123',
- database: process.env.DB_NAME || 'mqtt_vue_dashboard',
- charset: 'utf8mb4',
- waitForConnections: true,
- connectionLimit: 10,
- queueLimit: 0
- });
- const getPool = () => {
- if (!pool) {
- const dbConfig = getDbConfig();
- console.log('创建数据库连接池:', {
- host: dbConfig.host,
- port: dbConfig.port,
- user: dbConfig.user,
- database: dbConfig.database,
- env_DB_HOST: process.env.DB_HOST,
- env_DB_PORT: process.env.DB_PORT,
- env_DB_USER: process.env.DB_USER,
- env_DB_NAME: process.env.DB_NAME
- });
- pool = promise_1.default.createPool(dbConfig);
- }
- return pool;
- };
- const executeQuery = async (query, params) => {
- try {
- const [rows] = await getPool().execute(query, params);
- loggerService_1.LoggerService.info('数据库查询成功', {
- source: 'database',
- module: 'execute_query',
- details: JSON.stringify({
- query: query.substring(0, 200),
- params: params ? JSON.stringify(params).substring(0, 100) : undefined,
- resultCount: Array.isArray(rows) ? rows.length : 1
- })
- }).catch(err => {
- console.error('数据库查询成功日志写入失败:', err);
- });
- return rows;
- }
- catch (error) {
- console.error('Database query error:', error);
- loggerService_1.LoggerService.error('数据库查询失败', {
- source: 'database',
- module: 'execute_query',
- details: JSON.stringify({
- query: query.substring(0, 200),
- params: params ? JSON.stringify(params).substring(0, 100) : undefined,
- error: error instanceof Error ? error.message : '未知错误'
- })
- }).catch(err => {
- console.error('数据库查询失败日志写入失败:', err);
- });
- throw error;
- }
- };
- exports.executeQuery = executeQuery;
- const testConnection = async () => {
- try {
- const connection = await getPool().getConnection();
- await connection.ping();
- connection.release();
- console.log('Database connection successful');
- loggerService_1.LoggerService.info('数据库连接成功', {
- source: 'database',
- module: 'test_connection',
- details: JSON.stringify({
- host: getDbConfig().host,
- port: getDbConfig().port,
- database: getDbConfig().database,
- user: getDbConfig().user
- })
- }).catch(err => {
- console.error('数据库连接成功日志写入失败:', err);
- });
- return true;
- }
- catch (error) {
- console.error('Database connection failed:', error);
- loggerService_1.LoggerService.error('数据库连接失败', {
- source: 'database',
- module: 'test_connection',
- details: JSON.stringify({
- host: getDbConfig().host,
- port: getDbConfig().port,
- database: getDbConfig().database,
- user: getDbConfig().user,
- error: error instanceof Error ? error.message : '未知错误'
- })
- }).catch(err => {
- console.error('数据库连接失败日志写入失败:', err);
- });
- return false;
- }
- };
- exports.testConnection = testConnection;
- const closePool = async () => {
- try {
- if (pool) {
- await pool.end();
- pool = null;
- console.log('Database connection pool closed');
- }
- }
- catch (error) {
- console.error('Error closing database connection pool:', error);
- }
- };
- exports.closePool = closePool;
- exports.default = getPool;
- //# sourceMappingURL=connection.js.map
|