Aurora.Ctx.QueryBuilder (Aurora.Ctx v0.1.2)

View Source

Provides functions for building and composing Ecto queries with common operations like filtering, sorting, pagination, and preloading associations.

Summary

Functions

Applies a list of query options to an Ecto query.

Functions

options(query, options \\ [])

@spec options(
  Ecto.Query.t() | nil,
  keyword()
) :: Ecto.Query.t()

Applies a list of query options to an Ecto query.

Parameters

  • query (Ecto.Query.t() | nil) - Core query to modify

  • options (keyword) - List of query options to apply

Options

  • :preload (atom | list) - Associations to preload

  • :where (keyword | map | tuple) - Filter conditions

    • Simple equality: {:field, value}
    • Comparison: {:field, operator, value} where operator can be:
      • :greater_than, :gt - Greater than
      • :greater_equal_than, :ge - Greater than or equal
      • :less_than, :lt - Less than
      • :less_equal_than, :le - Less than or equal
      • :equal_to, :eq - Equal to
    • Range: {:field, :between, start_value, end_value}
  • :or_where (keyword | map | tuple) - Same as :where but combines with OR

  • :paginate (map) - Pagination options with keys:
    • :page (integer) - Page number
    • :per_page (integer) - Items per page
  • :sort (atom | tuple | list) - Sorting options:

    • field (atom | {:asc | :desc, field}) - Field to sort by ascending

    • fields ([{:asc | :desc, field}]) - List of sort fields specifications

Returns

  • Ecto.Query.t() - Modified query with all options applied

Examples

query = from(p in Product)
QueryBuilder.options(query,
  where: [status: :active],
  preload: [:category],
  sort: [desc: :inserted_at],
  paginate: %{page: 1, per_page: 20}
)