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

Introduce op :=== to respect empty or nil filters #338

Open
vermaxik opened this issue Jun 1, 2023 · 3 comments
Open

Introduce op :=== to respect empty or nil filters #338

vermaxik opened this issue Jun 1, 2023 · 3 comments

Comments

@vermaxik
Copy link

vermaxik commented Jun 1, 2023

Is your feature request related to a problem? Please describe.
Currently if you use empty/nil query it ignore the query, I'm totally understand why is it like this:

%{filters: %{field: :name, op: :==, value: ""}} # ignores this filter

Describe the solution you'd like
But sometimes I want to search by exact empty value at column, and have proposal to introduce === op for such cases

%{filters: %{field: :name, op: :===, value: ""}} # does not ignore the filter and have `WHERE name == ""` query

Describe alternatives you've considered
It could any other naming like exact

Additional context
We use flop not only for phoenix tables, but also for filtering in Ecto contexts, and would love to get empty response instead of ignoring such of the filter and return all rows.

@woylie
Copy link
Owner

woylie commented Jun 11, 2023

For nil, [] and %{}, we have the empty and not_empty operators.

Empty strings are not explicitly filtered by the library, but we use Ecto.Changeset.cast/4 to cast the filter values, which regards empty strings as empty values by default, turning them into nil in the filter struct. Would it help you if we were to add an empy_values option that is passed on to cast/4? See https://hexdocs.pm/ecto/Ecto.Changeset.html#cast/4-options.

@vermaxik
Copy link
Author

vermaxik commented Aug 3, 2023

Thank you for your response and consideration of feature request @woylie.

Since we utilize Flop as a generic way to dynamically build Ecto queries, modifying the behavior of schemas and their changesets might introduce unintended consequences. Having an empty_values option passed on to Ecto.Changeset.cast/4 could potentially affect other parts of our application, making it less suitable for our needs.

We believe that introducing a new operator, :===, as proposed in our initial request, would provide the most intuitive and straightforward solution for us. This way, we can explicitly search for exact empty values in a column without interfering with the default behavior of Ecto schemas and changesets. On top of that, we think it will have the best developer experience.

We hope you can reconsider adding the :=== operator to Flop and I will be happy to create the PR 😉

@woylie
Copy link
Owner

woylie commented Aug 3, 2023

A bit has changed since you opened the issue. Filter values are now cast and validated depending on the field type and operator. Empty values are filtered here now. Since HTML forms will always produce empty strings for empty inputs, they are nilified at that place. That's where you'd have to conditionally not do that if the operator is :===. But that also means that this new operator would behave unexpectedly in HTML filter forms (an empty filter input would result in a filter now that cannot be removed). Although in that case, using :== and entering "" in the input would result in "\"\"", which would allow you to filter for empty strings in that context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants