EctoContext.Query (EctoContext v0.1.0)

Copy Markdown View Source

Query helpers used by the EEx templates in priv/templates/ecto_context/.

These functions are called at runtime from code generated by EctoContext. They handle optional query composition — preloading, ordering, limiting, selecting, and filtering by association.

Summary

Functions

Filters a query by the foreign key of the given association.

Conditionally applies a limit clause. No-op when nil.

Conditionally applies an order_by clause. No-op when nil.

Conditionally preloads associations. No-op when nil.

Conditionally applies a 1-arity query function. No-op when nil.

Conditionally applies a select clause. No-op when nil.

Resolves the foreign key field for an association.

Functions

assoc_where(query, module, assoc_atom, parent_id)

@spec assoc_where(Ecto.Queryable.t(), module(), atom(), term()) :: Ecto.Query.t()

Filters a query by the foreign key of the given association.

Resolves the foreign key via resolve_foreign_key/2, then applies a where clause.

maybe_limit(query, limit_num)

@spec maybe_limit(Ecto.Queryable.t(), nil | pos_integer()) :: Ecto.Query.t()

Conditionally applies a limit clause. No-op when nil.

maybe_order_by(query, order_by_clauses)

@spec maybe_order_by(Ecto.Queryable.t(), nil | term()) :: Ecto.Query.t()

Conditionally applies an order_by clause. No-op when nil.

maybe_preload(query, associations)

@spec maybe_preload(Ecto.Queryable.t(), nil | term()) :: Ecto.Query.t()

Conditionally preloads associations. No-op when nil.

maybe_query(queryable, fun)

@spec maybe_query(
  Ecto.Queryable.t(),
  nil | (Ecto.Queryable.t() -> Ecto.Queryable.t())
) ::
  Ecto.Queryable.t()

Conditionally applies a 1-arity query function. No-op when nil.

maybe_select(query, fields)

@spec maybe_select(Ecto.Queryable.t(), nil | [atom()]) :: Ecto.Query.t()

Conditionally applies a select clause. No-op when nil.

resolve_foreign_key(module, assoc_atom)

@spec resolve_foreign_key(module(), atom()) :: atom()

Resolves the foreign key field for an association.

Tries Ecto schema reflection first, falls back to :assoc_id convention. This allows list_for to work with both regular belongs_to and static_belongs_to.