forked from nevash-gobin/myAdvisorDev
-
Notifications
You must be signed in to change notification settings - Fork 6
/
db.js
57 lines (52 loc) · 1.72 KB
/
db.js
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
49
50
51
52
53
54
55
56
57
require('dotenv').config()
console.log("db.js files: ", typeof process.env.SYNCED)
const Sequelize = require("sequelize");
// Function to create a Sequelize instance based on the environment
function createSequelizeInstance() {
const env = process.env.NODE_ENV || 'development'; // Default to 'development' if NODE_ENV is not set
// console.log saying "In db.js files, we are in {env} environment and SYNCED is {process.env.SYNCED}"
console.log(`In db.js files, we are in ${env} environment and SYNCED is ${process.env.SYNCED}`);
if (env === 'production') {
// Extracting database connection information from the URL
const regex = /^postgres:\/\/([^:]+):([^@]+)@([^:]+):(\d+)\/(.+)$/;
const matches = RegExp(regex).exec(process.env.POSTGRES_URL);
if (!matches) {
throw new Error('Invalid POSTGRES_URL format');
}
const [_, DB_USER, DB_PASSWORD, DB_HOST, DB_PORT, DB_NAME] = matches;
// PostgreSQL database configuration
return new Sequelize({
dialect: 'postgres',
host: DB_HOST,
port: DB_PORT,
database: DB_NAME,
username: DB_USER,
password: DB_PASSWORD,
pool: {
max: 50,
min: 5,
acquire: 10000,
idle: 5000
},
});
} else if( env === 'development') {
// SQLite database configuration
return new Sequelize({
dialect: 'sqlite',
storage: 'database.sqlite', // Path to your SQLite database file
logging: false,
pool: {
max: 50,
min: 1,
acquire: 10000,
idle: 10000
},
dialectOptions: {
mode: Sequelize.QueryTypes.WAL
}
});
}
}
// Create and export the Sequelize instance
const db = createSequelizeInstance();
module.exports = db;