MongoManager.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. let MongoManager
  14. const { db, ObjectId } = require('./mongodb')
  15. const logger = require('logger-sharelatex')
  16. const metrics = require('@overleaf/metrics')
  17. const { promisify } = require('util')
  18. module.exports = MongoManager = {
  19. findDoc(project_id, doc_id, filter, callback) {
  20. if (callback == null) {
  21. callback = function (error, doc) {}
  22. }
  23. db.docs.findOne(
  24. {
  25. _id: ObjectId(doc_id.toString()),
  26. project_id: ObjectId(project_id.toString())
  27. },
  28. {
  29. projection: filter
  30. },
  31. callback
  32. )
  33. },
  34. getProjectsDocs(project_id, options, filter, callback) {
  35. const query = { project_id: ObjectId(project_id.toString()) }
  36. if (!options.include_deleted) {
  37. query.deleted = { $ne: true }
  38. }
  39. db.docs
  40. .find(query, {
  41. projection: filter
  42. })
  43. .toArray(callback)
  44. },
  45. getArchivedProjectDocs(project_id, callback) {
  46. const query = {
  47. project_id: ObjectId(project_id.toString()),
  48. inS3: true
  49. }
  50. db.docs.find(query).toArray(callback)
  51. },
  52. upsertIntoDocCollection(project_id, doc_id, updates, callback) {
  53. const update = {
  54. $set: updates,
  55. $inc: {
  56. rev: 1
  57. },
  58. $unset: {
  59. inS3: true
  60. }
  61. }
  62. update.$set.project_id = ObjectId(project_id)
  63. db.docs.updateOne(
  64. { _id: ObjectId(doc_id) },
  65. update,
  66. { upsert: true },
  67. callback
  68. )
  69. },
  70. markDocAsDeleted(project_id, doc_id, callback) {
  71. db.docs.updateOne(
  72. {
  73. _id: ObjectId(doc_id),
  74. project_id: ObjectId(project_id)
  75. },
  76. {
  77. $set: { deleted: true }
  78. },
  79. callback
  80. )
  81. },
  82. markDocAsArchived(doc_id, rev, callback) {
  83. const update = {
  84. $set: {},
  85. $unset: {}
  86. }
  87. update.$set.inS3 = true
  88. update.$unset.lines = true
  89. update.$unset.ranges = true
  90. const query = {
  91. _id: doc_id,
  92. rev
  93. }
  94. db.docs.updateOne(query, update, callback)
  95. },
  96. getDocVersion(doc_id, callback) {
  97. if (callback == null) {
  98. callback = function (error, version) {}
  99. }
  100. db.docOps.findOne(
  101. {
  102. doc_id: ObjectId(doc_id)
  103. },
  104. {
  105. projection: {
  106. version: 1
  107. }
  108. },
  109. function (error, doc) {
  110. if (error != null) {
  111. return callback(error)
  112. }
  113. callback(null, (doc && doc.version) || 0)
  114. }
  115. )
  116. },
  117. setDocVersion(doc_id, version, callback) {
  118. if (callback == null) {
  119. callback = function (error) {}
  120. }
  121. db.docOps.updateOne(
  122. {
  123. doc_id: ObjectId(doc_id)
  124. },
  125. {
  126. $set: { version }
  127. },
  128. {
  129. upsert: true
  130. },
  131. callback
  132. )
  133. },
  134. destroyDoc(doc_id, callback) {
  135. db.docs.deleteOne(
  136. {
  137. _id: ObjectId(doc_id)
  138. },
  139. function (err) {
  140. if (err != null) {
  141. return callback(err)
  142. }
  143. db.docOps.deleteOne(
  144. {
  145. doc_id: ObjectId(doc_id)
  146. },
  147. callback
  148. )
  149. }
  150. )
  151. }
  152. }
  153. const methods = Object.getOwnPropertyNames(MongoManager)
  154. module.exports.promises = {}
  155. for (const method of methods) {
  156. metrics.timeAsyncMethod(MongoManager, method, 'mongo.MongoManager', logger)
  157. module.exports.promises[method] = promisify(module.exports[method])
  158. }