Created
September 3, 2022 14:23
-
-
Save skolhustick/fed4a4974e2a1860bc6c575b85b04c26 to your computer and use it in GitHub Desktop.
astro-mongodob-mongodb.js
This file contains 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 characters
// src/lib/mongodb.js | |
import { MongoClient } from "mongodb"; | |
if (!import.meta.env.MONGODB_URI) { | |
throw new Error('Invalid environment variable: "MONGODB_URI"'); | |
} | |
const uri = import.meta.env.MONGODB_URI; | |
const options = {}; | |
let cachedMongo; | |
const connectToDB = async () => { | |
const mongo = await new MongoClient(uri, options).connect(); | |
// Change this to your own DB name of course. | |
// Or better yet, put it in your .env | |
return mongo.db("astro-mongodb"); | |
}; | |
export const getDB = async () => { | |
// In development mode, use a global variable so that the value | |
// is preserved across module reloads caused by HMR (Hot Module Replacement). | |
// Text above copied from : | |
// https://github.com/vercel/next.js/blob/canary/examples/with-mongodb/lib/mongodb.ts | |
if (import.meta.env.NODE_ENV === "development") { | |
if (!global._mongoConnection) { | |
global._mongoConnection = await connectToDB(); | |
cachedMongo = global._mongoConnection; | |
} | |
return cachedMongo; | |
} | |
const mongo = await connectToDB(); | |
return mongo; | |
}; | |
export const Users = async () => { | |
const db = await getDB(); | |
return db.collection("users"); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment