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

41 lines
1.3 KiB
CoffeeScript
Raw Normal View History

{db, ObjectId} = require "./mongojs"
module.exports = MongoManager =
2014-04-28 12:43:19 -04:00
2014-06-05 08:29:50 -04:00
findDoc: (doc_id, callback = (error, doc) ->) ->
db.docs.find _id: ObjectId(doc_id.toString()), {}, (error, docs = []) ->
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-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
markDocAsDeleted: (doc_id, callback)->
update =
$set: {}
update.$set["deleted"] = true
db.docs.update _id: ObjectId(doc_id), update, (err)->
callback(err)
2015-06-01 18:36:26 -04:00
markDocAsArchived: (doc_id, callback)->
update =
$set: {}
$unset: {}
update.$set["inS3"] = true
update.$unset["lines"] = true
# to ensure that the lines have not changed during the archive process
# what if we passed the lines through into the query {_id: doc_id, lines:lines}
# or more performant would be todo the rev {_id:doc_id, rev:rev}
2015-06-01 18:36:26 -04:00
db.docs.update _id: doc_id, update, (err)->
callback(err)