Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalizing State Shape - Relationships and Tables​ - Join/associative table example #4234

Open
RemyMachado opened this issue Dec 8, 2021 · 1 comment

Comments

@RemyMachado
Copy link

RemyMachado commented Dec 8, 2021

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:

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 bookIds where authorId is the one we want.
I struggle to understand how it is better than retrieving bookIds directly with authors.byId[id].books?
However, if we didn't want to store bookIds 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).

@markerikson
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants