If you are a Medium member, please read the story there: https://medium.com/@zsolt.deak/ngrx-vs-rest-66a442e8c5b1
NgRx entered maintenance mode a year ago, but people still use it, it has more than 14k downloads a week, so more users than any of your/my libs at least. It has a long standing major inconsistency though, it fails to do its single responsibility right, by the book.
What problem does (did) ngrx/data solve?
NgRx Data is an extension that offers a gentle introduction to NgRx by simplifying management of entity data while reducing the amount of explicitness.
Meaning, as long as you have a REST API you just define an entity with the URL and the interface that represents the API contract, and you have a CRUD implemented for you with Store and also caching. No Actions, Reducers, Effects and even gives you some automatically provided selectors, such as error$ or loading$ . As long as you don’t want to customise anything, it’s a pretty good deal. When you try and customise things you’ll soon face lack of community knowledge and will do lots of digging in the source code. You know, fun.
REST API and CRUD resource modifications
By the HTTP standard:
In a PUT request, the enclosed entity is considered to be a modified version of the resource stored on the origin server, and the client is requesting that the stored version be replaced. With PATCH, however, the enclosed entity contains a set of instructions describing how a resource currently residing on the origin server should be modified to produce a new version.
In short, PUT is for replace a full entity, PATCH is for editing one, very straightforward and has been in the last decade.
What does ngrx/data do?
They have update , upsert , add for updating, creating/updating, and creating entities in the backend. upsert and add use T, meaning the whole entity needs to be passed as parameter, update on the other hand looks like this:
update(update: Update<T>, options?: HttpOptions): Observable<T> {
const id = update && update.id;
const updateOrError =
id == null
? new Error(`No "${this.entityName}" update data or id`)
: update.changes;
return this.execute(
'PUT',
this.entityUrl + id,
updateOrError,
null,
options
);
}
Update<T> is basically Partial<T> , so we can either replace the whole data at the id , or modify it. It also uses a hardcoded PUT method.
Maintainer’s response
It has been brought to the maintainer’s attention 4 years ago. They shrugged it off very fast, without much consideration.
So then if you want a HTTP compliant REST API, you’ll need to subclass, and use the extension points of ngrx/data, if you still consider using this library.
It’s really ironic how its only purpose is to do a CRUD for you with as little boilerplate as possible, and fails to comply with the standards, forcing you to add boilerplate.
Comments