Ectomancer.Repo (Ectomancer v1.2.1)

Copy Markdown View Source

CRUD operations for Ecto schemas exposed via Ectomancer.

This module provides the actual database operations for the auto-generated CRUD tools. It handles:

  • Listing records with filters and pagination
  • Getting single records by primary key
  • Creating records via Ecto changesets
  • Updating records via Ecto changesets
  • Deleting records

Configuration

The Repo module is automatically detected from your application config:

config :ectomancer, :repo, MyApp.Repo

If not configured, it defaults to trying MyApp.Repo based on your app's module namespace.

Summary

Functions

Detects the Repo module based on the application name.

Gets a single record by primary key.

Lists records with optional filters.

Gets the configured Repo module.

Restores a soft-deleted record by setting its soft-delete field to nil.

Updates an existing record.

Validates dynamic include requests against allowed preloadable associations.

Functions

create(schema_module, params, opts \\ [])

Creates a new record.

Parameters

  • schema_module - The Ecto schema module
  • params - Map of attributes

Examples

create(MyApp.Accounts.User, %{"email" => "test@example.com", "name" => "Test"})

destroy(schema_module, params, opts \\ [])

Deletes a record.

Parameters

  • schema_module - The Ecto schema module
  • params - Map containing the primary key value

Examples

destroy(MyApp.Accounts.User, %{"id" => 123})

detect_repo()

@spec detect_repo() :: module() | nil

Detects the Repo module based on the application name.

get(schema_module, params, opts \\ [])

@spec get(module(), map(), keyword()) :: {:ok, struct() | nil} | {:error, any()}

Gets a single record by primary key.

Parameters

  • schema_module - The Ecto schema module
  • params - Map containing the primary key value
  • opts - Options including :preload for eager-loading associations

Examples

get(MyApp.Accounts.User, %{"id" => 123})
get(MyApp.Accounts.User, %{"id" => 123}, preload: [:posts, :comments])

list(schema_module, params \\ %{}, opts \\ [])

@spec list(module(), map(), keyword()) :: {:ok, [struct()]} | {:error, any()}

Lists records with optional filters.

Parameters

  • schema_module - The Ecto schema module
  • params - Map of filter parameters (optional)
  • opts - Options including pagination

Examples

list(MyApp.Accounts.User, %{"email" => "test@example.com"}, limit: 10)

repo()

@spec repo() :: module() | nil

Gets the configured Repo module.

Returns the repo from config or attempts to detect it from the application name.

restore(schema_module, params, opts \\ [])

Restores a soft-deleted record by setting its soft-delete field to nil.

Parameters

  • schema_module - The Ecto schema module
  • params - Map containing the primary key value

Examples

restore(MyApp.Accounts.User, %{"id" => 123})

update(schema_module, params, opts \\ [])

Updates an existing record.

Parameters

  • schema_module - The Ecto schema module
  • params - Map containing primary key and updated attributes

Examples

update(MyApp.Accounts.User, %{"id" => 123, "name" => "New Name"})

validate_includes(include, allowed, opts)

@spec validate_includes(list() | nil, :all | [atom()], keyword()) :: keyword()

Validates dynamic include requests against allowed preloadable associations.

Returns the opts keyword list with merged preloads.