overleaf/services/document-updater/app/js/mongodb.js
Jakob Ackermann f80a92ce46 [misc] migrate the app to the native mongo driver
acceptance tests to follow in a separate commit
2020-08-28 15:12:30 +01:00

30 lines
685 B
JavaScript

const Settings = require('settings-sharelatex')
const { MongoClient, ObjectId } = require('mongodb')
const clientPromise = MongoClient.connect(Settings.mongo.url)
async function healthCheck() {
const internalDb = (await clientPromise).db()
const res = await internalDb.command({ ping: 1 })
if (!res.ok) {
throw new Error('failed mongo ping')
}
}
async function waitForDb() {
await clientPromise
}
const db = {}
waitForDb().then(async function () {
const internalDb = (await clientPromise).db()
db.docSnapshots = internalDb.collection('docSnapshots')
})
module.exports = {
db,
ObjectId,
healthCheck: require('util').callbackify(healthCheck),
waitForDb
}