Repository navigation
Proposal: optional database example for create-next-app (MongoDB) #88377
Unanswered
shreyashpatel5506
asked this question in
Show and tell
Replies: 2 comments 1 reply
|
Good proposal! For the MongoDB example in create-next-app, here are some patterns that work well:
// lib/mongodb.js
import { MongoClient } from "mongodb";
let client;
let clientPromise;
if (process.env.NODE_ENV === "development") {
if (!global._mongoClientPromise) {
client = new MongoClient(process.env.MONGODB_URI);
global._mongoClientPromise = client.connect();
}
clientPromise = global._mongoClientPromise;
} else {
client = new MongoClient(process.env.MONGODB_URI);
clientPromise = client.connect();
}
export default clientPromise;
The existing with-mongodb example covers most of this, but having it as a create-next-app option would definitely reduce friction for new developers. |
0 replies
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Goals
Non-Goals
Background
Many developers starting with create-next-app look for guidance on setting up a database, and MongoDB is one of the most commonly used choices in the Next.js ecosystem.
While there are community blog posts and third-party examples, users often struggle with:
Next.js already uses an examples-first approach for integrations, which makes an example a natural and low-risk way to address this gap without increasing framework complexity.
Proposal
Add or improve an optional MongoDB example under the examples/ directory (for example, examples/with-mongodb).
The example would:
I am happy to contribute the implementation and follow existing conventions for examples in the repository. Before opening a PR, I wanted to confirm that this approach aligns with the project’s direction and get guidance on scope or structure.
All reactions