angular2-jsonapi icon indicating copy to clipboard operation
angular2-jsonapi copied to clipboard

filtering

Open lbertidrwolf opened this issue 5 years ago • 1 comments

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() : ''
  }
}

lbertidrwolf avatar Jul 03 '20 14:07 lbertidrwolf

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*',
  },
})

hpawe01 avatar Jan 21 '21 10:01 hpawe01