cantierecreativo / redux-bees

A nice, short and declarative way to interact with JSON APIs
592 stars 41 forks source link

Has anyone come up with a way to update a single entity in a list? #62

Open stueynet opened 6 years ago

stueynet commented 6 years ago

When you have a list of items already loaded, if one of items was updated, it seems you always have to invalidate and make the request again to the whole list of resources. This is the same for a new resource being added. So a new message comes in for example, and you have to fetch ALL messages. This is how the documentation suggests you do it but it is pretty non-performance. Particular when you want to be able to patch in updates from items within a list. You don't want to be always making the API call for a whole list of things just because one of them updated.

Anyone come up with a solution for this using bees?

dlikhten commented 5 years ago

If you request a list, say /api/comments?filter[mine]=true, you can always update a single record /api/comments/10. As long as you actually select the objects from redux, you will always have the freshest data. So updating a single record will update that redux record. There is only one cannonical record for comments/10 (state.bees.entities.comments['10']) so any updates will simply update that record.

In fact, the entities grabbed from a responses' results (getRequestInfo) are mapped to the actual values in redux, so in the end you're always looking at the freshest object. The advantage of having a key-value store vs a denormalized data set (https://github.com/cantierecreativo/redux-bees/blob/master/src/selectors.js#L58).