63 lines
1.7 KiB
JavaScript
63 lines
1.7 KiB
JavaScript
const { Sequelize } = require("sequelize");
|
|
|
|
// Load environment variables based on NODE_ENV
|
|
// This ensures variables are available for both CLI and programmatic usage
|
|
if (!process.env.DB_NAME && process.env.NODE_ENV) {
|
|
const dotenv = require("dotenv");
|
|
const envFile = `.env.${process.env.NODE_ENV}`;
|
|
const result = dotenv.config({ path: envFile });
|
|
if (result.error && process.env.NODE_ENV !== "production") {
|
|
console.warn(
|
|
`Warning: Could not load ${envFile}, using existing environment variables`
|
|
);
|
|
}
|
|
}
|
|
|
|
// Database configuration object
|
|
// Used by both Sequelize CLI and programmatic initialization
|
|
const dbConfig = {
|
|
username: process.env.DB_USER,
|
|
password: process.env.DB_PASSWORD,
|
|
database: process.env.DB_NAME,
|
|
host: process.env.DB_HOST,
|
|
port: process.env.DB_PORT || 5432,
|
|
dialect: "postgres",
|
|
logging: false,
|
|
pool: {
|
|
max: 5,
|
|
min: 0,
|
|
acquire: 10000,
|
|
idle: 10000,
|
|
},
|
|
};
|
|
|
|
// Configuration for Sequelize CLI (supports multiple environments)
|
|
// All environments use the same configuration from environment variables
|
|
const cliConfig = {
|
|
development: dbConfig,
|
|
dev: dbConfig,
|
|
test: dbConfig,
|
|
qa: dbConfig,
|
|
production: dbConfig,
|
|
prod: dbConfig,
|
|
};
|
|
|
|
// Create Sequelize instance for programmatic use
|
|
const sequelize = new Sequelize(
|
|
dbConfig.database,
|
|
dbConfig.username,
|
|
dbConfig.password,
|
|
{
|
|
host: dbConfig.host,
|
|
port: dbConfig.port,
|
|
dialect: dbConfig.dialect,
|
|
logging: dbConfig.logging,
|
|
pool: dbConfig.pool,
|
|
}
|
|
);
|
|
|
|
// Export the sequelize instance as default (for backward compatibility)
|
|
// Also export all environment configs for Sequelize CLI
|
|
module.exports = sequelize;
|
|
Object.assign(module.exports, cliConfig);
|