2020-02-16 09:01:47 -05:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
handle-callback-err,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-16 09:01:46 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-16 09:02:21 -05:00
|
|
|
let MongoManager
|
|
|
|
const { db, ObjectId } = require('./mongojs')
|
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
const metrics = require('metrics-sharelatex')
|
2020-07-31 11:20:07 -04:00
|
|
|
const { promisify } = require('util')
|
2014-04-28 11:45:59 -04:00
|
|
|
|
2020-02-16 09:02:21 -05:00
|
|
|
module.exports = MongoManager = {
|
|
|
|
findDoc(project_id, doc_id, filter, callback) {
|
|
|
|
if (callback == null) {
|
2020-05-28 09:20:54 -04:00
|
|
|
callback = function (error, doc) {}
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
|
|
|
return db.docs.find(
|
|
|
|
{
|
|
|
|
_id: ObjectId(doc_id.toString()),
|
|
|
|
project_id: ObjectId(project_id.toString())
|
|
|
|
},
|
|
|
|
filter,
|
2020-05-28 09:20:54 -04:00
|
|
|
function (error, docs) {
|
2020-02-16 09:02:21 -05:00
|
|
|
if (docs == null) {
|
|
|
|
docs = []
|
|
|
|
}
|
|
|
|
return callback(error, docs[0])
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
2014-04-28 12:43:19 -04:00
|
|
|
|
2020-02-16 09:02:21 -05:00
|
|
|
getProjectsDocs(project_id, options, filter, callback) {
|
|
|
|
if (options == null) {
|
|
|
|
options = { include_deleted: true }
|
|
|
|
}
|
|
|
|
const query = { project_id: ObjectId(project_id.toString()) }
|
|
|
|
if (!options.include_deleted) {
|
|
|
|
query.deleted = { $ne: true }
|
|
|
|
}
|
|
|
|
return db.docs.find(query, filter, callback)
|
|
|
|
},
|
2014-06-05 08:29:50 -04:00
|
|
|
|
2020-02-16 09:02:21 -05:00
|
|
|
getArchivedProjectDocs(project_id, callback) {
|
|
|
|
const query = {
|
|
|
|
project_id: ObjectId(project_id.toString()),
|
|
|
|
inS3: true
|
|
|
|
}
|
|
|
|
return db.docs.find(query, {}, callback)
|
|
|
|
},
|
2014-04-29 10:07:22 -04:00
|
|
|
|
2020-02-16 09:02:21 -05:00
|
|
|
upsertIntoDocCollection(project_id, doc_id, updates, callback) {
|
|
|
|
const update = {
|
|
|
|
$set: updates,
|
|
|
|
$inc: {
|
|
|
|
rev: 1
|
|
|
|
},
|
|
|
|
$unset: {
|
|
|
|
inS3: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
update.$set.project_id = ObjectId(project_id)
|
|
|
|
return db.docs.update(
|
|
|
|
{ _id: ObjectId(doc_id) },
|
|
|
|
update,
|
|
|
|
{ upsert: true },
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
2015-06-02 14:55:22 -04:00
|
|
|
|
2020-02-16 09:02:21 -05:00
|
|
|
markDocAsDeleted(project_id, doc_id, callback) {
|
|
|
|
return db.docs.update(
|
|
|
|
{
|
|
|
|
_id: ObjectId(doc_id),
|
|
|
|
project_id: ObjectId(project_id)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$set: { deleted: true }
|
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
2015-01-20 12:09:51 -05:00
|
|
|
|
2020-02-16 09:02:21 -05:00
|
|
|
markDocAsArchived(doc_id, rev, callback) {
|
|
|
|
const update = {
|
|
|
|
$set: {},
|
|
|
|
$unset: {}
|
|
|
|
}
|
|
|
|
update.$set.inS3 = true
|
|
|
|
update.$unset.lines = true
|
|
|
|
update.$unset.ranges = true
|
|
|
|
const query = {
|
|
|
|
_id: doc_id,
|
|
|
|
rev
|
|
|
|
}
|
2020-05-28 09:20:54 -04:00
|
|
|
return db.docs.update(query, update, (err) => callback(err))
|
2020-02-16 09:02:21 -05:00
|
|
|
},
|
2015-06-01 18:36:26 -04:00
|
|
|
|
2020-02-16 09:02:21 -05:00
|
|
|
getDocVersion(doc_id, callback) {
|
|
|
|
if (callback == null) {
|
2020-05-28 09:20:54 -04:00
|
|
|
callback = function (error, version) {}
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
|
|
|
return db.docOps.find(
|
|
|
|
{
|
|
|
|
doc_id: ObjectId(doc_id)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
version: 1
|
|
|
|
},
|
2020-05-28 09:20:54 -04:00
|
|
|
function (error, docs) {
|
2020-02-16 09:02:21 -05:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (docs.length < 1 || docs[0].version == null) {
|
|
|
|
return callback(null, 0)
|
|
|
|
} else {
|
|
|
|
return callback(null, docs[0].version)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
2016-11-28 09:55:16 -05:00
|
|
|
|
2020-02-16 09:02:21 -05:00
|
|
|
setDocVersion(doc_id, version, callback) {
|
|
|
|
if (callback == null) {
|
2020-05-28 09:20:54 -04:00
|
|
|
callback = function (error) {}
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
|
|
|
return db.docOps.update(
|
|
|
|
{
|
|
|
|
doc_id: ObjectId(doc_id)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$set: { version }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
upsert: true
|
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
2017-03-16 12:46:58 -04:00
|
|
|
|
2020-02-16 09:02:21 -05:00
|
|
|
destroyDoc(doc_id, callback) {
|
|
|
|
return db.docs.remove(
|
|
|
|
{
|
|
|
|
_id: ObjectId(doc_id)
|
|
|
|
},
|
2020-05-28 09:20:54 -04:00
|
|
|
function (err) {
|
2020-02-16 09:02:21 -05:00
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
return db.docOps.remove(
|
|
|
|
{
|
|
|
|
doc_id: ObjectId(doc_id)
|
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2020-07-31 11:20:07 -04:00
|
|
|
|
|
|
|
const methods = Object.getOwnPropertyNames(MongoManager)
|
|
|
|
|
|
|
|
module.exports.promises = {}
|
|
|
|
for (const method of methods) {
|
2020-02-16 09:02:21 -05:00
|
|
|
metrics.timeAsyncMethod(MongoManager, method, 'mongo.MongoManager', logger)
|
2020-07-31 11:20:07 -04:00
|
|
|
module.exports.promises[method] = promisify(module.exports[method])
|
|
|
|
}
|