Upgrade to Pro — share decks privately, control downloads, hide ads and more …

@ngrx/entity - Managing collections with ease

Yannick Baron
February 24, 2023

@ngrx/entity - Managing collections with ease

Using @ngrx/store or @ngrx/component-store?
Handling lists of entities yourself?
@ngrx/entity and EntityAdapter to the rescue!

Yannick Baron

February 24, 2023
Tweet

More Decks by Yannick Baron

Other Decks in Technology

Transcript

  1. • hold several entities • entities unique/identi fi able •

    can have an order • can be implemented by various data structures Collections
  2. • add an entity, add many, ... • update an

    entity, update many, ... • remove entity, remove many, ... • read speci fi c entity, fi nd entities matching a condition, ... Operations on Collections
  3. // collection
 let collection: Entity[] = [...]; // item we

    want to add
 const newItem: Entity = { id: 'itemId', ... }; // simply appending to the end
 collection = collection.concat(newItem);
 collection = [...collection, newItem];
 
 // beware of duplicates + order Adding to the collection (array)
  4. // collection
 let collection: Entity[] = [...]; // id we

    want to remove
 const removeId = 'itemId'; // filter the collection
 collection = collection.filter(item => item.id !== removeId); Removing from the collection (array)
  5. // collection
 let collection: Entity[] = [...]; // item we

    want to update
 const updateId = 'itemId';
 const update: Partial<Entity> = { foobar: 42 }; // map the collection
 collection = collection
 .map(item => item.id === updateId ? { ...item, ...update } : item); Updating an item in the collection (array)
  6. // collection
 let collection: Entity[] = [...]; // id we

    are trying to find
 const target = 'itemId'; // find in the collection
 const item: Entity | undefined = collection.find(item => item.id === target); // worst case: iterates whole list without a match Read an item from the collection (array) by id
  7. // collection
 let collection: Entity[] = [...]; // filter the

    collection
 const items = collection.filter(item => item.foobar >= 42); Find items in the collection (array) by condition
  8. • many applications handle one or many collections of entities

    • always same/similar CRUD code • often similar logic in regards sorting + uniqueness of items → Abstraction possible! → @ngrx/entity provides the EntityAdapter Boilerplate? Repetition?
  9. • stateless class provided by the @ngrx/entity package • stores

    many entities ef fi ciently • provides methods and selectors to manage a collection immutably • agnostic, can be used in many contexts (Store, ComponentStore) → reduces boilerplate / repetition → keeps reducers small What is the EntityAdapter?
  10. const entityAdapter = createEntityAdapter(); 
 
 // optionally provide id

    accessor and/or comparator
 const personAdapter = createEntityAdapter<Person>({
 selectId: ({ uid }) => uid,
 sortComparer: (p1, p2) => p1.foobar - p2.foobar,
 }); Creating an EntityAdapter
  11. let collection = entityAdapter.getInitialState(); 
 
 // optionally initialise additional

    state properties
 let collection = createEntityAdapter<Entity>({
 loading: false,
 }); Get the initial collection state
  12. // add one
 const newItem: Entity = { id: 'item-1',

    ... };
 collection = entityAdapter.addOne(newEntity, collection);
 
 // remove one
 collection = entityAdapter.removeOne('item-2', collection);
 
 // update one
 const update: Update<Entity> = {
 id: "item-2",
 changes: { foobar: 52 },
 }
 collection = entityAdapter.updateOne(update, collection); Update the collection
  13. const { selectAll, selectTotal } = entityAdapter.getSelectors();
 
 const all

    = selectAll(collection);
 const count = selectTotal(collection); const { selectEntities, selectIds } = entityAdapter.getSelectors();
 
 const entity = selectEntities(collection)["item-1"];
 const ids = selectIds(collection); Read from the collection via selectors
  14. • easy to use • reduces boilerplate / repetition •

    use with Store and ComponentStore → use it → make your life easier EntityAdapter