connection.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.closePool = exports.testConnection = exports.executeQuery = void 0;
  7. const promise_1 = __importDefault(require("mysql2/promise"));
  8. const loggerService_1 = require("../services/loggerService");
  9. let pool = null;
  10. const getDbConfig = () => ({
  11. host: process.env.DB_HOST || '192.168.1.17',
  12. port: parseInt(process.env.DB_PORT || '3306'),
  13. user: process.env.DB_USER || 'root',
  14. password: process.env.DB_PASSWORD || '123',
  15. database: process.env.DB_NAME || 'mqtt_vue_dashboard',
  16. charset: 'utf8mb4',
  17. waitForConnections: true,
  18. connectionLimit: 10,
  19. queueLimit: 0
  20. });
  21. const getPool = () => {
  22. if (!pool) {
  23. const dbConfig = getDbConfig();
  24. console.log('创建数据库连接池:', {
  25. host: dbConfig.host,
  26. port: dbConfig.port,
  27. user: dbConfig.user,
  28. database: dbConfig.database,
  29. env_DB_HOST: process.env.DB_HOST,
  30. env_DB_PORT: process.env.DB_PORT,
  31. env_DB_USER: process.env.DB_USER,
  32. env_DB_NAME: process.env.DB_NAME
  33. });
  34. pool = promise_1.default.createPool(dbConfig);
  35. }
  36. return pool;
  37. };
  38. const executeQuery = async (query, params) => {
  39. try {
  40. const [rows] = await getPool().execute(query, params);
  41. loggerService_1.LoggerService.info('数据库查询成功', {
  42. source: 'database',
  43. module: 'execute_query',
  44. details: JSON.stringify({
  45. query: query.substring(0, 200),
  46. params: params ? JSON.stringify(params).substring(0, 100) : undefined,
  47. resultCount: Array.isArray(rows) ? rows.length : 1
  48. })
  49. }).catch(err => {
  50. console.error('数据库查询成功日志写入失败:', err);
  51. });
  52. return rows;
  53. }
  54. catch (error) {
  55. console.error('Database query error:', error);
  56. loggerService_1.LoggerService.error('数据库查询失败', {
  57. source: 'database',
  58. module: 'execute_query',
  59. details: JSON.stringify({
  60. query: query.substring(0, 200),
  61. params: params ? JSON.stringify(params).substring(0, 100) : undefined,
  62. error: error instanceof Error ? error.message : '未知错误'
  63. })
  64. }).catch(err => {
  65. console.error('数据库查询失败日志写入失败:', err);
  66. });
  67. throw error;
  68. }
  69. };
  70. exports.executeQuery = executeQuery;
  71. const testConnection = async () => {
  72. try {
  73. const connection = await getPool().getConnection();
  74. await connection.ping();
  75. connection.release();
  76. console.log('Database connection successful');
  77. loggerService_1.LoggerService.info('数据库连接成功', {
  78. source: 'database',
  79. module: 'test_connection',
  80. details: JSON.stringify({
  81. host: getDbConfig().host,
  82. port: getDbConfig().port,
  83. database: getDbConfig().database,
  84. user: getDbConfig().user
  85. })
  86. }).catch(err => {
  87. console.error('数据库连接成功日志写入失败:', err);
  88. });
  89. return true;
  90. }
  91. catch (error) {
  92. console.error('Database connection failed:', error);
  93. loggerService_1.LoggerService.error('数据库连接失败', {
  94. source: 'database',
  95. module: 'test_connection',
  96. details: JSON.stringify({
  97. host: getDbConfig().host,
  98. port: getDbConfig().port,
  99. database: getDbConfig().database,
  100. user: getDbConfig().user,
  101. error: error instanceof Error ? error.message : '未知错误'
  102. })
  103. }).catch(err => {
  104. console.error('数据库连接失败日志写入失败:', err);
  105. });
  106. return false;
  107. }
  108. };
  109. exports.testConnection = testConnection;
  110. const closePool = async () => {
  111. try {
  112. if (pool) {
  113. await pool.end();
  114. pool = null;
  115. console.log('Database connection pool closed');
  116. }
  117. }
  118. catch (error) {
  119. console.error('Error closing database connection pool:', error);
  120. }
  121. };
  122. exports.closePool = closePool;
  123. exports.default = getPool;
  124. //# sourceMappingURL=connection.js.map