import mongoose from 'mongoose'; const MONGODB_URI = process.env.DB_URI || ''; if (!MONGODB_URI) { throw new Error( 'Please define the DB_URI environment variable inside .env' ); } /** * Global is used here to maintain a cached connection across hot reloads * in development. This prevents connections growing exponentially * during API Route usage. */ let cached = global.mongoose; if (!cached) { cached = global.mongoose = { conn: null, promise: null }; } async function dbConnect() { if (cached.conn) { return cached.conn; } if (!cached.promise) { const opts = { bufferCommands: false, maxPoolSize: 10, // Maintain up to 10 socket connections serverSelectionTimeoutMS: 5000, // Keep trying to send operations for 5 seconds socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity }; cached.promise = mongoose.connect(MONGODB_URI, opts).then((mongoose) => { console.log('Connected to MongoDB'); return mongoose; }).catch((error) => { console.error('Error connecting to MongoDB:', error); throw error; }); } try { cached.conn = await cached.promise; } catch (e) { cached.promise = null; throw e; } return cached.conn; } export default dbConnect;