Ecto.Repo.prepare_query

You're seeing just the callback prepare_query, go back to Ecto.Repo module for more information.
Link to this callback

prepare_query(operation, query, opts)

View Source (optional)

Specs

prepare_query(operation, query :: Ecto.Query.t(), opts :: Keyword.t()) ::
  {Ecto.Query.t(), Keyword.t()}
when operation: :all | :update_all | :delete_all | :stream | :insert_all

A user customizable callback invoked for query-based operations.

This callback can be used to further modify the query and options before it is transformed and sent to the database.

This callback is invoked for all query APIs, including the stream functions. It is also invoked for insert_all if a source query is given. It is not invoked for any of the other schema functions.

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

The callback will be invoked for all queries, including queries made from associations and preloads. It is not invoked for each individual join inside a query.