- Update database.js to support individual environment variables for Docker - Create .env file template with required environment variables - Fix backend container database host configuration - Add connection logging for debugging 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
52 lines
No EOL
1.6 KiB
JavaScript
52 lines
No EOL
1.6 KiB
JavaScript
const { Sequelize } = require('sequelize');
|
|
require('dotenv').config();
|
|
|
|
// Support both DATABASE_URL and individual environment variables
|
|
let sequelize;
|
|
|
|
if (process.env.DATABASE_URL) {
|
|
// Use DATABASE_URL if provided (for local development)
|
|
sequelize = new Sequelize(process.env.DATABASE_URL, {
|
|
dialect: 'postgres',
|
|
logging: process.env.NODE_ENV === 'development' ? console.log : false,
|
|
pool: {
|
|
max: 10,
|
|
min: 0,
|
|
acquire: 30000,
|
|
idle: 10000,
|
|
},
|
|
});
|
|
} else {
|
|
// Use individual environment variables (for Docker deployment)
|
|
const dbConfig = {
|
|
database: process.env.DATABASE_NAME || 'ideas_gen_prod',
|
|
username: process.env.DATABASE_USER || 'ideas_admin',
|
|
password: process.env.DATABASE_PASSWORD,
|
|
host: process.env.DATABASE_HOST || 'localhost',
|
|
port: process.env.DATABASE_PORT || 5432,
|
|
dialect: 'postgres',
|
|
logging: process.env.NODE_ENV === 'development' ? console.log : false,
|
|
pool: {
|
|
max: 10,
|
|
min: 0,
|
|
acquire: 30000,
|
|
idle: 10000,
|
|
},
|
|
};
|
|
|
|
console.log(`🔗 Connecting to database: ${dbConfig.username}@${dbConfig.host}:${dbConfig.port}/${dbConfig.database}`);
|
|
|
|
sequelize = new Sequelize(dbConfig.database, dbConfig.username, dbConfig.password, dbConfig);
|
|
}
|
|
|
|
const testConnection = async () => {
|
|
try {
|
|
await sequelize.authenticate();
|
|
console.log('✅ Database connection established successfully.');
|
|
} catch (error) {
|
|
console.error('❌ Unable to connect to the database:', error);
|
|
process.exit(1);
|
|
}
|
|
};
|
|
|
|
module.exports = { sequelize, testConnection }; |