Aurora.Ctx
A macro set for automatically generating CRUD operations in Elixir context modules from Ecto schemas. Aurora.Ctx provides:
- Automatic CRUD function generation from Ecto schemas
- Built-in pagination with navigation helpers
- Advanced query building with filters and sorting
- Flexible preloading of associations
- Customizable changeset functions for create/update operations
- Safe and raising versions of operations
- Full Ecto compatibility and query composition
Installation
Available in Hex, the package can be installed
by adding aurora_ctx to your list of dependencies in mix.exs:
def deps do
[
{:aurora_ctx, "~> 0.1.10"}
]
endUsage
Add Aurora.Ctx to your context module and register your schemas:
defmodule MyApp.Inventory do
use Aurora.Ctx
# Basic usage with default repo
ctx_register_schema(Product)
# With custom options
ctx_register_schema(Category, MyCustomRepo,
update_changeset: :custom_changeset,
create_changeset: :creation_changeset
)
endGenerated Functions
For a schema named Product with table name: “products”, several functions are automatically generated,
as shown in the examples:
# List operations with advanced filtering
list_products() # List all products
list_products( # List with filters and sorting
where: [status: :active],
order_by: [desc: :inserted_at],
preload: [:category]
)
count_products() # Count all products
# Pagination with navigation
page = list_products_paginated(
paginate: %{page: 1, per_page: 20}
)
next_page = next_products_page(page)
prev_page = previous_products_page(page)
# Create with custom changeset
create_product(%{name: "Item"}) # Uses default or specified changeset
create_product!(%{name: "Item"}) # Raise on error version
# Read single records
get_product(1, preload: [:category, reviews: [:user]])
get_product!(1) # Raise on error version
get_product_by(reference: "item_001") # Get record by using a filter
# Update with custom changeset
update_product(product, attrs) # Uses default or specified changeset
change_product(product) # Get changeset for forms
# Delete operations
delete_product(product) # Returns {:ok, struct} or {:error, changeset}
delete_product!(product) # Raise on error version
# New operation with preload
new_product(preload: :product_transactions) # Returns a Product schema with product_transactions []
Documentation
For detailed usage examples and API reference, please see:
Want to contribute?
Read the guidelines.