diff options
Diffstat (limited to 'lib/dbConnect.js')
-rw-r--r-- | lib/dbConnect.js | 46 |
1 files changed, 0 insertions, 46 deletions
diff --git a/lib/dbConnect.js b/lib/dbConnect.js deleted file mode 100644 index a58ef79..0000000 --- a/lib/dbConnect.js +++ /dev/null @@ -1,46 +0,0 @@ -import mongoose from 'mongoose' - -// const MONGODB_URI = process.env.MYAPPS_MONGODB_URI -const MONGODB_URI = 'mongodb://localhost:27017/myapps' - -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} -} - -async function dbConnect() { - if (cached.conn) { - return cached.conn - } - - if (!cached.promise) { - const opts = { - useNewUrlParser: true, - useUnifiedTopology: true, - bufferCommands: false, - bufferMaxEntries: 0, - useFindAndModify: false, - useCreateIndex: true, - } - - cached.promise = mongoose.connect(MONGODB_URI, opts).then((mongoose) => { - return mongoose - }) - } - cached.conn = await cached.promise - return cached.conn -} - -export default dbConnect |