-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
163 additions
and
6 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ yarn-error.log* | |
|
||
# local env files | ||
.env*.local | ||
.env | ||
|
||
# vercel | ||
.vercel | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { MongoClient } from "mongodb"; | ||
|
||
const uri = process.env.MONGODB_URI; // Your MongoDB connection string | ||
declare global { | ||
var _mongoClientPromise: Promise<MongoClient>; | ||
} | ||
|
||
let client; | ||
let clientPromise; | ||
|
||
if (!process.env.MONGODB_URI) { | ||
throw new Error("Please add your Mongo URI to .env.local"); | ||
} | ||
|
||
if (process.env.NODE_ENV === "development") { | ||
// In development mode, use a global variable to preserve the database connection | ||
if (!global._mongoClientPromise) { | ||
client = new MongoClient(uri || ""); | ||
global._mongoClientPromise = client.connect(); | ||
} | ||
clientPromise = global._mongoClientPromise; | ||
} else { | ||
// In production mode, use a new database connection | ||
client = new MongoClient(uri || ""); | ||
clientPromise = client.connect(); | ||
} | ||
|
||
export default clientPromise; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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