Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix with-mongodb hot-reload issue and race condition
- Loading branch information
1 parent
4c38e3e
commit 45fbe64
Showing
1 changed file
with
29 additions
and
21 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,37 +1,45 @@ | ||
import { MongoClient } from 'mongodb' | ||
|
||
let uri = process.env.MONGODB_URI | ||
let dbName = process.env.MONGODB_DB | ||
const { MONGODB_URI, MONGODB_DB } = process.env | ||
|
||
let cachedClient = null | ||
let cachedDb = null | ||
|
||
if (!uri) { | ||
if (!MONGODB_URI) { | ||
throw new Error( | ||
'Please define the MONGODB_URI environment variable inside .env.local' | ||
) | ||
} | ||
|
||
if (!dbName) { | ||
if (!MONGODB_DB) { | ||
throw new Error( | ||
'Please define the MONGODB_DB environment variable inside .env.local' | ||
) | ||
} | ||
|
||
/** | ||
* Global is used here to maintain a cached connection across hot reloads | ||
* in development. This prevents connections growing exponentiatlly | ||
* during API Route usage. | ||
*/ | ||
let cached = global.mongo | ||
if (!cached) cached = global.mongo = {} | ||
|
||
export async function connectToDatabase() { | ||
if (cachedClient && cachedDb) { | ||
return { client: cachedClient, db: cachedDb } | ||
if (cached.conn) return cached.conn | ||
if (!cached.promise) { | ||
const conn = {} | ||
const opts = { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true, | ||
} | ||
cached.promise = MongoClient.connect(MONGODB_URI, opts) | ||
.then((client) => { | ||
conn.client = client | ||
return client.db(MONGODB_DB) | ||
}) | ||
.then((db) => { | ||
conn.db = db | ||
cached.conn = conn | ||
}) | ||
} | ||
|
||
const client = await MongoClient.connect(uri, { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true, | ||
}) | ||
|
||
const db = await client.db(dbName) | ||
|
||
cachedClient = client | ||
cachedDb = db | ||
|
||
return { client, db } | ||
await cached.promise | ||
return cached.conn | ||
} |