mongodb.js 897 B

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