[misc] mongodb: refactor the process of setting up the db construct

Co-Authored-By: John Lees-Miller <jdleesmiller@gmail.com>
This commit is contained in:
Jakob Ackermann 2020-09-07 09:51:16 +01:00
parent 74a3a95dc6
commit 9d28208870

View file

@ -3,16 +3,20 @@ const { MongoClient, ObjectId } = require('mongodb')
const clientPromise = MongoClient.connect(Settings.mongo.url) const clientPromise = MongoClient.connect(Settings.mongo.url)
let setupDbPromise
async function waitForDb() { async function waitForDb() {
await clientPromise if (!setupDbPromise) {
setupDbPromise = setupDb()
}
await setupDbPromise
} }
const db = {} const db = {}
waitForDb().then(async function () { async function setupDb() {
const internalDb = (await clientPromise).db() const internalDb = (await clientPromise).db()
db.notifications = internalDb.collection('notifications') db.notifications = internalDb.collection('notifications')
}) }
module.exports = { module.exports = {
db, db,