mongodb.js 886 B

12345678910111213141516171819202122232425262728293031323334353637
  1. // @ts-check
  2. import Metrics from '@overleaf/metrics'
  3. import Settings from '@overleaf/settings'
  4. import MongoUtils from '@overleaf/mongo-utils'
  5. import mongodb from 'mongodb-legacy'
  6. const { MongoClient, ObjectId, BSON, ReadPreference } = mongodb
  7. const mongoClient = new MongoClient(Settings.mongo.url, Settings.mongo.options)
  8. const mongoDb = mongoClient.db()
  9. const db = {
  10. docs: mongoDb.collection('docs'),
  11. }
  12. Metrics.mongodb.monitor(mongoClient)
  13. async function cleanupTestDatabase() {
  14. await MongoUtils.cleanupTestDatabase(mongoClient)
  15. }
  16. const READ_PREFERENCE_PRIMARY = ReadPreference.primary.mode
  17. const READ_PREFERENCE_SECONDARY = Settings.mongo.hasSecondaries
  18. ? ReadPreference.secondary.mode
  19. : ReadPreference.secondaryPreferred.mode
  20. export default {
  21. READ_PREFERENCE_PRIMARY,
  22. READ_PREFERENCE_SECONDARY,
  23. db,
  24. mongoClient,
  25. ObjectId,
  26. BSON,
  27. cleanupTestDatabase,
  28. }