-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
48 lines (43 loc) · 1.05 KB
/
database.js
File metadata and controls
48 lines (43 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* Database configuration and initialization
*/
const { Sequelize } = require('sequelize');
const config = require('./index');
const winston = require('winston');
// Get environment-specific database configuration
const env = process.env.NODE_ENV || 'development';
const dbConfig = config.database[env];
// Create Sequelize instance
const sequelize = new Sequelize(
dbConfig.database,
dbConfig.username,
dbConfig.password,
{
host: dbConfig.host,
dialect: dbConfig.dialect,
storage: dbConfig.storage, // For SQLite
logging: dbConfig.logging,
dialectOptions: dbConfig.dialectOptions,
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
}
);
// Test database connection
const testConnection = async () => {
try {
await sequelize.authenticate();
console.log('Database connection has been established successfully.');
return true;
} catch (error) {
console.error('Unable to connect to the database:', error);
return false;
}
};
module.exports = {
sequelize,
testConnection
};