Created
June 16, 2019 10:08
-
-
Save mikkeldamm/9df9e29b78f83fd2df7792dc33964c21 to your computer and use it in GitHub Desktop.
zeit now 2.0 mongodb connection
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
let cachedMongoClient = null; | |
let cachedMongoDb = null; | |
exports.connect = async () => { | |
if (cachedMongoDb && cachedMongoClient && cachedMongoClient.isConnected()) { | |
return cachedMongoDb; | |
} | |
cachedMongoClient = await MongoClient.connect('MONGO_CONNECTION', { useNewUrlParser: true }); | |
cachedMongoDb = cachedMongoClient.db(); | |
return cachedMongoDb; | |
} |
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
const { connect } = requie('./connection'); | |
const handler = async (req, res) => { | |
const db = await connect(); | |
res.send(await db.findOne({ user: 'Mikkel' })); | |
}; | |
module.exports = handler; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment