3

I am using Angular 8 with @ngrx/data to manage my entities. When I initiate a delete operation which fails (server returns 500), the entity is removed from the ngrx client side cache, despite it not being deleted on the server.

How do I prevent the entity from being removed from the cache on error? Or how do I re-add the (non-)deleted entity back to the cache on error?

Aviad P.
  • 32,036
  • 14
  • 103
  • 124

1 Answers1

3

Use pesimistic save https://ngrx.io/guide/data/entity-change-tracker#save-errors

The EntityActions whose operation names end in _OPTIMISTIC start an optimistic save.

Many apps are easier to build when saves are "optimistic" because the changes are immediately available to application code that is watching collection selectors. The app doesn't have to wait for confirmation that the entity operation succeeded on the server.

A pessimistic save doesn't update the store until the server until the server confirms that the save succeeded, which NgRx Data then turns into a "SUCCESS" action that updates the collection. With a pessimistic save, the changes won't be available in the store

Xesenix
  • 2,418
  • 15
  • 30
  • 2
    Thanks, it took me a while to figure out exactly how to do it, but that's the solution. Thanks. For those interested, the entity service `delete` method accepts a 2nd options argument which includes the optimistic/pessimistic flag. – Aviad P. Jun 30 '19 at 00:17
  • 1
    for those coming later, the relevent overload looks like this: `delete(key: number | string, options?: EntityActionOptions)` and options looks like this: https://ngrx.io/api/data/EntityActionOptions – BlackICE Feb 24 '21 at 23:16