-
Notifications
You must be signed in to change notification settings - Fork 27.4k
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
2 changed files
with
13 additions
and
13 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 |
---|---|---|
@@ -1,32 +1,32 @@ | ||
import { MongoClient } from 'mongodb'; | ||
import { MongoClient } from 'mongodb' | ||
|
||
const uri = process.env.MONGODB_URI; | ||
const uri = process.env.MONGODB_URI | ||
const options = { | ||
useUnifiedTopology: true, | ||
useNewUrlParser: true, | ||
}; | ||
} | ||
|
||
let client; | ||
let clientPromise; | ||
let client | ||
let clientPromise | ||
|
||
if (!process.env.MONGODB_URI) { | ||
throw new Error('Please add your Mongo URI to .env.local'); | ||
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(); | ||
client = new MongoClient(uri, options) | ||
global._mongoClientPromise = client.connect() | ||
} | ||
clientPromise = global._mongoClientPromise; | ||
clientPromise = global._mongoClientPromise | ||
} else { | ||
// In production mode, it's best to not use a global variable. | ||
client = new MongoClient(uri, options); | ||
clientPromise = client.connect(); | ||
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; | ||
export default clientPromise |
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