2021-07-12 16:47:16 +00:00
|
|
|
const Settings = require('@overleaf/settings')
|
2020-09-10 11:58:06 +00:00
|
|
|
const { MongoClient, ObjectId } = require('mongodb')
|
|
|
|
|
|
|
|
const clientPromise = MongoClient.connect(
|
|
|
|
Settings.mongo.url,
|
|
|
|
Settings.mongo.options
|
|
|
|
)
|
|
|
|
|
|
|
|
let setupDbPromise
|
|
|
|
async function waitForDb() {
|
|
|
|
if (!setupDbPromise) {
|
|
|
|
setupDbPromise = setupDb()
|
|
|
|
}
|
|
|
|
await setupDbPromise
|
|
|
|
}
|
|
|
|
|
|
|
|
const db = {}
|
|
|
|
async function setupDb() {
|
|
|
|
const internalDb = (await clientPromise).db()
|
|
|
|
|
|
|
|
db.docHistory = internalDb.collection('docHistory')
|
|
|
|
db.docHistoryIndex = internalDb.collection('docHistoryIndex')
|
|
|
|
db.projectHistoryMetaData = internalDb.collection('projectHistoryMetaData')
|
|
|
|
}
|
|
|
|
|
2020-09-29 11:23:18 +00:00
|
|
|
async function closeDb() {
|
|
|
|
let client
|
|
|
|
try {
|
|
|
|
client = await clientPromise
|
|
|
|
} catch (e) {
|
|
|
|
// there is nothing to close
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return client.close()
|
|
|
|
}
|
|
|
|
|
2020-09-10 11:58:06 +00:00
|
|
|
module.exports = {
|
|
|
|
db,
|
|
|
|
ObjectId,
|
2020-09-29 11:23:18 +00:00
|
|
|
closeDb,
|
2021-07-13 11:04:43 +00:00
|
|
|
waitForDb,
|
2020-09-10 11:58:06 +00:00
|
|
|
}
|