diff --git a/package.json b/package.json index eeeb8c4..7245e2f 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" + ] } } diff --git a/src/config/database.ts b/src/config/database.ts index 65bb270..8283b38 100644 --- a/src/config/database.ts +++ b/src/config/database.ts @@ -5,7 +5,12 @@ export const connectDatabase = async (): Promise => { 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'); @@ -16,6 +21,20 @@ export const connectDatabase = async (): Promise => { 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);