Skip to content
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

fix: Prevent MongoDB client promise being cached #66977

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions examples/with-mongodb/lib/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,24 @@ 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;
};

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;
13 changes: 2 additions & 11 deletions examples/with-mongodb/pages/index.tsx
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 = {
Expand All @@ -10,16 +10,7 @@ export const getServerSideProps: GetServerSideProps<
ConnectionStatus
> = async () => {
try {
await clientPromise;
// `await clientPromise` will use the default database passed in the MONGODB_URI
// However you can use another database (e.g. myDatabase) by replacing the `await clientPromise` with the following code:
//
// `const client = await clientPromise`
// `const db = client.db("myDatabase")`
//
// Then you can execute queries against your database like so:
// db.find({}) or any of the MongoDB Node Driver commands

await client.connect(); // `await client.connect()` will use the default database passed in the MONGODB_URI
return {
props: { isConnected: true },
};
Expand Down
5 changes: 0 additions & 5 deletions examples/with-mongodb/types/mongodb.d.ts

This file was deleted.

Loading