mongodb.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. const { ObjectId, ReadPreference } = require('mongodb')
  2. const OError = require('@overleaf/o-error')
  3. const Settings = require('@overleaf/settings')
  4. const { getNativeDb } = require('./Mongoose')
  5. if (
  6. typeof global.beforeEach === 'function' &&
  7. process.argv.join(' ').match(/unit/)
  8. ) {
  9. throw new Error(
  10. 'It looks like unit tests are running, but you are connecting to Mongo. Missing a stub?'
  11. )
  12. }
  13. const READ_PREFERENCE_PRIMARY = ReadPreference.primary.mode
  14. const READ_PREFERENCE_SECONDARY = Settings.mongo.hasSecondaries
  15. ? ReadPreference.secondary.mode
  16. : ReadPreference.secondaryPreferred.mode
  17. let setupDbPromise
  18. async function waitForDb() {
  19. if (!setupDbPromise) {
  20. setupDbPromise = setupDb()
  21. }
  22. await setupDbPromise
  23. }
  24. const db = {}
  25. async function setupDb() {
  26. const internalDb = await getNativeDb()
  27. db.contacts = internalDb.collection('contacts')
  28. db.deletedFiles = internalDb.collection('deletedFiles')
  29. db.deletedProjects = internalDb.collection('deletedProjects')
  30. db.deletedSubscriptions = internalDb.collection('deletedSubscriptions')
  31. db.deletedUsers = internalDb.collection('deletedUsers')
  32. db.dropboxEntities = internalDb.collection('dropboxEntities')
  33. db.dropboxProjects = internalDb.collection('dropboxProjects')
  34. db.docHistory = internalDb.collection('docHistory')
  35. db.docHistoryIndex = internalDb.collection('docHistoryIndex')
  36. db.docOps = internalDb.collection('docOps')
  37. db.docSnapshots = internalDb.collection('docSnapshots')
  38. db.docs = internalDb.collection('docs')
  39. db.feedbacks = internalDb.collection('feedbacks')
  40. db.githubSyncEntityVersions = internalDb.collection(
  41. 'githubSyncEntityVersions'
  42. )
  43. db.githubSyncProjectStates = internalDb.collection('githubSyncProjectStates')
  44. db.githubSyncUserCredentials = internalDb.collection(
  45. 'githubSyncUserCredentials'
  46. )
  47. db.institutions = internalDb.collection('institutions')
  48. db.messages = internalDb.collection('messages')
  49. db.migrations = internalDb.collection('migrations')
  50. db.notifications = internalDb.collection('notifications')
  51. db.oauthAccessTokens = internalDb.collection('oauthAccessTokens')
  52. db.oauthApplications = internalDb.collection('oauthApplications')
  53. db.oauthAuthorizationCodes = internalDb.collection('oauthAuthorizationCodes')
  54. db.projectAuditLogEntries = internalDb.collection('projectAuditLogEntries')
  55. db.projectHistoryChunks = internalDb.collection('projectHistoryChunks')
  56. db.projectHistoryFailures = internalDb.collection('projectHistoryFailures')
  57. db.projectHistoryLabels = internalDb.collection('projectHistoryLabels')
  58. db.projectHistoryMetaData = internalDb.collection('projectHistoryMetaData')
  59. db.projectHistorySyncState = internalDb.collection('projectHistorySyncState')
  60. db.projectInvites = internalDb.collection('projectInvites')
  61. db.projects = internalDb.collection('projects')
  62. db.publishers = internalDb.collection('publishers')
  63. db.rooms = internalDb.collection('rooms')
  64. db.samlCache = internalDb.collection('samlCache')
  65. db.samlLogs = internalDb.collection('samlLogs')
  66. db.spellingPreferences = internalDb.collection('spellingPreferences')
  67. db.splittests = internalDb.collection('splittests')
  68. db.ssoConfigs = internalDb.collection('ssoConfigs')
  69. db.subscriptions = internalDb.collection('subscriptions')
  70. db.surveys = internalDb.collection('surveys')
  71. db.systemmessages = internalDb.collection('systemmessages')
  72. db.tags = internalDb.collection('tags')
  73. db.teamInvites = internalDb.collection('teamInvites')
  74. db.templates = internalDb.collection('templates')
  75. db.tokens = internalDb.collection('tokens')
  76. db.userAuditLogEntries = internalDb.collection('userAuditLogEntries')
  77. db.users = internalDb.collection('users')
  78. db.userstubs = internalDb.collection('userstubs')
  79. db.onboardingDataCollection = internalDb.collection(
  80. 'onboardingDataCollection'
  81. )
  82. }
  83. async function getCollectionNames() {
  84. const internalDb = await getNativeDb()
  85. const collections = await internalDb.collections()
  86. return collections.map(collection => collection.collectionName)
  87. }
  88. async function dropTestDatabase() {
  89. const internalDb = await getNativeDb()
  90. const dbName = internalDb.databaseName
  91. const env = process.env.NODE_ENV
  92. if (dbName !== 'test-sharelatex' || env !== 'test') {
  93. throw new OError(
  94. `Refusing to clear database '${dbName}' in environment '${env}'`
  95. )
  96. }
  97. await internalDb.dropDatabase()
  98. }
  99. /**
  100. * WARNING: Consider using a pre-populated collection from `db` to avoid typos!
  101. */
  102. async function getCollectionInternal(name) {
  103. const internalDb = await getNativeDb()
  104. return internalDb.collection(name)
  105. }
  106. module.exports = {
  107. db,
  108. ObjectId,
  109. getCollectionNames,
  110. getCollectionInternal,
  111. dropTestDatabase,
  112. waitForDb,
  113. READ_PREFERENCE_PRIMARY,
  114. READ_PREFERENCE_SECONDARY,
  115. }