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

Add documentation for prepare_query callback #3184

Merged
merged 2 commits into from
Dec 11, 2019

Conversation

Manzanit0
Copy link
Contributor

Simply added an example use case, to make it clearer.

end

def prepare_query(op, query, opts), do: {query, opts}
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR but I am not sure if this is a good example. :) The fields from Ecto.Query are available only to adapter implementations and therefore are between public and private APIs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@josevalim how about something like this:

## Examples

Let's say you want to filter out records that were "soft-deleted" (have `deleted_at` column set)
from all operations unless an admin is running the query, you can define the callback like this:

    @impl true
    def prepare_query(_operation, query, opts) do
      if opts[:admin] do
        {query, opts}
      else
        query = from(x in query, where: is_nil(x.deleted_at))
        {query, opts}
      end
    end

And then execute the query:

    Repo.all(query)              # only non-deleted records are returned
    Repo.all(query, admin: true) # all records are returned

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated it on the branch :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL. Cheers! :)

@josevalim
Copy link
Member

josevalim commented Dec 11, 2019 via email

@wojtekmach wojtekmach merged commit b8ce772 into elixir-ecto:master Dec 11, 2019
@wojtekmach
Copy link
Member

Thanks!

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

Successfully merging this pull request may close these issues.

3 participants