init
This commit is contained in:
21
lib/db.ts
Normal file
21
lib/db.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { MongoClient, type Db } from "mongodb";
|
||||
|
||||
const uri = process.env.MONGODB_URI ?? "mongodb://localhost:27017";
|
||||
const dbName = process.env.MONGODB_DB ?? "knur";
|
||||
|
||||
declare global {
|
||||
var _mongoClientPromise: Promise<MongoClient> | undefined;
|
||||
}
|
||||
|
||||
function getClientPromise(): Promise<MongoClient> {
|
||||
if (!global._mongoClientPromise) {
|
||||
const client = new MongoClient(uri);
|
||||
global._mongoClientPromise = client.connect();
|
||||
}
|
||||
return global._mongoClientPromise;
|
||||
}
|
||||
|
||||
export async function getDb(): Promise<Db> {
|
||||
const client = await getClientPromise();
|
||||
return client.db(dbName);
|
||||
}
|
||||
Reference in New Issue
Block a user