[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:31:50 +01:00
parent 0cf5cad3b8
commit f6083a2d51

View file

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