2021-07-12 12:47:16 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2020-09-10 07:58:06 -04: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 07:23:18 -04:00
|
|
|
async function closeDb() {
|
|
|
|
let client
|
|
|
|
try {
|
|
|
|
client = await clientPromise
|
|
|
|
} catch (e) {
|
|
|
|
// there is nothing to close
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return client.close()
|
|
|
|
}
|
|
|
|
|
2020-09-10 07:58:06 -04:00
|
|
|
module.exports = {
|
|
|
|
db,
|
|
|
|
ObjectId,
|
2020-09-29 07:23:18 -04:00
|
|
|
closeDb,
|
2021-07-13 07:04:43 -04:00
|
|
|
waitForDb,
|
2020-09-10 07:58:06 -04:00
|
|
|
}
|