first commit
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
const logger = require('../utils/logger');
|
||||
|
||||
const databaseConfig = {
|
||||
development: {
|
||||
host: process.env.DB_HOST || 'localhost',
|
||||
port: process.env.DB_PORT || 5432,
|
||||
database: process.env.DB_NAME || 'reason_flow_dev',
|
||||
username: process.env.DB_USER || 'postgres',
|
||||
password: process.env.DB_PASSWORD || 'password',
|
||||
dialect: 'postgres',
|
||||
logging: (msg) => logger.debug(msg),
|
||||
pool: {
|
||||
max: 5,
|
||||
min: 0,
|
||||
acquire: 30000,
|
||||
idle: 10000
|
||||
},
|
||||
define: {
|
||||
timestamps: true,
|
||||
underscored: true,
|
||||
freezeTableName: true
|
||||
}
|
||||
},
|
||||
|
||||
test: {
|
||||
host: process.env.DB_HOST || 'localhost',
|
||||
port: process.env.DB_PORT || 5432,
|
||||
database: process.env.DB_NAME || 'reason_flow_test',
|
||||
username: process.env.DB_USER || 'postgres',
|
||||
password: process.env.DB_PASSWORD || 'password',
|
||||
dialect: 'postgres',
|
||||
logging: false,
|
||||
pool: {
|
||||
max: 5,
|
||||
min: 0,
|
||||
acquire: 30000,
|
||||
idle: 10000
|
||||
},
|
||||
define: {
|
||||
timestamps: true,
|
||||
underscored: true,
|
||||
freezeTableName: true
|
||||
}
|
||||
},
|
||||
|
||||
production: {
|
||||
host: process.env.DB_HOST,
|
||||
port: process.env.DB_PORT || 5432,
|
||||
database: process.env.DB_NAME,
|
||||
username: process.env.DB_USER,
|
||||
password: process.env.DB_PASSWORD,
|
||||
dialect: 'postgres',
|
||||
logging: false,
|
||||
pool: {
|
||||
max: 20,
|
||||
min: 5,
|
||||
acquire: 60000,
|
||||
idle: 10000
|
||||
},
|
||||
define: {
|
||||
timestamps: true,
|
||||
underscored: true,
|
||||
freezeTableName: true
|
||||
},
|
||||
dialectOptions: {
|
||||
ssl: process.env.DB_SSL === 'true' ? {
|
||||
require: true,
|
||||
rejectUnauthorized: false
|
||||
} : false
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const getConfig = () => {
|
||||
const env = process.env.NODE_ENV || 'development';
|
||||
const config = databaseConfig[env];
|
||||
|
||||
if (!config) {
|
||||
throw new Error(`Database configuration not found for environment: ${env}`);
|
||||
}
|
||||
|
||||
// Validate required fields for production
|
||||
if (env === 'production') {
|
||||
const requiredFields = ['host', 'database', 'username', 'password'];
|
||||
const missingFields = requiredFields.filter(field => !config[field]);
|
||||
|
||||
if (missingFields.length > 0) {
|
||||
throw new Error(`Missing required database configuration: ${missingFields.join(', ')}`);
|
||||
}
|
||||
}
|
||||
|
||||
logger.info(`Using database configuration for environment: ${env}`);
|
||||
return config;
|
||||
};
|
||||
|
||||
const validateConnection = async (sequelize) => {
|
||||
try {
|
||||
await sequelize.authenticate();
|
||||
logger.info('Database connection validated successfully');
|
||||
return true;
|
||||
} catch (error) {
|
||||
logger.error('Database connection validation failed:', error);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const getDatabaseUrl = () => {
|
||||
const config = getConfig();
|
||||
const { host, port, database, username, password } = config;
|
||||
|
||||
return `postgresql://${username}:${password}@${host}:${port}/${database}`;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
databaseConfig,
|
||||
getConfig,
|
||||
validateConnection,
|
||||
getDatabaseUrl
|
||||
};
|
||||
Reference in New Issue
Block a user