mongodb.js 714 B

12345678910111213141516171819202122232425262728
  1. const Metrics = require('@overleaf/metrics')
  2. const Settings = require('@overleaf/settings')
  3. const { MongoClient, ObjectId } = require('mongodb-legacy')
  4. const mongoClient = new MongoClient(Settings.mongo.url, Settings.mongo.options)
  5. const mongoDb = mongoClient.db()
  6. const db = {
  7. docs: mongoDb.collection('docs'),
  8. docSnapshots: mongoDb.collection('docSnapshots'),
  9. projects: mongoDb.collection('projects'),
  10. }
  11. async function healthCheck() {
  12. const res = await mongoDb.command({ ping: 1 })
  13. if (!res.ok) {
  14. throw new Error('failed mongo ping')
  15. }
  16. }
  17. Metrics.mongodb.monitor(mongoClient)
  18. module.exports = {
  19. db,
  20. ObjectId,
  21. mongoClient,
  22. healthCheck: require('node:util').callbackify(healthCheck),
  23. }