MongoManager.js 3.7 KB

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