| 1234567891011121314151617181920212223242526 |
- // lib/db.js
- import { MongoClient } from "mongodb";
- const uri = process.env.MONGODB_URI;
- if (!uri) {
- throw new Error("Bitte setze MONGODB_URI in .env.docker / .env.local");
- }
- // Wir cachen die Verbindung über globalThis, damit Next.js nicht bei jedem Request
- // eine neue Verbindung aufmacht (wichtig im Dev & bei Hot Reload).
- let client;
- let clientPromise;
- if (!global._mongoClientPromise) {
- client = new MongoClient(uri);
- global._mongoClientPromise = client.connect();
- }
- clientPromise = global._mongoClientPromise;
- export async function getDb() {
- const client = await clientPromise;
- // Wenn im URI ein DB-Name steht (/rhl-lieferscheine), nimmt der Client den automatisch.
- return client.db();
- }
|