batch: add convenience batch.set / batch.get for updating paths simultaneously

This commit is contained in:
Tyler Renelle
2013-05-29 10:54:16 +01:00
parent 1745ae6205
commit 4c684502f2
4 changed files with 16 additions and 21 deletions
+4 -1
View File
@@ -7,9 +7,12 @@ character = require('./character')
module.exports.batchTxn = batchTxn = (model, cb, options) ->
user = model.at("_user")
uObj = hydrate(user.get()) # see https://github.com/codeparty/racer/issues/116
batch =
set: (k,v) -> helpers.dotSet(k,v,uObj); paths[k] = true
get: (k) -> helpers.dotGet(k,uObj)
paths = {}
model._dontPersist = true
cb uObj, paths
cb uObj, paths, batch
_.each paths, (v,k) -> user.pass({cron:options?.cron}).set(k,helpers.dotGet(k, uObj));true
model._dontPersist = false
# some hackery in our own branched racer-db-mongo, see findAndModify of lefnire/racer-db-mongo#habitrpg index.js