Refactor code structure for improved readability and maintainability

This commit is contained in:
2026-03-14 02:25:36 +07:00
parent 7f7ee254d8
commit e345d9ccce
2 changed files with 7 additions and 8 deletions
+6 -7
View File
@@ -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
})
}