Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] Add ability to combine filters into OR arrangement #1417

Closed
IgnusG opened this issue Jan 7, 2022 · 5 comments · Fixed by #1598
Closed

[FEAT] Add ability to combine filters into OR arrangement #1417

IgnusG opened this issue Jan 7, 2022 · 5 comments · Fixed by #1598
Assignees
Labels
enhancement New feature or request

Comments

@IgnusG
Copy link
Contributor

IgnusG commented Jan 7, 2022

Is 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.

@IgnusG IgnusG added the enhancement New feature or request label Jan 7, 2022
@omeraplak
Copy link
Member

Hey @IgnusG ,
Definitely a missing feature in this Refine. We are now focused on headless. Headless will be released shortly. Then let's see what we can do.

@salihozdemir
Copy link
Contributor

Hey @IgnusG,
As you said, I'm trying to support the "or" operator, but I'm stuck while defining the type the CrudFilter. I explained what I want to do in more detail on here. I would be glad if you can help with this.

@IgnusG
Copy link
Contributor Author

IgnusG commented Mar 1, 2022

Hey @IgnusG,

As you said, I'm trying to support the "or" operator, but I'm stuck while defining the type the CrudFilter. I explained what I want to do in more detail on here. I would be glad if you can help with this.

Hi @salihozdemir,

You can use discriminated unions for this. What you do is you define an extra OrFilter with something like:

type OtherFilter = {
  field: string;
  operator: Exclude<Operators, "or">;
  value: any;
}

type OrFilter = {
  // we omit the field property here
  operator: "or";
  value: Filter[];
}

And then you use unions to define the Filter as follows:

type Filter = OrFilter | OtherFilter;

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 the field property. If it's anything else it knows it must be an OtherFilter 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! 👍

@salihozdemir
Copy link
Contributor

salihozdemir commented Mar 1, 2022

@IgnusG 🥇
You saved my day man, thank you so much for your detailed explanation 🥳

@omeraplak
Copy link
Member

Hey @IgnusG ,
We've released 3.6.0! Thank you for your help! Can you help us add the or operator for Airtable?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants