overleaf/services/docstore/app/coffee/MongoManager.coffee

69 lines
1.8 KiB
CoffeeScript
Raw Normal View History

{db, ObjectId} = require "./mongojs"
module.exports = MongoManager =
2014-04-28 12:43:19 -04:00
2016-09-02 08:32:50 -04:00
findDoc: (project_id, doc_id, callback = (error, doc) ->) ->
db.docs.find {_id: ObjectId(doc_id.toString()), project_id: ObjectId(project_id.toString())}, {}, (error, docs = []) ->
2014-06-05 08:29:50 -04:00
callback error, docs[0]
getProjectsDocs: (project_id, callback)->
db.docs.find project_id: ObjectId(project_id.toString()), {}, 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
2015-02-27 09:06:06 -05:00
upsertIntoDocCollection: (project_id, doc_id, lines, callback)->
update =
$set:{}
$inc:{}
2015-06-01 18:36:26 -04:00
$unset:{}
update.$set["lines"] = lines
update.$set["project_id"] = ObjectId(project_id)
update.$inc["rev"] = 1 #on new docs being created this will set the rev to 1
2015-06-01 18:36:26 -04:00
update.$unset["inS3"] = true
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
markDocAsArchived: (doc_id, rev, callback)->
2015-06-01 18:36:26 -04:00
update =
$set: {}
$unset: {}
update.$set["inS3"] = true
update.$unset["lines"] = true
2015-06-02 14:55:22 -04:00
query =
_id: doc_id
rev: rev
db.docs.update query, update, (err)->
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
}, callback