MongoManager.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. getNonDeletedArchivedProjectDocs(project_id, callback) {
  53. const query = {
  54. project_id: ObjectId(project_id.toString()),
  55. deleted: { $ne: true },
  56. inS3: true
  57. }
  58. db.docs.find(query).toArray(callback)
  59. },
  60. upsertIntoDocCollection(project_id, doc_id, updates, callback) {
  61. const update = {
  62. $set: updates,
  63. $inc: {
  64. rev: 1
  65. },
  66. $unset: {
  67. inS3: true
  68. }
  69. }
  70. update.$set.project_id = ObjectId(project_id)
  71. db.docs.updateOne(
  72. { _id: ObjectId(doc_id) },
  73. update,
  74. { upsert: true },
  75. callback
  76. )
  77. },
  78. markDocAsDeleted(project_id, doc_id, callback) {
  79. db.docs.updateOne(
  80. {
  81. _id: ObjectId(doc_id),
  82. project_id: ObjectId(project_id)
  83. },
  84. {
  85. $set: { deleted: true }
  86. },
  87. callback
  88. )
  89. },
  90. markDocAsArchived(doc_id, rev, callback) {
  91. const update = {
  92. $set: {},
  93. $unset: {}
  94. }
  95. update.$set.inS3 = true
  96. update.$unset.lines = true
  97. update.$unset.ranges = true
  98. const query = {
  99. _id: doc_id,
  100. rev
  101. }
  102. db.docs.updateOne(query, update, callback)
  103. },
  104. getDocVersion(doc_id, callback) {
  105. if (callback == null) {
  106. callback = function (error, version) {}
  107. }
  108. db.docOps.findOne(
  109. {
  110. doc_id: ObjectId(doc_id)
  111. },
  112. {
  113. projection: {
  114. version: 1
  115. }
  116. },
  117. function (error, doc) {
  118. if (error != null) {
  119. return callback(error)
  120. }
  121. callback(null, (doc && doc.version) || 0)
  122. }
  123. )
  124. },
  125. setDocVersion(doc_id, version, callback) {
  126. if (callback == null) {
  127. callback = function (error) {}
  128. }
  129. db.docOps.updateOne(
  130. {
  131. doc_id: ObjectId(doc_id)
  132. },
  133. {
  134. $set: { version }
  135. },
  136. {
  137. upsert: true
  138. },
  139. callback
  140. )
  141. },
  142. destroyDoc(doc_id, callback) {
  143. db.docs.deleteOne(
  144. {
  145. _id: ObjectId(doc_id)
  146. },
  147. function (err) {
  148. if (err != null) {
  149. return callback(err)
  150. }
  151. db.docOps.deleteOne(
  152. {
  153. doc_id: ObjectId(doc_id)
  154. },
  155. callback
  156. )
  157. }
  158. )
  159. }
  160. }
  161. const methods = Object.getOwnPropertyNames(MongoManager)
  162. module.exports.promises = {}
  163. for (const method of methods) {
  164. metrics.timeAsyncMethod(MongoManager, method, 'mongo.MongoManager', logger)
  165. module.exports.promises[method] = promisify(module.exports[method])
  166. }