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