-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodb.ts
43 lines (35 loc) · 1.05 KB
/
mongodb.ts
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
31
32
33
34
35
36
37
38
39
40
41
42
43
import mongoose, { Connection, Mongoose } from "mongoose";
const URI = process.env.MONGODB_URI || ""; // Replace with your MongoDB connection string
const DbName = process.env.Db_Name; // Replace with your database name
let cachedDb: Connection | null = null;
export async function ConnectMongoDb(): Promise<void> {
try {
if (!URI) {
throw new Error("MONGODB_URI environment variable is not defined");
}
if (cachedDb) {
console.log("Using cached database connection");
return;
}
await mongoose.connect(URI, {
dbName: DbName,
});
cachedDb = mongoose.connection;
console.log("Connected successfully to the database");
} catch (error) {
console.error("Failed to connect to the database:", error);
throw error;
}
}
export function getCachedDb(): Connection {
if (!cachedDb) {
throw new Error("Database connection has not been established");
}
return cachedDb;
}
export function closeCachedDb(): Promise<void> {
if (cachedDb) {
return cachedDb.close();
}
return Promise.resolve();
}