flop icon indicating copy to clipboard operation
flop copied to clipboard

Introduce op `:===` to respect empty or nil filters

Open vermaxik opened this issue 1 year ago • 3 comments

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.

vermaxik avatar Jun 01 '23 15:06 vermaxik

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.

woylie avatar Jun 11 '23 02:06 woylie

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 😉

vermaxik avatar Aug 03 '23 08:08 vermaxik

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.

woylie avatar Aug 03 '23 10:08 woylie