mongodb.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. const mongodb = require('mongodb-legacy')
  2. const OError = require('@overleaf/o-error')
  3. const Settings = require('@overleaf/settings')
  4. const Mongoose = require('./Mongoose')
  5. const { addConnectionDrainer } = require('./GracefulShutdown')
  6. // Ensure Mongoose is using the same mongodb instance as the mongodb module,
  7. // otherwise we will get multiple versions of the ObjectId class. Mongoose
  8. // patches ObjectId, so loading multiple versions of the mongodb module can
  9. // cause problems with ObjectId comparisons.
  10. if (Mongoose.mongo.ObjectId !== mongodb.ObjectId) {
  11. throw new OError(
  12. 'FATAL ERROR: Mongoose is using a different mongodb instance'
  13. )
  14. }
  15. const { ObjectId, ReadPreference } = mongodb
  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. const mongoClient = new mongodb.MongoClient(
  21. Settings.mongo.url,
  22. Settings.mongo.options
  23. )
  24. addConnectionDrainer('mongodb', async () => {
  25. await mongoClient.close()
  26. })
  27. const internalDb = mongoClient.db()
  28. const db = {
  29. contacts: internalDb.collection('contacts'),
  30. deletedProjects: internalDb.collection('deletedProjects'),
  31. deletedSubscriptions: internalDb.collection('deletedSubscriptions'),
  32. deletedUsers: internalDb.collection('deletedUsers'),
  33. dropboxEntities: internalDb.collection('dropboxEntities'),
  34. dropboxProjects: internalDb.collection('dropboxProjects'),
  35. docHistory: internalDb.collection('docHistory'),
  36. docHistoryIndex: internalDb.collection('docHistoryIndex'),
  37. docSnapshots: internalDb.collection('docSnapshots'),
  38. docs: internalDb.collection('docs'),
  39. feedbacks: internalDb.collection('feedbacks'),
  40. githubSyncEntityVersions: internalDb.collection('githubSyncEntityVersions'),
  41. githubSyncProjectStates: internalDb.collection('githubSyncProjectStates'),
  42. githubSyncUserCredentials: internalDb.collection('githubSyncUserCredentials'),
  43. globalMetrics: internalDb.collection('globalMetrics'),
  44. grouppolicies: internalDb.collection('grouppolicies'),
  45. groupAuditLogEntries: internalDb.collection('groupAuditLogEntries'),
  46. institutions: internalDb.collection('institutions'),
  47. messages: internalDb.collection('messages'),
  48. migrations: internalDb.collection('migrations'),
  49. notifications: internalDb.collection('notifications'),
  50. oauthAccessTokens: internalDb.collection('oauthAccessTokens'),
  51. oauthApplications: internalDb.collection('oauthApplications'),
  52. oauthAuthorizationCodes: internalDb.collection('oauthAuthorizationCodes'),
  53. projectAuditLogEntries: internalDb.collection('projectAuditLogEntries'),
  54. projectHistoryChunks: internalDb.collection('projectHistoryChunks'),
  55. projectHistoryFailures: internalDb.collection('projectHistoryFailures'),
  56. projectHistoryGlobalBlobs: internalDb.collection('projectHistoryGlobalBlobs'),
  57. projectHistoryLabels: internalDb.collection('projectHistoryLabels'),
  58. projectHistoryMetaData: internalDb.collection('projectHistoryMetaData'),
  59. projectHistorySyncState: internalDb.collection('projectHistorySyncState'),
  60. projectInvites: internalDb.collection('projectInvites'),
  61. projects: internalDb.collection('projects'),
  62. publishers: internalDb.collection('publishers'),
  63. rooms: internalDb.collection('rooms'),
  64. samlCache: internalDb.collection('samlCache'),
  65. samlLogs: internalDb.collection('samlLogs'),
  66. spellingPreferences: internalDb.collection('spellingPreferences'),
  67. splittests: internalDb.collection('splittests'),
  68. ssoConfigs: internalDb.collection('ssoConfigs'),
  69. subscriptions: internalDb.collection('subscriptions'),
  70. surveys: internalDb.collection('surveys'),
  71. systemmessages: internalDb.collection('systemmessages'),
  72. tags: internalDb.collection('tags'),
  73. teamInvites: internalDb.collection('teamInvites'),
  74. tokens: internalDb.collection('tokens'),
  75. userAuditLogEntries: internalDb.collection('userAuditLogEntries'),
  76. users: internalDb.collection('users'),
  77. onboardingDataCollection: internalDb.collection('onboardingDataCollection'),
  78. scriptLogs: internalDb.collection('scriptLogs'),
  79. }
  80. const connectionPromise = mongoClient.connect()
  81. async function getCollectionNames() {
  82. const internalDb = mongoClient.db()
  83. const collections = await internalDb.collections()
  84. return collections.map(collection => collection.collectionName)
  85. }
  86. async function cleanupTestDatabase() {
  87. ensureTestDatabase()
  88. const collectionNames = await getCollectionNames()
  89. const collections = []
  90. for (const name of collectionNames) {
  91. if (name in db && name !== 'migrations') {
  92. collections.push(db[name])
  93. }
  94. }
  95. await Promise.all(collections.map(coll => coll.deleteMany({})))
  96. }
  97. async function dropTestDatabase() {
  98. ensureTestDatabase()
  99. await mongoClient.db().dropDatabase()
  100. }
  101. function ensureTestDatabase() {
  102. const internalDb = mongoClient.db()
  103. const dbName = internalDb.databaseName
  104. const env = process.env.NODE_ENV
  105. if (dbName !== 'test-overleaf' || env !== 'test') {
  106. throw new OError(
  107. `Refusing to clear database '${dbName}' in environment '${env}'`
  108. )
  109. }
  110. }
  111. /**
  112. * WARNING: Consider using a pre-populated collection from `db` to avoid typos!
  113. */
  114. async function getCollectionInternal(name) {
  115. const internalDb = mongoClient.db()
  116. return internalDb.collection(name)
  117. }
  118. module.exports = {
  119. db,
  120. ObjectId,
  121. connectionPromise,
  122. getCollectionNames,
  123. getCollectionInternal,
  124. cleanupTestDatabase,
  125. dropTestDatabase,
  126. READ_PREFERENCE_PRIMARY,
  127. READ_PREFERENCE_SECONDARY,
  128. }