This commit is contained in:
爱喝水的木子
2026-03-13 16:28:51 +08:00
commit bfdf4843e1
38 changed files with 9490 additions and 0 deletions

24
lib/mongo.ts Normal file
View 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;
}