MongoManager.js 4.6 KB

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