How to handle related entities

How to build app state if I have related entities using ngrx-store (redux)?

For example, I have model called "Post" and rest api returns json like this:

[
   { id: 1, title: 'Title 1', user: { id: 1, name: 'User name' } },
   { id: 2, title: 'Title 2', user: { id: 1, name: 'User name' } },
]

What the best way to store that data?

Upvotes: 1

Views: 352

Answers (1)

cartant
cartant

Reputation: 58400

There is no single, best way to store the data, but you should store relational data in a normalized fashion. For example, you could store it something like this:

{
  "posts": { 
    "1": { 
      id: 1,
      title: "Title 1",
      user: 1
    },
    "2": { 
      id: 2,
      title: "Title 2",
      user: 1
    }
  },
  "users": {
    "1": {
      "id": 1,
      "name": "User name"
    }
  }
}

normalizr can be helpful in re-arranging relational data for storage in a Redux store.

Also, there is a related answer here that contains additional references.

Upvotes: 1

Related Questions