ghidoz / angular2-jsonapi

A lightweight Angular 2 adapter for JSON API
200 stars 123 forks source link

filtering #270

Open lbertidrwolf opened 4 years ago

lbertidrwolf commented 4 years ago

Hello, on my BackEnd I am using elide.io, I didn't find a way to apply elide filters with findAll.For example if I want to filter using filter[book]=genre=='Science Fiction';title==The* What should be in this case the second argument of the findAll method of JsonApiDatastore? using a json like

filter: {
          genre: 'Science Fiction',
          title:'The*'
        },

doesn't work and if I pass a string like 'filter[book]=genre=='Science Fiction';title==The*' this will not appear in the url as query param.

The only way I found to have it working is to override the toQueryString method in the DatastoreConfig

const config: DatastoreConfig = {
  baseUrl: '/jsonapi',
  models: {
    ....
  },
  overrides:
  {
    toQueryString: (params: any) => params ? params.toString() : ''
  }
}
hpawe01 commented 3 years ago

The recommended way for a filter url is described in a recommendation of the JSON:API spec. So in your example it would generate a query like: ?filter[genre]=Science Fiction&filter[title]=The* (if you request something like findAll(Book, ...)).

But if your server implements another filtering strategy and expects a query like ?filter[book]=genre=='Science Fiction';title==The* (url encoded it would be ?filter%5Dbook%5D=genre%3D%3D%27Science%20Fiction%27%3Btitle%3D%3DThe* by the way), you can try the following filter config:

findAll(Book, {
  filter: {
    book: 'genre==\'Science Fiction\';title==The*',
  },
})