1

I'm currently working on a project where ngrx-data is used to store the information. So far it works great, but now I want to store the entity data from an endpoint which gives me two separate entity types in its response.

{
  "posts": [
    {
      "id": 1,
      "title": "Title"
    },
    {
      "id": 2,
      "title": "Title 2"
    }
  ],
  "users": [
    {
      "id": 1,
      "username": "Username"
    },
    {
      "id": 2,
      "username": "Username 2"
    }
  ],
  "date": "1996-10-15T00:05:32.000Z"
}

Now I want to save them separately in the entityCache of ngrx-data. For this I have implemented the basic HTTP service in Angular which extends the DefaultDataService. This I integrated into my facade and added the necessary metadata.

constructor(entityServices: EntityServices) {
  this.postsAndUsersService = entityServices.getEntityCollectionService(
    'PostsAndUsers'
  );
}
export const entityMetadata: EntityMetadataMap = {
  PostsAndUsers: {
    selectId: selectIdPostsAndUsers
  }
}

I'm able to get all the received information "as a whole" from the store, but I think I'm loosing many advantages I get from using ngrx-data (like getting an array of ids).

Is there some way to normalize/split the data and writing it into different sections of the entityCache?

Thank you very much for any hint.

PaNic175
  • 330
  • 4
  • 12

1 Answers1

1

You need to extend the getAll() method in your dataService. getAll(): Observable<Users[]> { return super.getAll().pipe(map(data => data.users)); }

But in this concept you need another dataService for the posts. But i think this is redundance, more better if you implement a custom state management with the ngrx package, because this data package now too strict and only suports the pure C.R.U.D. operations. Or need to separates the data to two API endpoint.

Anarno
  • 1,470
  • 9
  • 18
  • When I extend the getAll method as you mentioned, I would loose access to the `posts`, which I do not want. – PaNic175 Jan 16 '20 at 08:52