-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.replit.js
More file actions
62 lines (53 loc) · 1.46 KB
/
database.replit.js
File metadata and controls
62 lines (53 loc) · 1.46 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* Database configuration for Replit deployment
*/
const { Sequelize } = require('sequelize');
const path = require('path');
const fs = require('fs');
const logger = require('../utils/logger');
// Determine database path
const dbPath = process.env.DATABASE_URL || 'sqlite:./database.sqlite';
// Create database directory if it doesn't exist (for SQLite)
if (dbPath.startsWith('sqlite:')) {
const sqlitePath = dbPath.replace('sqlite:', '');
const dbDir = path.dirname(sqlitePath);
if (!fs.existsSync(dbDir)) {
fs.mkdirSync(dbDir, { recursive: true });
logger.info(`Created database directory: ${dbDir}`);
}
}
// Initialize Sequelize with SQLite for Replit
const sequelize = new Sequelize(dbPath, {
dialect: 'sqlite',
logging: (msg) => logger.debug(msg),
define: {
timestamps: true,
underscored: false
},
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},
// SQLite-specific options
storage: dbPath.replace('sqlite:', '')
});
// Test database connection
const testConnection = async () => {
try {
await sequelize.authenticate();
logger.info('Database connection has been established successfully.');
// Sync models with database
await sequelize.sync();
logger.info('Database models synchronized successfully.');
return true;
} catch (error) {
logger.error('Unable to connect to the database:', error);
return false;
}
};
module.exports = {
sequelize,
testConnection
};