reyesoft / ngx-jsonapi

JSON API client library for Angular 5+ 👌 :: Production Ready 🚀
https://ngx-jsonapi.reyesoft.com/
MIT License
101 stars 52 forks source link

Bug: The package sorts all incoming data by "id" automatically. #54

Closed leetharris closed 6 years ago

leetharris commented 6 years ago

As the title states, this package sorts all incoming data by "id" and does not preserve the original order.

This is a problem as we have specific ordering coming out of our backend based on remote "sort" parameters (supported by the JSON-API spec).

pablorsk commented 6 years ago

Library works with collections. Every element on collection has the resource id as key.

Javascript sort collections by key. On my mind, we have only solution: replace collections for arrays. This solution is very aggressive, because we need change a lot of features. Collections are very fast, because we save cache on memory and browser index db; we need only ask if element exists. If we migrate to array, we need on every interaction traverse all data (imagine relations on each resource of a collection).

Another better idea?

leetharris commented 6 years ago

We actually made our own workaround for this which may give you some inspiration on how you could handle it in your package.

We created a middleware that checked all incoming arrays and assigned an array_order attribute to each item in the collection.

Then we sorted our tables by this array_order attribute on each resource. Worked out pretty well!

Thanks for making this package!

OpticNectar commented 6 years ago

@leetharris Could you share your code on this? @pablorsk The order should be based on the server or there should be a sort option. Automatically sorting by ID defeats the purpose of what a lot of this package does, especially since jsonapi itself supports sorting.