diff --git a/lib/database.ts b/lib/database.ts index 71f9ca7..f67d412 100644 --- a/lib/database.ts +++ b/lib/database.ts @@ -1,14 +1,38 @@ import mongoose from 'mongoose'; +const MONGODB_URI = process.env.MONGODB_URI; + +if (!MONGODB_URI) { + throw new Error('Please define the MONGODB_URI environment variable inside .env.local'); +} + +/** + * 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 }; +} + export default async function dbConnect() { - // check if we have a connection to the database or if it's currently - // connecting or disconnecting (readyState 1, 2 and 3) - if (mongoose.connection.readyState >= 1) { - return; + if (cached.conn) { + return cached.conn; } - return mongoose.connect(process.env.MONGODB_URI, { - useNewUrlParser: true, - useUnifiedTopology: true - }); + if (!cached.promise) { + const opts = { + bufferCommands: false + }; + + cached.promise = mongoose.connect(MONGODB_URI, opts).then((mongoose) => { + return mongoose; + }); + } + + cached.conn = await cached.promise; + + return cached.conn; }