Skip to content

Commit

Permalink
Merge pull request #285 from aesmail/filter-array-fields
Browse files Browse the repository at this point in the history
Support filtering {:array, :string} fields.
  • Loading branch information
aesmail authored Sep 3, 2023
2 parents 09fb1da + 3a24a1c commit fc4c39c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/kaffy/resource_query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ defmodule Kaffy.ResourceQuery do
filtered_fields = Enum.filter(params, fn {k, v} -> k in schema_fields and v != "" end)

Enum.map(filtered_fields, fn {name, value} ->
%{name: name, value: value}
f = String.to_existing_atom(name)
field_type = Kaffy.ResourceSchema.field_type(resource[:schema], f)
%{name: name, value: value, type: field_type}
end)
end

Expand Down Expand Up @@ -167,7 +169,7 @@ defmodule Kaffy.ResourceQuery do

defp build_list_query(schema, _composite_key, key_pairs) do
Enum.reduce(key_pairs, schema, fn pair, query_acc ->
from query_acc, or_where: ^pair
from(query_acc, or_where: ^pair)
end)
end

Expand All @@ -181,7 +183,14 @@ defmodule Kaffy.ResourceQuery do

false ->
field_name = String.to_existing_atom(filter.name)
from(s in query, where: field(s, ^field_name) == ^filter.value)

case filter.type do
{:array, :string} ->
from(s in query, where: ^filter.value in field(s, ^field_name))

_ ->
from(s in query, where: field(s, ^field_name) == ^filter.value)
end
end

build_filtered_fields_query(query, rest)
Expand Down
1 change: 1 addition & 0 deletions lib/kaffy/resource_schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ defmodule Kaffy.ResourceSchema do
fields_to_be_removed(schema) ++
primary_keys(schema) ++
[:inserted_at, :updated_at]

Keyword.drop(fields(schema), to_be_removed)
end

Expand Down

0 comments on commit fc4c39c

Please sign in to comment.