redux
redux copied to clipboard
Normalizing State Shape - Relationships and Tables - Join/associative table example
First of all, I love the new documentation, last time I checked it was years ago and was way more confusing.
What docs page needs to be fixed?
Using Redux > Redux logic and patterns > Structuring reducers > Nomalizing State Shape:
- Relationships and Tables > Join/associative table example https://redux.js.org/usage/structuring-reducers/normalizing-state-shape#relationships-and-tables
What is the problem?
From the documentation, we have this given code:
{
entities: {
authors : { byId : {}, allIds : [] },
books : { byId : {}, allIds : [] },
authorBook : {
byId : {
1 : {
id : 1,
authorId : 5,
bookId : 22
},
2 : {
id : 2,
authorId : 5,
bookId : 15,
},
3 : {
id : 3,
authorId : 42,
bookId : 12
}
},
allIds : [1, 2, 3]
}
}
}
It is stated:
Operations like "Look up all books by this author", can then be accomplished easily with a single loop over the join table.
If I understood well, the goal is to loop over authorBook
keys to save all bookId
s where authorId
is the one we want.
I struggle to understand how it is better than retrieving bookId
s directly with authors.byId[id].books
?
However, if we didn't want to store bookId
s inside the author
schema, we could have used a look up table like this one:
booksByAuthorId : {
5 : {
authorId : 5,
bookIds : [22, 23]
},
}
Wouldn't it make more sense?
I asked my backend team and they were also confused about it.
It would be really appreciated if someone could enlighten us.
What should be changed to fix the problem?
If we couldn't understand it, maybe it's a sign it needs to be more explicit (e.g. more/better examples).
FWIW, I wrote that docs page in 2016, and when I did so I wrote it based on the approach used by https://github.com/redux-orm/redux-orm at the time.
There are probably a number of different ways that you could store the key associations, and I was trying to avoid saying "you must do it $SPECIFIC_WAY". Instead, I was trying to offer some suggestions to get people thinking in certain directions.