-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update to use the latest MongoDB best practices to limit connection p…
…ooling issues. (#28350) * Update to use the latest MongoDB best practices. * lint-fix Co-authored-by: Lee Robinson <[email protected]> Co-authored-by: [email protected] <[email protected]>
- Loading branch information
1 parent
877f982
commit 220fa9c
Showing
4 changed files
with
34 additions
and
47 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,2 +1 @@ | ||
MONGODB_URI= | ||
MONGODB_DB= | ||
MONGODB_URI= |
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
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,49 +1,32 @@ | ||
import { MongoClient } from 'mongodb' | ||
|
||
const MONGODB_URI = process.env.MONGODB_URI | ||
const MONGODB_DB = process.env.MONGODB_DB | ||
|
||
if (!MONGODB_URI) { | ||
throw new Error( | ||
'Please define the MONGODB_URI environment variable inside .env.local' | ||
) | ||
} | ||
|
||
if (!MONGODB_DB) { | ||
throw new Error( | ||
'Please define the MONGODB_DB environment variable inside .env.local' | ||
) | ||
const uri = process.env.MONGODB_URI | ||
const options = { | ||
useUnifiedTopology: true, | ||
useNewUrlParser: true, | ||
} | ||
|
||
/** | ||
* Global is used here to maintain a cached connection across hot reloads | ||
* in development. This prevents connections growing exponentially | ||
* during API Route usage. | ||
*/ | ||
let cached = global.mongo | ||
let client | ||
let clientPromise | ||
|
||
if (!cached) { | ||
cached = global.mongo = { conn: null, promise: null } | ||
if (!process.env.MONGODB_URI) { | ||
throw new Error('Please add your Mongo URI to .env.local') | ||
} | ||
|
||
export async function connectToDatabase() { | ||
if (cached.conn) { | ||
return cached.conn | ||
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() | ||
} | ||
|
||
if (!cached.promise) { | ||
const opts = { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true, | ||
} | ||
|
||
cached.promise = MongoClient.connect(MONGODB_URI, opts).then((client) => { | ||
return { | ||
client, | ||
db: client.db(MONGODB_DB), | ||
} | ||
}) | ||
} | ||
cached.conn = await cached.promise | ||
return cached.conn | ||
clientPromise = global._mongoClientPromise | ||
} 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 | ||
// separate module, the client can be shared across functions. | ||
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