OPC
This commit is contained in:
24
lib/mongo.ts
Normal file
24
lib/mongo.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { Db, MongoClient } from "mongodb";
|
||||
|
||||
const uri = process.env.MONGODB_URI;
|
||||
const dbName = process.env.MONGODB_DB || "pushinfo";
|
||||
|
||||
if (!uri) {
|
||||
throw new Error("Missing MONGODB_URI in environment variables");
|
||||
}
|
||||
|
||||
let client: MongoClient | null = null;
|
||||
let db: Db | null = null;
|
||||
|
||||
export async function getDb(): Promise<Db> {
|
||||
if (db) return db;
|
||||
|
||||
if (!client) {
|
||||
client = new MongoClient(uri, { serverSelectionTimeoutMS: 5000 });
|
||||
}
|
||||
if (!client.topology?.isConnected()) {
|
||||
await client.connect();
|
||||
}
|
||||
db = client.db(dbName);
|
||||
return db;
|
||||
}
|
||||
Reference in New Issue
Block a user