2020-08-25 11:32:16 +00:00
|
|
|
const Settings = require('settings-sharelatex')
|
|
|
|
const { MongoClient, ObjectId } = require('mongodb')
|
|
|
|
|
2020-09-10 09:40:05 +00:00
|
|
|
const clientPromise = MongoClient.connect(
|
|
|
|
Settings.mongo.url,
|
|
|
|
Settings.mongo.options
|
|
|
|
)
|
2020-08-25 11:32:16 +00:00
|
|
|
|
|
|
|
async function healthCheck() {
|
|
|
|
const internalDb = (await clientPromise).db()
|
|
|
|
const res = await internalDb.command({ ping: 1 })
|
|
|
|
if (!res.ok) {
|
|
|
|
throw new Error('failed mongo ping')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-07 08:49:06 +00:00
|
|
|
let setupDbPromise
|
2020-08-25 11:32:16 +00:00
|
|
|
async function waitForDb() {
|
2020-09-07 08:49:06 +00:00
|
|
|
if (!setupDbPromise) {
|
|
|
|
setupDbPromise = setupDb()
|
|
|
|
}
|
|
|
|
await setupDbPromise
|
2020-08-25 11:32:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const db = {}
|
2020-09-07 08:49:06 +00:00
|
|
|
async function setupDb() {
|
2020-08-25 11:32:16 +00:00
|
|
|
const internalDb = (await clientPromise).db()
|
|
|
|
|
|
|
|
db.docSnapshots = internalDb.collection('docSnapshots')
|
2020-09-07 08:49:06 +00:00
|
|
|
}
|
2020-08-25 11:32:16 +00:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
db,
|
|
|
|
ObjectId,
|
|
|
|
healthCheck: require('util').callbackify(healthCheck),
|
|
|
|
waitForDb
|
|
|
|
}
|