55 lines
1.3 KiB
JavaScript
55 lines
1.3 KiB
JavaScript
// node_api/config/database.js
|
|
const { Sequelize } = require('sequelize');
|
|
const { logger } = require('../utils/logger');
|
|
|
|
// Database configuration
|
|
const sequelize = new Sequelize({
|
|
dialect: 'postgres',
|
|
host: process.env.DB_HOST || 'localhost',
|
|
port: process.env.DB_PORT || 5432,
|
|
database: process.env.DB_DATABASE || 'directus',
|
|
username: process.env.DB_USERNAME || 'directus',
|
|
password: process.env.DB_PASSWORD || 'directus',
|
|
logging: (msg) => logger.debug(msg),
|
|
pool: {
|
|
max: 10,
|
|
min: 0,
|
|
acquire: 30000,
|
|
idle: 10000
|
|
}
|
|
});
|
|
|
|
// Test database connection
|
|
async function testConnection() {
|
|
try {
|
|
await sequelize.authenticate();
|
|
logger.info('Database connection established successfully');
|
|
return true;
|
|
} catch (error) {
|
|
logger.error('Unable to connect to database:', error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Initialize database models
|
|
async function initializeModels() {
|
|
try {
|
|
// Import models
|
|
const { JobStatus } = require('../models/JobStatus');
|
|
|
|
// Sync models (create tables if they don't exist)
|
|
await sequelize.sync();
|
|
|
|
logger.info('Database models initialized successfully');
|
|
return true;
|
|
} catch (error) {
|
|
logger.error('Failed to initialize database models:', error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
sequelize,
|
|
testConnection,
|
|
initializeModels
|
|
}; |