Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"start": "node dist/server.js",
"lint": "eslint . --ext .ts",
"format": "prettier --write .",
"test": "jest"
"test": "jest",
"prepare": "husky install"
},
"dependencies": {
"bcryptjs": "2.4.3",
Expand Down Expand Up @@ -53,5 +54,11 @@
"ts-jest": "^29.4.11",
"ts-node": "10.9.2",
"typescript": "5.2.2"
},
"lint-staged": {
"*.ts": [
"eslint --fix",
"prettier --write"
]
}
}
21 changes: 20 additions & 1 deletion src/config/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ export const connectDatabase = async (): Promise<void> => {
try {
const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/swiftchain';

await mongoose.connect(mongoUri);
await mongoose.connect(mongoUri, {
maxPoolSize: 10, // Maximum number of connections in the pool
minPoolSize: 2, // Minimum number of connections in the pool
serverSelectionTimeoutMS: 5000, // Timeout after 5s instead of 30s
socketTimeoutMS: 45000, // Close sockets after 45s of inactivity
});

logger.info('✅ Connected to MongoDB successfully');

Expand All @@ -16,6 +21,20 @@ export const connectDatabase = async (): Promise<void> => {
mongoose.connection.on('disconnected', () => {
logger.warn('MongoDB disconnected');
});

mongoose.connection.on('reconnected', () => {
logger.info('✅ MongoDB reconnected');
});

mongoose.connection.on('connected', () => {
logger.info(`MongoDB connected to ${mongoose.connection.host}`);
});

process.on('SIGINT', async () => {
await mongoose.connection.close();
logger.info('MongoDB connection closed through app termination');
process.exit(0);
});
} catch (error) {
logger.error('❌ Failed to connect to MongoDB:', error);
process.exit(1);
Expand Down