Aurora.Ctx (Aurora.Ctx v0.1.9)

View Source

A 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.

Generated Functions

For a schema with source name "users" and module name "user", the following functions are generated:

List Functions

  • list_users/0 - Lists all records
  • list_users/1 - Lists all records with options
  • list_users_paginated/0 - Lists records with pagination
  • list_users_paginated/1 - Lists records with pagination and options
  • count_users/0 - Counts total records
  • count_users/1 - Counts records with options

Pagination Functions

  • to_users_page/2 - Navigates to a specific page
  • next_users_page/1 - Navigates to the next page
  • previous_users_page/1 - Navigates to the previous page

Create Functions

  • create_user/0 - Creates a new record with empty attributes
  • create_user/1 - Creates a new record with given attributes
  • create_user!/0 - Creates a new record with empty attributes (raises on error)
  • create_user!/1 - Creates a new record with given attributes (raises on error)

Get Functions

  • get_user/1 - Gets a record by ID
  • get_user/2 - Gets a record by ID with options
  • get_user!/1 - Gets a record by ID (raises if not found)
  • get_user!/2 - Gets a record by ID with options (raises if not found)

Get_by Functions

  • get_by_user/1 - Gets a record by filtering using where clauses
  • get_by_user/2 - Gets a record by filtering using where clauses and applying query options
  • get_by_user!/1 - Gets a record by filtering using where clauses (raises if not found)
  • get_by_user!/2 - Gets a record by filtering using where clauses and applying query options (raises if not found)

Delete Functions

  • delete_user/1 - Deletes a record
  • delete_user!/1 - Deletes a record (raises on error)

Change Functions

  • change_user/1 - Creates a changeset from a record
  • change_user/2 - Creates a changeset from a record applying given attributes

Update Functions

  • update_user/1 - Updates a record
  • update_user/2 - Updates a record with attributes

New Functions

  • new_user/0 - Initializes a new struct
  • new_user/1 - Initializes a new struct with attributes
  • new_user/2 - Initializes a new struct with attributes and preload option

Usage Exanples

  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

For detailed examples of using the generated functions, see Examples Guide. The generated functions are backed up by the module Aurora.Ctx.Core, see its documentation for detailed information.

Summary

Functions

Registers a schema for CRUD function generation.

Lists all implementable CRUD functions for a given schema.

Functions

ctx_register_schema(schema_module, opts)

(macro)
@spec ctx_register_schema(module(), module() | keyword()) :: Macro.t()

Registers a schema for CRUD function generation.

Parameters

  • schema_module (module()) - The Ecto schema module to generate functions for
  • repo (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() | atom()) - 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() | atom()) - 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:

  1. Passing it as the second argument to ctx_register_schema/3
  2. 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"
)

ctx_register_schema(schema_module, repo \\ nil, opts \\ [])

(macro)
@spec ctx_register_schema(module(), module() | nil, keyword()) :: Macro.t()

implementable_functions(schema_module, opts \\ [])

@spec implementable_functions(
  module(),
  keyword()
) :: list()

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
  • opts - Options that affect function generation:
    • :infix - Custom infix for singular function names
    • :plural_infix - Custom infix for plural function names

Returns

  • List of maps containing:
    • :type - The function type:
      • :list - Functions that return collections of records
      • :list_paginated - Functions that return paginated results
      • :count - Functions that count records
      • :create - Functions that create new records
      • :get - Functions that fetch single records
      • :delete - Functions that remove records
      • :change - Functions that create changesets
      • :update - Functions that modify records
      • :new - Functions that initialize structs
      • :to_page, :next_page, :previous_page - Pagination navigation
    • :name - The generated function name as an atom
    • :arity - The function arity (number of arguments)