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
23 changes: 20 additions & 3 deletions config/database.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
module.exports = {
mongoURI: 'mongodb://localhost/tododb-dev'
}
const mongoose = require('mongoose');

const options = {
useNewUrlParser: true,
useUnifiedTopology: true,
poolSize: 10, // Maintain up to 10 socket connections
serverSelectionTimeoutMS: 5000, // Retry for 5 seconds
socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity
};

// Enable detailed logging for MongoDB queries to track performance
mongoose.set('debug', true);

mongoose.connect(process.env.MONGO_URI || 'mongodb://localhost/tododb-dev', options)
.then(() => console.log('Database connected successfully'))
.catch((err) => {
console.error('Initial connection error:', err);
process.exit(1);
});

5 changes: 4 additions & 1 deletion models/Todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ const TodoSchema = new Schema({
}
});

mongoose.model('todos', TodoSchema);
// Add indexing for faster queries
TodoSchema.index({ title: 1, user: 1 });

mongoose.model('todos', TodoSchema);