2021-07-12 12:47:20 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2020-08-21 12:50:01 -04:00
|
|
|
const { MongoClient, ObjectId } = require('mongodb')
|
|
|
|
|
2020-09-10 05:36:15 -04:00
|
|
|
const clientPromise = MongoClient.connect(
|
|
|
|
Settings.mongo.url,
|
|
|
|
Settings.mongo.options
|
|
|
|
)
|
2020-08-21 12:50:01 -04:00
|
|
|
|
2020-09-07 04:35:38 -04:00
|
|
|
let setupDbPromise
|
2020-08-21 12:50:01 -04:00
|
|
|
async function waitForDb() {
|
2020-09-07 04:35:38 -04:00
|
|
|
if (!setupDbPromise) {
|
|
|
|
setupDbPromise = setupDb()
|
|
|
|
}
|
|
|
|
await setupDbPromise
|
2020-08-21 12:50:01 -04:00
|
|
|
}
|
|
|
|
|
2020-08-28 08:13:19 -04:00
|
|
|
const db = {}
|
2020-09-07 04:35:38 -04:00
|
|
|
async function setupDb() {
|
2020-08-28 08:13:19 -04:00
|
|
|
const internalDb = (await clientPromise).db()
|
|
|
|
|
|
|
|
db.docs = internalDb.collection('docs')
|
|
|
|
db.docOps = internalDb.collection('docOps')
|
2020-09-07 04:35:38 -04:00
|
|
|
}
|
2020-09-07 04:44:19 -04:00
|
|
|
async function addCollection(name) {
|
|
|
|
await waitForDb()
|
|
|
|
const internalDb = (await clientPromise).db()
|
|
|
|
|
|
|
|
db[name] = internalDb.collection(name)
|
|
|
|
}
|
2020-08-28 08:13:19 -04:00
|
|
|
|
2020-08-21 12:50:01 -04:00
|
|
|
module.exports = {
|
2020-08-28 08:13:19 -04:00
|
|
|
db,
|
2020-08-21 12:50:01 -04:00
|
|
|
ObjectId,
|
2020-09-07 04:44:19 -04:00
|
|
|
addCollection,
|
2021-07-13 07:04:48 -04:00
|
|
|
waitForDb,
|
2020-08-21 12:50:01 -04:00
|
|
|
}
|