mongodb.mjs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import mongodb from 'mongodb-legacy'
  2. import OError from '@overleaf/o-error'
  3. import Settings from '@overleaf/settings'
  4. import MongoUtils from '@overleaf/mongo-utils'
  5. import Mongoose from './Mongoose.mjs'
  6. import { addConnectionDrainer } from './GracefulShutdown.mjs'
  7. import Metrics from '@overleaf/metrics'
  8. // Ensure Mongoose is using the same mongodb instance as the mongodb module,
  9. // otherwise we will get multiple versions of the ObjectId class. Mongoose
  10. // patches ObjectId, so loading multiple versions of the mongodb module can
  11. // cause problems with ObjectId comparisons.
  12. if (Mongoose.mongo.ObjectId !== mongodb.ObjectId) {
  13. throw new OError(
  14. 'FATAL ERROR: Mongoose is using a different mongodb instance'
  15. )
  16. }
  17. export const { ObjectId } = mongodb
  18. const { ReadPreference } = mongodb
  19. export const READ_PREFERENCE_PRIMARY = ReadPreference.primary.mode
  20. export const READ_PREFERENCE_SECONDARY = Settings.mongo.hasSecondaries
  21. ? ReadPreference.secondary.mode
  22. : ReadPreference.secondaryPreferred.mode
  23. const mongoClient = new mongodb.MongoClient(
  24. Settings.mongo.url,
  25. Settings.mongo.options
  26. )
  27. Metrics.mongodb.monitor(mongoClient, 'native')
  28. addConnectionDrainer('mongodb', async () => {
  29. await mongoClient.close()
  30. })
  31. const internalDb = mongoClient.db()
  32. export const db = {
  33. contacts: internalDb.collection('contacts'),
  34. deletedProjects: internalDb.collection('deletedProjects'),
  35. deletedSubscriptions: internalDb.collection('deletedSubscriptions'),
  36. deletedUsers: internalDb.collection('deletedUsers'),
  37. domainVerifications: internalDb.collection('domainVerifications'),
  38. dropboxEntities: internalDb.collection('dropboxEntities'),
  39. dropboxProjects: internalDb.collection('dropboxProjects'),
  40. docSnapshots: internalDb.collection('docSnapshots'),
  41. docs: internalDb.collection('docs'),
  42. feedbacks: internalDb.collection('feedbacks'),
  43. githubSyncEntityVersions: internalDb.collection('githubSyncEntityVersions'),
  44. githubSyncProjectStates: internalDb.collection('githubSyncProjectStates'),
  45. githubSyncUserCredentials: internalDb.collection('githubSyncUserCredentials'),
  46. globalMetrics: internalDb.collection('globalMetrics'),
  47. grouppolicies: internalDb.collection('grouppolicies'),
  48. groupAuditLogEntries: internalDb.collection('groupAuditLogEntries'),
  49. institutions: internalDb.collection('institutions'),
  50. messages: internalDb.collection('messages'),
  51. migrations: internalDb.collection('migrations'),
  52. notifications: internalDb.collection('notifications'),
  53. emailNotifications: internalDb.collection('emailNotifications'),
  54. notificationsPreferences: internalDb.collection('notificationsPreferences'),
  55. oauthAccessTokens: internalDb.collection('oauthAccessTokens'),
  56. oauthApplications: internalDb.collection('oauthApplications'),
  57. oauthAuthorizationCodes: internalDb.collection('oauthAuthorizationCodes'),
  58. projectAuditLogEntries: internalDb.collection('projectAuditLogEntries'),
  59. projectHistoryChunks: internalDb.collection('projectHistoryChunks'),
  60. projectHistoryFailures: internalDb.collection('projectHistoryFailures'),
  61. projectHistoryGlobalBlobs: internalDb.collection('projectHistoryGlobalBlobs'),
  62. projectHistoryLabels: internalDb.collection('projectHistoryLabels'),
  63. projectHistorySizes: internalDb.collection('projectHistorySizes'),
  64. projectHistorySyncState: internalDb.collection('projectHistorySyncState'),
  65. projectInvites: internalDb.collection('projectInvites'),
  66. projects: internalDb.collection('projects'),
  67. publishers: internalDb.collection('publishers'),
  68. rooms: internalDb.collection('rooms'),
  69. samlCache: internalDb.collection('samlCache'),
  70. samlLogs: internalDb.collection('samlLogs'),
  71. spellingPreferences: internalDb.collection('spellingPreferences'),
  72. splittests: internalDb.collection('splittests'),
  73. ssoConfigs: internalDb.collection('ssoConfigs'),
  74. subscriptions: internalDb.collection('subscriptions'),
  75. surveys: internalDb.collection('surveys'),
  76. systemmessages: internalDb.collection('systemmessages'),
  77. tags: internalDb.collection('tags'),
  78. teamInvites: internalDb.collection('teamInvites'),
  79. tokens: internalDb.collection('tokens'),
  80. userAuditLogEntries: internalDb.collection('userAuditLogEntries'),
  81. users: internalDb.collection('users'),
  82. onboardingDataCollection: internalDb.collection('onboardingDataCollection'),
  83. scriptLogs: internalDb.collection('scriptLogs'),
  84. }
  85. export const connectionPromise = mongoClient.connect()
  86. export async function getCollectionNames() {
  87. const internalDb = mongoClient.db()
  88. const collections = await internalDb.collections()
  89. return collections.map(collection => collection.collectionName)
  90. }
  91. export async function cleanupTestDatabase() {
  92. await MongoUtils.cleanupTestDatabase(mongoClient)
  93. }
  94. export async function dropTestDatabase() {
  95. await MongoUtils.dropTestDatabase(mongoClient)
  96. }
  97. /**
  98. * WARNING: Consider using a pre-populated collection from `db` to avoid typos!
  99. */
  100. export async function getCollectionInternal(name) {
  101. const internalDb = mongoClient.db()
  102. return internalDb.collection(name)
  103. }
  104. export async function waitForDb() {
  105. await connectionPromise
  106. }
  107. export default {
  108. db,
  109. ObjectId,
  110. connectionPromise,
  111. waitForDb,
  112. getCollectionNames,
  113. getCollectionInternal,
  114. cleanupTestDatabase,
  115. dropTestDatabase,
  116. READ_PREFERENCE_PRIMARY,
  117. READ_PREFERENCE_SECONDARY,
  118. }