flux icon indicating copy to clipboard operation
flux copied to clipboard

[Question] If there are many stores that store the same type of records, does it make sense for them to store only record IDs?

Open szimek opened this issue 11 years ago • 3 comments

Let's assume that there are many stores that store e.g. user records like users' friend lists, user search results, likers of posts, authors of comments etc. It's possible that some users will be in more than one store, e.g. one of my friends likes one of my posts. Does it make sense to store all user records in a single store and keep only their IDs in derived stores to avoid storing copies and worry about synchronizing them across stores? If so, would the main user store need to listen to all actions that can change any of the derived stores or is there some other way to update it when one of the derived stores changes?

szimek avatar Mar 10 '15 22:03 szimek

Yes, this is often the best approach. Just like relational databases should be normalized, so should the stores. Some folks may disagree with me, but I think you save yourself some insanity if you maintain a single source of truth for all data.

would the main user store need to listen to all actions that can change any of the derived stores or is there some other way to update it when one of the derived stores changes?

The main store is already receiving all the actions, but yes it might need to have more cases in the switch statement that handles the actions.

You may be touching on one of the problems we've discovered at Facebook in working in very large Flux applications: when an engineer is adding code to handle new actions, how is that engineer to know what other stores need to also be updated to handle the same actions?

We have some new solutions that help with this, and we hope to open source them soon in the form of both a FluxStore base class and some non-breaking changes to the dispatcher. These include a hasChanged() method in the store, and a mechanism by which additional callbacks may be invoked at the end of the dispatch, after all data mutations are complete. One can build a system on top of these features that will alert engineers with console warnings when dependent stores need to handle the same actions as their "upstream" stores.

fisherwebdev avatar Mar 29 '15 13:03 fisherwebdev

Thank you for reporting this issue and appreciate your patience. We've notified the core team for an update on this issue. We're looking for a response within the next 30 days or the issue may be closed.

ghost avatar Aug 05 '15 18:08 ghost

Appears answered.

kyldvs avatar Nov 18 '15 21:11 kyldvs