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

Does this library support filtering on nested associations tables (joined tables fields)? #10

Closed
acrolink opened this issue Oct 25, 2017 · 12 comments · Fixed by #17
Closed

Comments

@acrolink
Copy link

No description provided.

@valyukov
Copy link
Owner

Sure, look at readme examples.

@acrolink
Copy link
Author

I have tried something like this as outlined in the readme examples:

"comments_body_eq": "body",

But it is not working at all. I am using Phoenix 1.3 and suspect that this might be due to the newly introduced contexts.

@valyukov
Copy link
Owner

Could you please provide a full example or simple test that could help to reproduce issue? Because this example works fine for me.

@valyukov valyukov reopened this Oct 26, 2017
@acrolink
Copy link
Author

I have properties belonging to users. A property has a user_id field referencing it to a user. I tried to do users_name_cont but nothing happens.

@valyukov
Copy link
Owner

Did you expect that ex_sieve detect relation and make join instead of you? It doesn't work that way, you should make explicit join before apply ex_sieve filters.

@ekarak
Copy link

ekarak commented May 12, 2020

Actually, this functionality only works one level deep, and it would be immensely useful to filter on arbitrary nested fields. Right now, one can only do filters like parent_child_field_predicate and cannot do stuff like (grand+)parent_parent_child_field_predicate.
To give an example, in the typical Phoenix demo schema, there are Users that have_many Posts that have_many Comments. I'd like to be able to pass in a filter like
user_post_comment_text_cont=Elias to find all users whose posts have a comment that mention my name. I found that recursing into nested structs is stopping at the 1st descendant in ExSieve.Node.Attribute.get_name/2 here and recursion is possible by replacing get_name by extract:

  #defp get_name(%{related: module}, key), do: get_name(module, key)
  defp get_name(%{related: module}, key), do: extract(key, module)

... however things get messy after this as the dynamic_predicate function in Builder.Where needs to unroll the recursed Attributes returned. I've tried things like:

  # ekarak: recurse on higher-order nestings
  defp dynamic_predicate(pred, %Attribute{parent: parent, name: %Attribute{} = child}, value) do
    IO.inspect("pred=#{pred} parent=#{inspect(parent)} child=#{inspect(child)}", label: "dynamic_predicate -->")
    values = dynamic_predicate(pred, %Attribute{parent: child.parent, name: child.name}, value)
    |> IO.inspect(label: "values")
#    dynamic_predicate(pred, parent, values)
    dynamic([{^parent, p}], field(p, :id) in ^values)
    |> IO.inspect(label: "<-- dynamic_predicate #{inspect(parent)}")
  end

Any help appreciated, especially by @albertored who did changes here recently.

Thanks!

@albertored
Copy link
Collaborator

Hi @ekarak, as soon as I find some free time I will take a look at it

@albertored
Copy link
Collaborator

@ekarak I think I have something working, in the next days I will clean up the code a little bit a push it

@ekarak
Copy link

ekarak commented May 16, 2020

Thats great, thanks @albertored !

@albertored
Copy link
Collaborator

albertored commented May 18, 2020

@ekarak I have just pushed a branch with the new feature, if you want you can give it a try

@ekarak
Copy link

ekarak commented May 18, 2020

thank you @albertored ! Will test asap

@ekarak
Copy link

ekarak commented May 18, 2020

This works beautifully! I want to test it a bit more (mainly to check for edge cases). @valyukov can you take a look as well?

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

Successfully merging a pull request may close this issue.

4 participants