blob: 1fc60d435d8f7ad108fa79164b5255470b411723 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
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");
}
interface Connection {
isConnected?: number;
}
const connection: Connection = {};
async function dbConnect(): Promise<void> {
if (connection.isConnected) {
return;
}
try {
const db = await mongoose.connect(MONGODB_URI!);
connection.isConnected = db.connections[0].readyState;
console.log("MongoDB connected successfully");
} catch (error) {
console.error("MongoDB connection error:", error);
throw error;
}
}
export default dbConnect;
|