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

30 lines
567 B
JavaScript
Raw Normal View History

const Settings = require('@overleaf/settings')
2020-08-19 08:00:01 -04:00
const { MongoClient, ObjectId } = require('mongodb')
const clientPromise = MongoClient.connect(
Settings.mongo.url,
Settings.mongo.options
)
2020-08-19 08:00:01 -04: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 08:00:01 -04:00
module.exports = {
db,
2020-08-19 08:00:01 -04:00
ObjectId,
2021-07-13 07:04:48 -04:00
waitForDb,
2020-08-19 08:00:01 -04:00
}