mongodb.js 784 B

12345678910111213141516171819202122232425262728293031323334353637
  1. const Settings = require('settings-sharelatex')
  2. const { MongoClient, ObjectId } = require('mongodb')
  3. const clientPromise = MongoClient.connect(
  4. Settings.mongo.url,
  5. Settings.mongo.options
  6. )
  7. async function healthCheck() {
  8. const internalDb = (await clientPromise).db()
  9. const res = await internalDb.command({ ping: 1 })
  10. if (!res.ok) {
  11. throw new Error('failed mongo ping')
  12. }
  13. }
  14. let setupDbPromise
  15. async function waitForDb() {
  16. if (!setupDbPromise) {
  17. setupDbPromise = setupDb()
  18. }
  19. await setupDbPromise
  20. }
  21. const db = {}
  22. async function setupDb() {
  23. const internalDb = (await clientPromise).db()
  24. db.docSnapshots = internalDb.collection('docSnapshots')
  25. }
  26. module.exports = {
  27. db,
  28. ObjectId,
  29. healthCheck: require('util').callbackify(healthCheck),
  30. waitForDb
  31. }