MongoManager.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 Settings = require('settings-sharelatex')
  18. const { promisify } = require('util')
  19. module.exports = MongoManager = {
  20. findDoc(project_id, doc_id, filter, callback) {
  21. if (callback == null) {
  22. callback = function (error, doc) {}
  23. }
  24. db.docs.findOne(
  25. {
  26. _id: ObjectId(doc_id.toString()),
  27. project_id: ObjectId(project_id.toString())
  28. },
  29. {
  30. projection: filter
  31. },
  32. callback
  33. )
  34. },
  35. getProjectsDeletedDocs(project_id, filter, callback) {
  36. db.docs
  37. .find(
  38. {
  39. project_id: ObjectId(project_id.toString()),
  40. deleted: true
  41. },
  42. {
  43. projection: filter,
  44. sort: { deletedAt: -1 },
  45. limit: Settings.max_deleted_docs
  46. }
  47. )
  48. .toArray(callback)
  49. },
  50. getProjectsDocs(project_id, options, filter, callback) {
  51. const query = { project_id: ObjectId(project_id.toString()) }
  52. if (!options.include_deleted) {
  53. query.deleted = { $ne: true }
  54. }
  55. const queryOptions = {
  56. projection: filter
  57. }
  58. if (options.limit) {
  59. queryOptions.limit = options.limit
  60. }
  61. db.docs.find(query, queryOptions).toArray(callback)
  62. },
  63. getArchivedProjectDocs(project_id, callback) {
  64. const query = {
  65. project_id: ObjectId(project_id.toString()),
  66. inS3: true
  67. }
  68. db.docs.find(query).toArray(callback)
  69. },
  70. getNonDeletedArchivedProjectDocs(project_id, callback) {
  71. const query = {
  72. project_id: ObjectId(project_id.toString()),
  73. deleted: { $ne: true },
  74. inS3: true
  75. }
  76. db.docs.find(query).toArray(callback)
  77. },
  78. upsertIntoDocCollection(project_id, doc_id, updates, callback) {
  79. const update = {
  80. $set: updates,
  81. $inc: {
  82. rev: 1
  83. },
  84. $unset: {
  85. inS3: true
  86. }
  87. }
  88. update.$set.project_id = ObjectId(project_id)
  89. db.docs.updateOne(
  90. { _id: ObjectId(doc_id) },
  91. update,
  92. { upsert: true },
  93. callback
  94. )
  95. },
  96. patchDoc(project_id, doc_id, meta, callback) {
  97. db.docs.updateOne(
  98. {
  99. _id: ObjectId(doc_id),
  100. project_id: ObjectId(project_id)
  101. },
  102. { $set: meta },
  103. callback
  104. )
  105. },
  106. markDocAsArchived(doc_id, rev, callback) {
  107. const update = {
  108. $set: {},
  109. $unset: {}
  110. }
  111. update.$set.inS3 = true
  112. update.$unset.lines = true
  113. update.$unset.ranges = true
  114. const query = {
  115. _id: doc_id,
  116. rev
  117. }
  118. db.docs.updateOne(query, update, callback)
  119. },
  120. getDocVersion(doc_id, callback) {
  121. if (callback == null) {
  122. callback = function (error, version) {}
  123. }
  124. db.docOps.findOne(
  125. {
  126. doc_id: ObjectId(doc_id)
  127. },
  128. {
  129. projection: {
  130. version: 1
  131. }
  132. },
  133. function (error, doc) {
  134. if (error != null) {
  135. return callback(error)
  136. }
  137. callback(null, (doc && doc.version) || 0)
  138. }
  139. )
  140. },
  141. setDocVersion(doc_id, version, callback) {
  142. if (callback == null) {
  143. callback = function (error) {}
  144. }
  145. db.docOps.updateOne(
  146. {
  147. doc_id: ObjectId(doc_id)
  148. },
  149. {
  150. $set: { version }
  151. },
  152. {
  153. upsert: true
  154. },
  155. callback
  156. )
  157. },
  158. destroyDoc(doc_id, callback) {
  159. db.docs.deleteOne(
  160. {
  161. _id: ObjectId(doc_id)
  162. },
  163. function (err) {
  164. if (err != null) {
  165. return callback(err)
  166. }
  167. db.docOps.deleteOne(
  168. {
  169. doc_id: ObjectId(doc_id)
  170. },
  171. callback
  172. )
  173. }
  174. )
  175. }
  176. }
  177. const methods = Object.getOwnPropertyNames(MongoManager)
  178. module.exports.promises = {}
  179. for (const method of methods) {
  180. metrics.timeAsyncMethod(MongoManager, method, 'mongo.MongoManager', logger)
  181. module.exports.promises[method] = promisify(module.exports[method])
  182. }