Skip to content

Commit

Permalink
fix: store('scope').update() ignores scope
Browse files Browse the repository at this point in the history
Ensures that when calling `store('scope').update(...)`, that only items in scope are updated.  Items out of scope are ignored.

This code assumes the following:
* `storedObjects` is an array with the exact same size as `objectsOrIds`
* `objectsOrIds` is either a string, an object, a list of strings, or a list of objects
* if `objectsOrIds` is not an array then it is a single string representing an id or a single object and, as a result, `storedObjects` will either be null or a single object
* when attempting to find a single objectOrId, a single object is returned (rather than an array with one object)
* every item returned by find() will have an id associated with it.
  • Loading branch information
capellini committed Aug 27, 2016
1 parent f1057f8 commit 5fd6289
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion lib/scoped/update.js
@@ -1,11 +1,59 @@
module.exports = update

var find = require('./find')
var merge = require('lodash/merge')

function update (type, api, objectsOrIds, change) {
return find(type, api, objectsOrIds)

.then(function (storedObjects) {
return api.update(objectsOrIds, change)
// objectsOrIds may contain updated information for the respective storedObject
// (e.g. _deleted = true), so incorporate all information in the objectOrId into
// the storedObject
var updatedObjects = updateObjectsAndFilterErrors(objectsOrIds, storedObjects)

return api.update(updatedObjects, change)

.then(function (apiUpdates) {
if (!Array.isArray(apiUpdates)) {
return apiUpdates
}

return restoreOriginalFindErrors(storedObjects, apiUpdates)
})
})
}

function updateObjectsAndFilterErrors (objectsOrIds, storedObjects) {
if (!Array.isArray(objectsOrIds)) {
return updateObject(objectsOrIds, storedObjects)
}

return storedObjects.map(function (storedObject, index) {
if (storedObject instanceof Error) {
return storedObject
}

return updateObject(objectsOrIds[index], storedObject)
}).filter(function (storedObject) {
return !(storedObject instanceof Error)
})
}

function updateObject (objectOrId, storedObject) {
if (typeof objectOrId !== 'object') {
return storedObject
}

return merge(storedObject, objectOrId)
}

function restoreOriginalFindErrors (foundObjects, apiUpdates) {
return foundObjects.map(function (foundObject) {
if (foundObject instanceof Error) {
return foundObject
}

return apiUpdates.shift()
})
}

0 comments on commit 5fd6289

Please sign in to comment.