diff --git a/lib/adapters/memory.js b/lib/adapters/memory.js index f40d1753..ee840558 100644 --- a/lib/adapters/memory.js +++ b/lib/adapters/memory.js @@ -229,9 +229,26 @@ Memory.prototype.count = function count(model, callback, where) { }; Memory.prototype.updateAttributes = function updateAttributes(model, id, data, cb) { + if(!id) { + var err = new Error('You must provide an id when updating attributes!'); + if(cb) { + return cb(err); + } else { + throw err; + } + } + data.id = id; - var base = JSON.parse(this.cache[model][id]); - this.save(model, merge(base, data), cb); + + var cachedModels = this.cache[model]; + var modelAsString = cachedModels && this.cache[model][id]; + var modelData = modelAsString && JSON.parse(modelAsString); + + if(modelData) { + this.save(model, merge(base, data), cb); + } else { + cb(new Error('Could not update attributes. Object with id ' + id + ' does not exist!')); + } }; Memory.prototype.transaction = function () {