MongoManager.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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('./mongojs')
  15. const logger = require('logger-sharelatex')
  16. const metrics = require('metrics-sharelatex')
  17. module.exports = MongoManager = {
  18. findDoc(project_id, doc_id, filter, callback) {
  19. if (callback == null) {
  20. callback = function(error, doc) {}
  21. }
  22. return db.docs.find(
  23. {
  24. _id: ObjectId(doc_id.toString()),
  25. project_id: ObjectId(project_id.toString())
  26. },
  27. filter,
  28. function(error, docs) {
  29. if (docs == null) {
  30. docs = []
  31. }
  32. return callback(error, docs[0])
  33. }
  34. )
  35. },
  36. getProjectsDocs(project_id, options, filter, callback) {
  37. if (options == null) {
  38. options = { include_deleted: true }
  39. }
  40. const query = { project_id: ObjectId(project_id.toString()) }
  41. if (!options.include_deleted) {
  42. query.deleted = { $ne: true }
  43. }
  44. return db.docs.find(query, filter, callback)
  45. },
  46. getArchivedProjectDocs(project_id, callback) {
  47. const query = {
  48. project_id: ObjectId(project_id.toString()),
  49. inS3: true
  50. }
  51. return db.docs.find(query, {}, callback)
  52. },
  53. upsertIntoDocCollection(project_id, doc_id, updates, callback) {
  54. const update = {
  55. $set: updates,
  56. $inc: {
  57. rev: 1
  58. },
  59. $unset: {
  60. inS3: true
  61. }
  62. }
  63. update.$set.project_id = ObjectId(project_id)
  64. return db.docs.update(
  65. { _id: ObjectId(doc_id) },
  66. update,
  67. { upsert: true },
  68. callback
  69. )
  70. },
  71. markDocAsDeleted(project_id, doc_id, callback) {
  72. return db.docs.update(
  73. {
  74. _id: ObjectId(doc_id),
  75. project_id: ObjectId(project_id)
  76. },
  77. {
  78. $set: { deleted: true }
  79. },
  80. callback
  81. )
  82. },
  83. markDocAsArchived(doc_id, rev, callback) {
  84. const update = {
  85. $set: {},
  86. $unset: {}
  87. }
  88. update.$set.inS3 = true
  89. update.$unset.lines = true
  90. update.$unset.ranges = true
  91. const query = {
  92. _id: doc_id,
  93. rev
  94. }
  95. return db.docs.update(query, update, err => callback(err))
  96. },
  97. getDocVersion(doc_id, callback) {
  98. if (callback == null) {
  99. callback = function(error, version) {}
  100. }
  101. return db.docOps.find(
  102. {
  103. doc_id: ObjectId(doc_id)
  104. },
  105. {
  106. version: 1
  107. },
  108. function(error, docs) {
  109. if (error != null) {
  110. return callback(error)
  111. }
  112. if (docs.length < 1 || docs[0].version == null) {
  113. return callback(null, 0)
  114. } else {
  115. return callback(null, docs[0].version)
  116. }
  117. }
  118. )
  119. },
  120. setDocVersion(doc_id, version, callback) {
  121. if (callback == null) {
  122. callback = function(error) {}
  123. }
  124. return db.docOps.update(
  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. return db.docs.remove(
  139. {
  140. _id: ObjectId(doc_id)
  141. },
  142. function(err) {
  143. if (err != null) {
  144. return callback(err)
  145. }
  146. return db.docOps.remove(
  147. {
  148. doc_id: ObjectId(doc_id)
  149. },
  150. callback
  151. )
  152. }
  153. )
  154. }
  155. }
  156. ;[
  157. 'findDoc',
  158. 'getProjectsDocs',
  159. 'getArchivedProjectDocs',
  160. 'upsertIntoDocCollection',
  161. 'markDocAsArchived',
  162. 'getDocVersion',
  163. 'setDocVersion'
  164. ].map(method =>
  165. metrics.timeAsyncMethod(MongoManager, method, 'mongo.MongoManager', logger)
  166. )