Files
nxxmdata/government-backend/config/database.js

47 lines
1.0 KiB
JavaScript
Raw Normal View History

2025-09-17 18:04:28 +08:00
const { Sequelize } = require('sequelize');
2025-09-25 17:43:54 +08:00
const config = require('./index.js');
2025-09-17 18:04:28 +08:00
const sequelize = new Sequelize(
2025-09-25 17:43:54 +08:00
config.DB_CONFIG.database,
config.DB_CONFIG.user,
config.DB_CONFIG.password,
2025-09-17 18:04:28 +08:00
{
2025-09-25 17:43:54 +08:00
host: config.DB_CONFIG.host,
port: config.DB_CONFIG.port,
dialect: config.DB_CONFIG.dialect,
2025-09-17 18:04:28 +08:00
logging: process.env.NODE_ENV === 'development' ? console.log : false,
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},
define: {
timestamps: true,
paranoid: true,
underscored: true,
freezeTableName: true
},
timezone: '+08:00' // 设置为中国时区
}
);
// 测试数据库连接
async function testConnection() {
try {
await sequelize.authenticate();
console.log('数据库连接成功');
2025-09-25 17:43:54 +08:00
return true;
2025-09-17 18:04:28 +08:00
} catch (error) {
console.error('数据库连接失败:', error);
2025-09-25 17:43:54 +08:00
return false;
2025-09-17 18:04:28 +08:00
}
}
// 先导出sequelize实例
const db = sequelize;
2025-09-17 18:04:28 +08:00
// 再添加测试连接方法
db.testConnection = testConnection;
module.exports = db;