Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Juoelenis authored Nov 25, 2024
1 parent f8f4e01 commit 0a278d4
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions mongodb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var MongoClient = require("mongodb").MongoClient


const uri = process.env.MONGOURL
const options = {
useUnifiedTopology: true,
useNewUrlParser: true,
}

let client = null;
let clientPromise

if (!process.env.MONGOURL) {
throw new Error("Please add your Mongo URI to .env.local")
}

if (process.env.NODE_ENV === "development")
{
// In development mode, use a global variable so that the value
// is preserved across module reloads caused by HMR (Hot Module Replacement).
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()
}


// Export a module-scoped MongoClient promise. By doing this in a
// separate module, the client can be shared across functions.
export default clientPromise

0 comments on commit 0a278d4

Please sign in to comment.