Simplify Collection Management with ngrx Entity in Angular

InstructorLukas Ruebbelke

Share this video with your friends

Send Tweet

Data manipulation on collections can be expensive because it is usually necessary to iterate over the collection at least once to perform any kind of update or selection to it. A common pattern for avoiding expensive iterations is to break a collection into a key-value store that allows for instantaneous lookup of value provided you have the key. We could handle this data transformation ourselves but ngrx entity was created to do all the heavy lifting for us. In this lesson, we will see how to update how we define our feature state to use EntityState, create an EntityAdapter, define our initial state using our adapter and most importantly, how to leverage the convenience methods that the adapter gives us in our reducer. We will also examine how ngrx entity changes the shape of our state by splitting our collection into ids and entities and how to update our feature selector to work with it.

Adam
~ 4 years ago

@Lukas The remapping of projects to observable stream seems awkward. Why would we use an entity adapter to make this nice structure only to use an inefficient remapping of key value pairs.

this.projects$ = store.pipe(
      select('projects'),
      map(data => data.entities),
      map(entity => Object.keys(entity).map(k => entity[k]))
);

This seems bad. Is the entityAdapter overkill if we have to remap like this to show it in the view? What happens if our list of projects is >1000?

Lukas Ruebbelkeinstructor
~ 4 years ago

Keep watching ๐Ÿ˜€ I would never do this in "real life" as that is what selectors are for

Adam
~ 4 years ago

Keep watching ๐Ÿ˜€ I would never do this in "real life" as that is what selectors are for

Haha, sorry. I spoke too soon!

Lukas Ruebbelkeinstructor
~ 4 years ago

No worries at all! #highFive