Search code examples
angularngrxngrx-entity

Angular NGRX Entity counting the same object


I have a call where I get a list of Ids of person who have outstanding reports to file, and I want to keep track of the amount of reports that person has outstanding. I'm storing that data using EntityAdapter. Now I can't figure out how to keep count in the adapter.

So far I've checked the ngrx docs, tried quite a few manipulations to the code and asked my question on the gitter chat room, with no result so far.

My service returns an array of strings, which contains the id of the person. Example data could be:

Array of person IDs

Here I have the same Id twice, which means that person has 2 reports. I want to store that Id and keep count '2'. At the moment I'm using this code for the adapter:

    export interface State extends EntityState<string> {
      showRegistrations: boolean,
      loading: boolean,
      loaded: boolean,
    }

    export const adapter: EntityAdapter<string> = createEntityAdapter<string>({
      selectId: (item: string) => item,
      sortComparer: false,
    });

    export const initialState: State = adapter.getInitialState({
      showRegistrations: true,
      loading: false,
      loaded: false,
    });

This gives me in my store following result:

store result

But I'm actually looking for following result, where each Id is stored and I know how many times that Id was found specifically:

enter image description here


Solution

  • @ngrx/entity is aimed to store and retrieve entities based on an id. The use case that you're describing is not fitted for @ngrx/entity.