@ngrx/data - Passing additional parameters or using custom functions in entity data service?

666 Views Asked by At

I am trying to use ngrx/data to handle CRUD operations on one of my models, but since it is "nested", I need to pass multiple parameters in the getById method. Is there a way to write custom functions in the data service (which extends the default data service) so that they can be accessed via the entity service injected in my component? Or, does the interface of ngrx/data support any additional parameters?

    @Injectable()
export class AutomationEmailDataService extends DefaultDataService<AutomationEmail> {

  constructor(
    http: HttpClient,
    httpUrlGenerator: HttpUrlGenerator,
    private smService: SettingsManagerService
  ) {
    super('AutomationEmail', http, httpUrlGenerator);
  }

  // need to pass multiple keys here
  getById(automationId, emailId): Observable<AutomationEmail> {
    return this.http.get(AutomationsEndpoints.getAutomationEmailInfo(this.smService.getStoreId(), automationId, emailId))
      .map((res: AutomationEmail) => new AutomationEmail().deserialize(res));
  }

  // or, need a custom function that I can access through the entity service 
  getEmail(automationId, emailId): Observable<AutomationEmail> {
     // same http request here
  }

}
1

There are 1 best solutions below

1
On

The getWithQuery function of EntityCollectionServiceBase accepts QueryParams as an argument.

So in your data service:

getEmail(automationId, emailId) {

const params = new HttpParams().set("emailId", emailId).set("automationId", automationId);
this.getWithQuery(params);