woylie / flop

Filtering, ordering and pagination for Ecto
MIT License
678 stars 36 forks source link

Support compound_fields custom ecto type #517

Open stanleygtrillion opened 4 days ago

stanleygtrillion commented 4 days ago

currently compound_fields automatically hardcode type to :string but in the case of compound_fields of 2 uuid fields, performing filter to this compound_field will fail

e.g.

compound_fields: [
  new_uuid: [
    :uuid1,
    :uuid2,
  ]
],

with op like_or will generate uuid1 like $1 OR uuid2 like $1 this will fail because uuid operation with like throw error at postgres e.g: select * from my_table where id like '<uuid value>';

is it possible to defined the ecto_type on compound_fields so on building the like_or operation it will generate uuid1 = $1 OR uuid2 = $2

woylie commented 4 days ago

I think you'll want to use the :in operator here and pass the list of UUIDs you want to filter on as filter value.

stanleygtrillion commented 3 days ago

:in will not work in this scenario, it will throw this error:

%{
  "filters" => %{
    "0" => %{
      "field" => "new_uuid",
      "op" => "in",
      "value" => ["8ff7ccfd-b896-41e4-97f6-4b4516f57f62"]
    }
  }
}

Invalid Flop: #Ecto.Changeset<action: :replace, changes: %{first: 25, filters: [#Ecto.Changeset<action: :insert, changes: %{value: [\"8ff7ccfd-b896-41e4-97f6-4b4516f57f62\"], op: :in, field: :new_uuid}, errors: [op: {\"is invalid\", [allowed_operators: [:=~, :like, :not_like, :like_and, :like_or, :ilike, :not_ilike, :ilike_and, :ilike_or, :empty, :not_empty]]}], data: #Flop.Filter<>, valid?: false>], order_by: [:inserted_at], order_directions: [:desc]}, errors: [], data: #Flop<>, valid?: false>

this is because :in is not part of the allowed_operators for compound fields

stanleygtrillion commented 1 day ago

@woylie mind taking a look at this PR: https://github.com/woylie/flop/pull/518 i have added test cases for the new proposed :or operator to facilitate this operation do let me know how i can improve it. thank you