overleaf/services/chat/app/js/mongodb.js

30 lines
566 B
JavaScript
Raw Normal View History

const Settings = require('@overleaf/settings')
2020-08-19 12:00:01 +00:00
const { MongoClient, ObjectId } = require('mongodb')
const clientPromise = MongoClient.connect(
Settings.mongo.url,
Settings.mongo.options
)
2020-08-19 12:00:01 +00:00
let setupDbPromise
async function waitForDb() {
if (!setupDbPromise) {
setupDbPromise = setupDb()
}
await setupDbPromise
}
const db = {}
async function setupDb() {
const internalDb = (await clientPromise).db()
db.messages = internalDb.collection('messages')
db.rooms = internalDb.collection('rooms')
}
2020-08-19 12:00:01 +00:00
module.exports = {
db,
2020-08-19 12:00:01 +00:00
ObjectId,
waitForDb
2020-08-19 12:00:01 +00:00
}