MongoManager.js 3.6 KB

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