Aurora.Ctx (Aurora.Ctx v0.1.2)
View SourceA module for automatically generating CRUD operations for Ecto schemas.
This module provides macros to generate standard database operation functions based on Ecto schemas. It simplifies the creation of context modules by automatically implementing common CRUD operations.
Usage
defmodule MyApp.Accounts do
use Aurora.Ctx
ctx_register_schema(User)
# or with options
ctx_register_schema(User, CustomRepo,
update_changeset: :custom_changeset,
create_changeset: :create_changeset
)
end
Generated Functions
For a schema with source name "users" and module name "user", the following functions are generated:
List Functions
list_users/0
- List all recordslist_users/1
- List all records with optionslist_users_paginated/0
- List records with paginationlist_users_paginated/1
- List records with pagination and optionscount_users/0
- Count total recordscount_users/1
- Count records with options
Pagination Functions
to_users_page/2
- Navigate to a specific pagenext_users_page/1
- Navigate to the next pageprevious_users_page/1
- Navigate to the previous page
Create Functions
create_user/0
- Create a record with empty attributescreate_user/1
- Create a record with given attributescreate_user!/0
- Create a record with empty attributes (raises on error)create_user!/1
- Create a record with given attributes (raises on error)
Get Functions
get_user/1
- Get a record by IDget_user/2
- Get a record by ID with optionsget_user!/1
- Get a record by ID (raises if not found)get_user!/2
- Get a record by ID with options (raises if not found)
Delete Functions
delete_user/1
- Delete a recorddelete_user!/1
- Delete a record (raises on error)
Change Functions
change_user/1
- Create a changeset from a recordchange_user/2
- Create a changeset from a record with attributes
Update Functions
update_user/1
- Update a recordupdate_user/2
- Update a record with attributes
New Functions
new_user/0
- Initialize a new structnew_user/1
- Initialize a new struct with attributesnew_user/2
- Initialize a new struct with attributes and options
Summary
Functions
Registers a schema for CRUD function generation.
Lists all implementable CRUD functions for a given schema.
Functions
Registers a schema for CRUD function generation.
Parameters
schema_module
(module()) - The Ecto schema module to generate functions forrepo
(module() | nil) - (Optional) The Ecto.Repo to use for database operations. Can be set globally with@ctx_repo_module
opts
(keyword()) - (Optional) Configuration options::changeset
(atom()) - A specific function to use for change function changesets (default::changeset
):create_changeset
(atom()) - A specific function to use for creation changesets (defaults to the value of option:changeset
or the function:changeset
):infix
(binary) - The fixed part to use when constructing the functions' names. By default, is the lowercase of the module name (the last part of the full module name).:plural_infix
(binary) - The fixed part to use when constructing the functions' names for plural functions. By default, uses the table name defined in the schema module.:update_changeset
(atom()) - The function to use for changesets (defaults to the value of option:changeset
or the function:changeset
)
Repository Configuration
The Ecto.Repo can be configured in two ways:
- Passing it as the second argument to
ctx_register_schema/3
- Setting the
@ctx_repo_module
module attribute in your context
If neither is specified, it will attempt to use YourApp.Repo
based on your
context module's namespace.
Function Names
Given a User schema with table "users":
# Default naming (no options)
get_user/1 # Singular functions use module name
list_users/0 # Plural functions use table name
# With infix: "customer"
get_customer/1 # Singular functions use custom infix
list_users/0 # Plural functions still use table name
# With plural_infix: "customers"
get_user/1 # Singular functions use module name
list_customers/0 # Plural functions use custom plural infix
# With both infix: "customer" and plural_infix: "customers"
get_customer/1 # Singular functions use custom infix
list_customers/0 # Plural functions use custom plural infix
Examples
# Using explicit repo
ctx_register_schema(User, MyApp.Repo)
# Using module attribute
@ctx_repo_module MyApp.Repo
ctx_register_schema(User)
# Generating custom function names
ctx_register_schema(User, MyApp.Repo,
infix: "customer",
plural_infix: "customers"
)
Lists all implementable CRUD functions for a given schema.
This function is used internally to determine which functions should be generated for a given schema. It returns a list of maps containing function definitions.
Parameters
schema_module
- The Ecto schema module to analyze
Returns
- List of maps containing:
:type
- The function type (:list
,:create
, etc.):name
- The function name:arity
- The function arity