-
Notifications
You must be signed in to change notification settings - Fork 33
Cache mongo client, not client promise #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,27 +7,24 @@ if (!process.env.MONGODB_URI) { | |
const uri = process.env.MONGODB_URI; | ||
const options = {}; | ||
|
||
let client; | ||
let clientPromise: Promise<MongoClient>; | ||
let client: MongoClient; | ||
|
||
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). | ||
let globalWithMongo = global as typeof globalThis & { | ||
_mongoClientPromise?: Promise<MongoClient>; | ||
_mongoClient?: MongoClient; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Optional, can we use |
||
}; | ||
|
||
if (!globalWithMongo._mongoClientPromise) { | ||
client = new MongoClient(uri, options); | ||
globalWithMongo._mongoClientPromise = client.connect(); | ||
if (!globalWithMongo._mongoClient) { | ||
globalWithMongo._mongoClient = new MongoClient(uri, options); | ||
} | ||
clientPromise = globalWithMongo._mongoClientPromise; | ||
client = globalWithMongo._mongoClient; | ||
} 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 | ||
// Export a module-scoped MongoClient. By doing this in a | ||
// separate module, the client can be shared across functions. | ||
export default clientPromise; | ||
export default client; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import Head from "next/head"; | ||
import clientPromise from "../lib/mongodb"; | ||
import client from "../lib/mongodb"; | ||
import type { InferGetServerSidePropsType, GetServerSideProps } from "next"; | ||
|
||
type ConnectionStatus = { | ||
|
@@ -10,8 +10,8 @@ export const getServerSideProps: GetServerSideProps< | |
ConnectionStatus | ||
> = async () => { | ||
try { | ||
await clientPromise; | ||
// `await clientPromise` will use the default database passed in the MONGODB_URI | ||
await client.connect(); | ||
// `await client.connect()` will use the default database passed in the MONGODB_URI | ||
Comment on lines
+13
to
+14
|
||
// However you can use another database (e.g. myDatabase) by replacing the `await clientPromise` with the following code: | ||
// | ||
// `const client = await clientPromise` | ||
|