[FEAT] Add ability to combine filters into OR arrangement
See original GitHub issueIs your feature request related to a problem? Please describe. Would be awesome if it was possible to include a search all input which could technically be coded as several filters set to the same value. The problem is there is no way to tell the data provider how to combine these values (which ones are AND and which ones are OR). In some providers I’ve seen these filters just passed on with this being left up to them, for example strapi I think does an AND and if there are 2 filters with the same field it uses OR for those.
Describe the solution you’d like It would be great if it was possible to include filters in a more complex arrangement that allows for OR constructions being passed to providers while searching, for example:
filter = [
{
operator: "eq",
field: "age",
value: 20
},
{
operator: "or",
value: [
{
field: "title",
operator: "eq",
value: "Test",
},
{
field: "description",
operator: "eq",
value: "Test"
}
]
}
]
Here the query would look like "age" == 20 AND ("title" == "Test" OR "description" == "Test")
This could be adopted gradually by data providers that support it (for example this would be possible in the Airtable Provider) and ignored by providers that don’t.
Additional context
In the example on https://refine.dev/docs/guides-and-concepts/search/table-search/ this is solved with the q
field but this must be available on the database.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (5 by maintainers)
Top GitHub Comments
Hi @salihozdemir,
You can use discriminated unions for this. What you do is you define an extra OrFilter with something like:
And then you use unions to define the Filter as follows:
Typescript can use type narrowing based on the operator after this. If the operator is “or” it knows we’re dealing with an
OrFilter
and it won’t offer thefield
property. If it’s anything else it knows it must be anOtherFilter
with that property.The important part on why this works here is that there is something that both types have (in our case the
operator
property) and this property has a different value in each union (either “or” or the other operators). You can read more about how this works here https://css-tricks.com/typescript-discriminated-unions/Hope this helps! 👍
@IgnusG 🥇 You saved my day man, thank you so much for your detailed explanation 🥳