MongoManager.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. // TODO(das7pad): remove name filter after back filling data
  42. name: { $exists: true }
  43. },
  44. {
  45. projection: filter,
  46. sort: { deletedAt: -1 },
  47. limit: Settings.max_deleted_docs
  48. }
  49. )
  50. .toArray(callback)
  51. },
  52. getProjectsDocs(project_id, options, filter, callback) {
  53. const query = { project_id: ObjectId(project_id.toString()) }
  54. if (!options.include_deleted) {
  55. query.deleted = { $ne: true }
  56. }
  57. db.docs
  58. .find(query, {
  59. projection: filter
  60. })
  61. .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. markDocAsDeleted(project_id, doc_id, callback) {
  97. db.docs.updateOne(
  98. {
  99. _id: ObjectId(doc_id),
  100. project_id: ObjectId(project_id)
  101. },
  102. {
  103. $set: { deleted: true }
  104. },
  105. callback
  106. )
  107. },
  108. patchDoc(project_id, doc_id, meta, callback) {
  109. db.docs.updateOne(
  110. {
  111. _id: ObjectId(doc_id),
  112. project_id: ObjectId(project_id)
  113. },
  114. { $set: meta },
  115. callback
  116. )
  117. },
  118. markDocAsArchived(doc_id, rev, callback) {
  119. const update = {
  120. $set: {},
  121. $unset: {}
  122. }
  123. update.$set.inS3 = true
  124. update.$unset.lines = true
  125. update.$unset.ranges = true
  126. const query = {
  127. _id: doc_id,
  128. rev
  129. }
  130. db.docs.updateOne(query, update, callback)
  131. },
  132. getDocVersion(doc_id, callback) {
  133. if (callback == null) {
  134. callback = function (error, version) {}
  135. }
  136. db.docOps.findOne(
  137. {
  138. doc_id: ObjectId(doc_id)
  139. },
  140. {
  141. projection: {
  142. version: 1
  143. }
  144. },
  145. function (error, doc) {
  146. if (error != null) {
  147. return callback(error)
  148. }
  149. callback(null, (doc && doc.version) || 0)
  150. }
  151. )
  152. },
  153. setDocVersion(doc_id, version, callback) {
  154. if (callback == null) {
  155. callback = function (error) {}
  156. }
  157. db.docOps.updateOne(
  158. {
  159. doc_id: ObjectId(doc_id)
  160. },
  161. {
  162. $set: { version }
  163. },
  164. {
  165. upsert: true
  166. },
  167. callback
  168. )
  169. },
  170. destroyDoc(doc_id, callback) {
  171. db.docs.deleteOne(
  172. {
  173. _id: ObjectId(doc_id)
  174. },
  175. function (err) {
  176. if (err != null) {
  177. return callback(err)
  178. }
  179. db.docOps.deleteOne(
  180. {
  181. doc_id: ObjectId(doc_id)
  182. },
  183. callback
  184. )
  185. }
  186. )
  187. }
  188. }
  189. const methods = Object.getOwnPropertyNames(MongoManager)
  190. module.exports.promises = {}
  191. for (const method of methods) {
  192. metrics.timeAsyncMethod(MongoManager, method, 'mongo.MongoManager', logger)
  193. module.exports.promises[method] = promisify(module.exports[method])
  194. }