MongoManager.js 3.9 KB

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