Skip to content

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

Open
@RemyMachado

Description

@RemyMachado

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions