Aurora.Ctx.Core (Aurora.Ctx v0.1.2)
View SourceCore implementation of common database operations using Ecto.
Provides functions for:
- CRUD operations (create, read, update, delete)
- Pagination (list_paginated, navigation)
- Query operations (count, list)
- Record management (change, new)
Summary
Functions
Creates a changeset for the given entity.
Creates a changeset for the given entity using a specific changeset function.
Counts total records matching query conditions.
Creates a new record.
Creates a new record with optional custom changeset function.
Creates a new record, raising on errors.
Creates a new record, raising on errors.
Deletes the given entity.
Deletes the given entity, raising on errors.
Excludes query clauses from an Ecto query.
Gets a record by id.
Gets a record by id, raising if not found.
Lists all records for a schema with optional filtering and sorting.
Lists records with pagination support.
Initializes a new schema struct with optional attributes or options.
Initializes a new struct with optional preloads.
Moves to next page in paginated results.
Moves to previous page in paginated results.
Changes to a specific page in paginated results.
Updates an entity with given attributes.
Updates an entity using a specific changeset function.
Functions
@spec change(Ecto.Schema.t() | Ecto.Changeset.t(), map() | nil) :: Ecto.Changeset.t()
Creates a changeset for the given entity.
Parameters:
entity_or_changeset (Ecto.Schema.t() | Ecto.Changeset.t()) - Entity for changeset
- attrs (map) - Changeset attributes
Returns Ecto.Changeset.
@spec change(Ecto.Schema.t() | Ecto.Changeset.t(), atom(), map() | nil) :: Ecto.Changeset.t()
Creates a changeset for the given entity using a specific changeset function.
Parameters
entity_or_changeset (Ecto.Schema.t() | Ecto.Changeset.t()) - Existing record or changeset
- changeset_function (atom()) - Custom changeset function
- attrs (map()) - Changeset attributes
Returns
- Ecto.Changeset.t()
Examples
alias MyApp.Product
product = %Product{name: "Widget"}
Core.change(product, %{name: "New Widget"})
#=> #Ecto.Changeset<changes: %{name: "New Widget"}>
Core.change(product, :custom_changeset, %{status: "active"})
#=> #Ecto.Changeset<changes: %{status: "active"}>
@spec count(module(), module(), keyword()) :: non_neg_integer()
Counts total records matching query conditions.
Parameters:
- repo_module (module) - Ecto.Repo to use
- schema_module (module) - Schema to query
- opts (keyword) - Optional query filtering options
Returns total count of matching records.
@spec create(module(), module() | Ecto.Changeset.t(), map() | nil) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
Creates a new record.
Parameters:
- repo_module (module()) - Ecto.Repo to use
schema_module (module() | ) - Schema to create
- attrs (map()) - Attributes for the new record
Returns {:ok, schema} on success, {:error, changeset} on failure.
@spec create(module(), module() | Ecto.Changeset.t(), atom(), map() | nil) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
Creates a new record with optional custom changeset function.
Parameters
- repo_module (module()) - Ecto.Repo module to use
schema_module (module() | Ecto.Changeset.t()) - Schema module for the record
- changeset_function (atom()) - Custom changeset function to use
- attrs (map()) - Attributes for the new record
Returns
{:ok, Ecto.Schema.t()}
- On success{:error, Ecto.Changeset.t()}
- On validation failure
Examples
alias MyApp.{Repo, Product}
Core.create(Repo, Product, %{name: "Widget"})
#=> {:ok, %Product{name: "Widget"}}
Core.create(Repo, Product, :custom_changeset, %{
name: "Widget",
status: "active"
})
#=> {:ok, %Product{name: "Widget", status: "active"}}
@spec create!(module(), module() | Ecto.Changeset.t(), map() | nil) :: Ecto.Schema.t()
Creates a new record, raising on errors.
Parameters:
- repo_module (module()) - Ecto.Repo to use
schema_module_or_changeset (module() | Ecto.Changeset.t()) - Schema to create
- attrs (map()) - Attributes for the new record
Returns created schema or raises on error.
@spec create!(module(), module() | Ecto.Changeset.t(), atom(), map() | nil) :: Ecto.Schema.t()
Creates a new record, raising on errors.
Parameters:
- repo_module (module) - Ecto.Repo to use
schema_module_or_changeset (module | Ecto.Changeset) - Schema to create
- changeset_function (atom) - Changeset function to use
- attrs (map) - Attributes for the new record
Returns created schema or raises on error.
@spec delete(module(), Ecto.Schema.t()) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
Deletes the given entity.
Parameters:
- repo_module (module): The Ecto.Repo module to use
- entity (Ecto.Schema.t()): The entity to delete
Returns: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
@spec delete!(module(), Ecto.Schema.t()) :: Ecto.Schema.t()
Deletes the given entity, raising on errors.
Parameters:
- repo_module (module): The Ecto.Repo module to use
- entity (Ecto.Schema.t()): The entity to delete
Returns: Ecto.Schema.t() Raises: Ecto.StaleEntryError
@spec exclude_clauses(Ecto.Query.t(), atom() | [atom()]) :: Ecto.Query.t()
Excludes query clauses from an Ecto query.
Parameters:
- query (Ecto.Query.t()) - Query to modify
clauses (atom | [atom]) - Clause(s) to exclude
Returns modified query.
@spec get(module(), module(), term(), keyword()) :: Ecto.Schema.t() | nil
Gets a record by id.
Parameters:
- repo_module (module) - Ecto.Repo to use
- schema_module (module) - Schema to query
- id (term) - Primary key to look up
- opts (keyword) - Optional query parameters
Returns found schema or nil if not found.
@spec get!(module(), module(), term(), keyword()) :: Ecto.Schema.t()
Gets a record by id, raising if not found.
Parameters:
- repo_module (module) - Ecto.Repo to use
- schema_module (module) - Schema to query
- id (term) - Primary key to look up
- opts (keyword) - Optional query parameters
Returns found schema or raises Ecto.NoResultsError.
Lists all records for a schema with optional filtering and sorting.
Parameters
- repo_module (module()) - Ecto.Repo module to use
- schema_module (module()) - Schema module to query
- opts (keyword()) - Optional query parameters:
:where
- Filter conditions:order_by
- Sort specifications:preload
- Associations to preload:limit
- Maximum number of records
Returns
- list(Ecto.Schema.t())
Examples
alias MyApp.{Repo, Product}
Core.list(Repo, Product)
#=> [%Product{}, ...]
Core.list(Repo, Product,
where: [status: :active],
order_by: [desc: :inserted_at],
preload: [:category]
)
#=> [%Product{category: %Category{}}, ...]
@spec list_paginated(module(), module(), keyword()) :: Aurora.Ctx.Pagination.t()
Lists records with pagination support.
Parameters
- repo_module (module()) - Ecto.Repo module to use
- schema_module (module()) - Schema module to query
- opts (keyword()) - Query options including:
:paginate
- Pagination settings (%{page: integer, per_page: integer}):where
- Filter conditions:order_by
- Sort specifications:preload
- Associations to preload
Configuration
Default pagination settings can be configured in your config.exs
:
config :aurora_ctx, :pagination,
page: 1,
per_page: 40
If not configured, defaults to page: 1, per_page: 40.
Returns
- Aurora.Ctx.Pagination.t()
Examples
alias MyApp.{Repo, Product}
Core.list_paginated(Repo, Product,
paginate: %{page: 1, per_page: 20},
where: [category_id: 1],
order_by: [desc: :inserted_at]
)
#=> %Aurora.Ctx.Pagination{
#=> entries: [%Product{}],
#=> page: 1,
#=> per_page: 20,
#=> entries_count: 50,
#=> pages_count: 3
#=> }
@spec new(module(), module(), map() | keyword()) :: Ecto.Schema.t()
Initializes a new schema struct with optional attributes or options.
Parameters
- repo_module (module) - Ecto.Repo to use
- schema_module (module) - Schema to initialize
- attrs_or_opts - One of:
- map: Initial attributes (will use empty options)
- keyword list: Options (will use empty attributes)
Returns initialized schema struct.
@spec new(module(), module(), map(), keyword()) :: Ecto.Schema.t()
Initializes a new struct with optional preloads.
Parameters:
- repo_module (module): The Ecto.Repo module to use
- schema_module (module): The Ecto.Schema module to initialize
- opts (keyword): Optional preload parameters
Returns: Ecto.Schema.t()
@spec next_page(Aurora.Ctx.Pagination.t() | map()) :: Aurora.Ctx.Pagination.t()
Moves to next page in paginated results.
Parameters:
- paginate (Pagination.t()) - Current pagination state
Returns updated Pagination struct with next page entries.
@spec previous_page(Aurora.Ctx.Pagination.t() | map()) :: Aurora.Ctx.Pagination.t()
Moves to previous page in paginated results.
Parameters:
- paginate (Pagination.t()) - Current pagination state
Returns updated Pagination struct with previous page entries.
@spec to_page(Aurora.Ctx.Pagination.t() | map(), integer()) :: Aurora.Ctx.Pagination.t()
Changes to a specific page in paginated results.
Parameters:
- paginate (Pagination.t()) - Current pagination state
- page (integer) - Target page number
Returns updated Pagination struct with new page entries.
@spec update(module(), Ecto.Schema.t() | Ecto.Changeset.t(), map() | nil) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
Updates an entity with given attributes.
Parameters:
- repo_module (module) - Ecto.Repo to use
entity_or_changeset (Ecto.Schema.t() | Ecto.Changeset.t()) - Entity to update or changeset to apply
- attrs (map) - Update attributes
Returns {:ok, schema} on success, {:error, changeset} on failure.
@spec update(module(), Ecto.Schema.t() | Ecto.Changeset.t(), atom(), map() | nil) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
Updates an entity using a specific changeset function.
Parameters
- repo_module (module()) - Ecto.Repo module to use
entity_or_changeset (Ecto.Schema.t() | Ecto.Changeset.t()) - Existing record to update
- changeset_function (atom()) - Custom changeset function
- attrs (map()) - Update attributes
Returns
{:ok, Ecto.Schema.t()}
- On success{:error, Ecto.Changeset.t()}
- On validation failure
Examples
alias MyApp.{Repo, Product}
product = %Product{name: "Old Name"}
Core.update(Repo, product, %{name: "New Name"})
#=> {:ok, %Product{name: "New Name"}}