Created
August 28, 2024 18:11
Revisions
-
LearnWebCode created this gist
Aug 28, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,36 @@ import { MongoClient } from "mongodb" let client let clientPromise if (!process.env.CONNECTIONSTRING) { throw new Error("Please add your MongoDB URI to the .env file") } const uri = process.env.CONNECTIONSTRING const options = {} if (process.env.NODE_ENV === "development") { // In development mode, use a global variable so that the MongoClient instance is not recreated. if (!global._mongoClientPromise) { client = new MongoClient(uri, options) global._mongoClientPromise = client.connect() } clientPromise = global._mongoClientPromise } else { // In production mode, it's best to not use a global variable. client = new MongoClient(uri, options) clientPromise = client.connect() } async function getDatabase() { const client = await clientPromise return client.db() } export async function getCollection(collectionName) { const db = await getDatabase() return db.collection(collectionName) } export default getDatabase