Refactor code structure for improved readability and maintainability
This commit is contained in:
+6
-7
@@ -1,11 +1,5 @@
|
||||
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")
|
||||
}
|
||||
|
||||
let cached = (global as any).mongoose
|
||||
|
||||
if (!cached) {
|
||||
@@ -13,6 +7,11 @@ if (!cached) {
|
||||
}
|
||||
|
||||
async function connectToMongoDB() {
|
||||
const mongodbUri = process.env.MONGODB_URI
|
||||
if (!mongodbUri) {
|
||||
throw new Error("Please define the MONGODB_URI environment variable")
|
||||
}
|
||||
|
||||
if (cached.conn) {
|
||||
return cached.conn
|
||||
}
|
||||
@@ -22,7 +21,7 @@ async function connectToMongoDB() {
|
||||
bufferCommands: false,
|
||||
}
|
||||
|
||||
cached.promise = mongoose.connect(MONGODB_URI!, opts).then((mongoose) => {
|
||||
cached.promise = mongoose.connect(mongodbUri, opts).then((mongoose) => {
|
||||
return mongoose
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user