2014-04-28 11:45:59 -04:00
|
|
|
{db, ObjectId} = require "./mongojs"
|
2017-03-16 12:46:58 -04:00
|
|
|
logger = require 'logger-sharelatex'
|
|
|
|
metrics = require 'metrics-sharelatex'
|
2014-04-28 11:45:59 -04:00
|
|
|
|
|
|
|
module.exports = MongoManager =
|
2014-04-28 12:43:19 -04:00
|
|
|
|
2016-12-05 12:27:31 -05:00
|
|
|
findDoc: (project_id, doc_id, filter, callback = (error, doc) ->) ->
|
|
|
|
db.docs.find {_id: ObjectId(doc_id.toString()), project_id: ObjectId(project_id.toString())}, filter, (error, docs = []) ->
|
2014-06-05 08:29:50 -04:00
|
|
|
callback error, docs[0]
|
|
|
|
|
2016-12-05 12:27:31 -05:00
|
|
|
getProjectsDocs: (project_id, options = {include_deleted: true}, filter, callback)->
|
2016-12-05 11:31:51 -05:00
|
|
|
query = {project_id: ObjectId(project_id.toString())}
|
2016-12-05 11:48:01 -05:00
|
|
|
if !options.include_deleted
|
2016-12-05 11:31:51 -05:00
|
|
|
query.deleted = { $ne: true }
|
2016-12-05 12:27:31 -05:00
|
|
|
db.docs.find query, filter, callback
|
2014-04-29 10:07:22 -04:00
|
|
|
|
2015-06-02 14:55:22 -04:00
|
|
|
getArchivedProjectDocs: (project_id, callback)->
|
|
|
|
query =
|
|
|
|
project_id: ObjectId(project_id.toString())
|
|
|
|
inS3: true
|
|
|
|
db.docs.find query, {}, callback
|
|
|
|
|
2016-12-02 10:22:08 -05:00
|
|
|
upsertIntoDocCollection: (project_id, doc_id, updates, callback)->
|
2015-01-20 12:09:51 -05:00
|
|
|
update =
|
2016-12-02 10:22:08 -05:00
|
|
|
$set: updates
|
|
|
|
$inc:
|
|
|
|
rev: 1
|
|
|
|
$unset:
|
|
|
|
inS3: true
|
2015-01-20 12:09:51 -05:00
|
|
|
update.$set["project_id"] = ObjectId(project_id)
|
|
|
|
db.docs.update _id: ObjectId(doc_id), update, {upsert: true}, callback
|
|
|
|
|
2016-12-02 10:02:54 -05:00
|
|
|
markDocAsDeleted: (project_id, doc_id, callback)->
|
|
|
|
db.docs.update {
|
|
|
|
_id: ObjectId(doc_id),
|
|
|
|
project_id: ObjectId(project_id)
|
|
|
|
}, {
|
|
|
|
$set: { deleted: true }
|
|
|
|
}, callback
|
2015-06-01 18:36:26 -04:00
|
|
|
|
2015-06-02 07:39:49 -04:00
|
|
|
markDocAsArchived: (doc_id, rev, callback)->
|
2015-06-01 18:36:26 -04:00
|
|
|
update =
|
|
|
|
$set: {}
|
|
|
|
$unset: {}
|
|
|
|
update.$set["inS3"] = true
|
|
|
|
update.$unset["lines"] = true
|
2017-03-30 12:13:43 -04:00
|
|
|
update.$unset["ranges"] = true
|
2015-06-02 14:55:22 -04:00
|
|
|
query =
|
|
|
|
_id: doc_id
|
|
|
|
rev: rev
|
|
|
|
db.docs.update query, update, (err)->
|
2016-11-28 09:55:16 -05:00
|
|
|
callback(err)
|
|
|
|
|
|
|
|
getDocVersion: (doc_id, callback = (error, version) ->) ->
|
|
|
|
db.docOps.find {
|
|
|
|
doc_id: ObjectId(doc_id)
|
|
|
|
}, {
|
|
|
|
version: 1
|
|
|
|
}, (error, docs) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
if docs.length < 1 or !docs[0].version?
|
|
|
|
return callback null, 0
|
|
|
|
else
|
|
|
|
return callback null, docs[0].version
|
|
|
|
|
|
|
|
setDocVersion: (doc_id, version, callback = (error) ->) ->
|
|
|
|
db.docOps.update {
|
|
|
|
doc_id: ObjectId(doc_id)
|
|
|
|
}, {
|
|
|
|
$set: version: version
|
|
|
|
}, {
|
|
|
|
upsert: true
|
2017-03-16 12:46:58 -04:00
|
|
|
}, callback
|
|
|
|
|
2019-07-02 07:45:54 -04:00
|
|
|
destroyDoc: (doc_id, callback) ->
|
|
|
|
db.docs.remove {
|
|
|
|
_id: ObjectId(doc_id)
|
|
|
|
}, (err) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
db.docOps.remove {
|
|
|
|
doc_id: ObjectId(doc_id)
|
|
|
|
}, callback
|
2017-03-16 12:46:58 -04:00
|
|
|
|
2017-03-17 11:27:36 -04:00
|
|
|
[
|
|
|
|
'findDoc',
|
|
|
|
'getProjectsDocs',
|
|
|
|
'getArchivedProjectDocs',
|
|
|
|
'upsertIntoDocCollection',
|
|
|
|
'markDocAsArchived',
|
|
|
|
'getDocVersion',
|
|
|
|
'setDocVersion'
|
|
|
|
].map (method) ->
|
|
|
|
metrics.timeAsyncMethod(MongoManager, method, 'mongo.MongoManager', logger)
|