Skip to content

Instantly share code, notes, and snippets.

@LearnWebCode
Created August 28, 2024 18:11

Revisions

  1. LearnWebCode created this gist Aug 28, 2024.
    36 changes: 36 additions & 0 deletions db.js
    Original 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