Aurora.Uix.Integration.Ctx.Crud (Aurora UIX v0.1.4-rc.7)

Copy Markdown

Context-based implementation of CRUD operations.

Provides wrapper functions that delegate to Context-based function references stored in CrudSpec structures. Enables consistent CRUD interface across different backend implementations by invoking the appropriate function with the provided arguments.

Key Features

  • Delegates CRUD operations to Context function references
  • Supports all operations: list, get, create, update, delete, change, new
  • Pagination integration via Aurora.Ctx.Core
  • Consistent interface matching Ash CRUD operations
  • Compatible with Ecto-based contexts
  • Implements socket_opts/2 as a no-op — Ecto contexts have no concept of an Ash actor, so socket-derived options are not forwarded

Key Constraints

  • Requires valid CrudSpec with function_spec field populated
  • Function arities must match the operation requirements
  • Pagination relies on Aurora.Ctx.Core.to_page/2
  • The trailing opts argument on create, update, delete, change, to_page is accepted to satisfy the Aurora.Uix.Integration.Crud behaviour but is ignored

Summary

Functions

Creates a changeset for updating a resource.

Creates a new resource in the database.

Deletes a resource from the database.

Retrieves a single resource by ID.

Lists resources using the provided CrudSpec function.

Creates a new resource struct with optional preloading.

Returns [] — Ecto contexts have no concept of an Ash actor.

Navigates to a specific page in paginated results.

Updates an existing resource in the database.

Functions

change(crud_spec, entity, form_name, attrs, opts \\ [])

Creates a changeset for updating a resource.

Parameters

  • crud_spec (CrudSpec.t()) - The spec containing the change function reference.
  • entity (struct()) - The resource to create a changeset for.
  • form_name (atom() | binary()) - Unused for the ctx backend.

  • attrs (map()) - Attributes to apply to the changeset.
  • opts (keyword()) - Ignored. Present to satisfy the behaviour contract.

Returns

Ecto.Changeset.t() - An %Ecto.Changeset{} structure.

Examples

iex> crud_spec = %CrudSpec{function_spec: &MyContext.change_user/2}
iex> change(crud_spec, %User{}, "user", %{name: "John"})
%Ecto.Changeset{...}

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

Creates a new resource in the database.

Parameters

  • crud_spec (CrudSpec.t()) - The spec containing the create function reference.
  • params (map()) - Parameters for the new resource.
  • opts (keyword()) - Ignored. Present to satisfy the behaviour contract.

Returns

tuple() - Result tuple, typically {:ok, struct()} or {:error, struct()}.

Examples

iex> crud_spec = %CrudSpec{function_spec: &MyContext.create_user/1}
iex> create(crud_spec, %{name: "Alice", email: "alice@example.com"})
{:ok, %User{name: "Alice"}}

delete(crud_spec, entity, opts \\ [])

Deletes a resource from the database.

Parameters

  • crud_spec (CrudSpec.t()) - The spec containing the delete function reference.
  • entity (struct()) - The resource to delete.
  • opts (keyword()) - Ignored. Present to satisfy the behaviour contract.

Returns

tuple() - Result tuple, typically {:ok, struct()} or {:error, struct()}.

Examples

iex> crud_spec = %CrudSpec{function_spec: &MyContext.delete_user/1}
iex> delete(crud_spec, %User{id: 1})
{:ok, %User{id: 1}}

get(crud_spec, id, opts)

Retrieves a single resource by ID.

Parameters

  • crud_spec (CrudSpec.t()) - The spec containing the get function reference.
  • id (term()) - The resource identifier.
  • opts (keyword()) - Additional options passed to the function.

Returns

struct() | nil - The retrieved resource or nil if not found.

Examples

iex> crud_spec = %CrudSpec{function_spec: &MyContext.get_user/2}
iex> get(crud_spec, 123, [])
%User{id: 123}

list(crud_spec, opts)

Lists resources using the provided CrudSpec function.

Parameters

  • crud_spec (CrudSpec.t()) - The spec containing the list function reference.
  • opts (keyword()) - Query options passed to the function.

Returns

term() - The result from the function invocation (typically a pagination structure).

Examples

iex> crud_spec = %CrudSpec{function_spec: &MyContext.list_users/1}
iex> list(crud_spec, where: [{:active, true}])
%Pagination{entries: [...]}

new(crud_spec, attrs, opts)

Creates a new resource struct with optional preloading.

Parameters

  • crud_spec (CrudSpec.t()) - The spec containing the new function reference.
  • attrs (map()) - Initial attributes for the new resource.
  • opts (keyword()) - Options passed to the function.

Returns

struct() - A new resource struct.

Examples

iex> crud_spec = %CrudSpec{function_spec: &MyContext.new_user/2}
iex> new(crud_spec, %{name: "Jane"}, [])
%User{name: "Jane"}

socket_opts(crud_spec, socket)

Returns [] — Ecto contexts have no concept of an Ash actor.

Present to satisfy the Aurora.Uix.Integration.Crud behaviour. Handlers that call Aurora.Uix.Integration.Crud.apply_socket_opts/2 on a ctx connector get back an empty keyword list and merge nothing into subsequent calls.

Parameters

  • crud_spec (CrudSpec.t()) - Ignored.
  • socket (Phoenix.LiveView.Socket.t() | map()) - Ignored.

Returns

keyword() - Always [].

to_page(crud_spec, pagination, page, opts \\ [])

Navigates to a specific page in paginated results.

Parameters

  • crud_spec (CrudSpec.t()) - The spec (currently unused).
  • pagination (Pagination.t()) - The current pagination structure.
  • page (integer()) - The target page number.
  • opts (keyword()) - Ignored. Present to satisfy the behaviour contract.

Returns

Pagination.t() - Updated %Pagination{} structure with the requested page data.

Examples

iex> to_page(crud_spec, %Pagination{page: 1, pages_count: 5}, 3)
%Pagination{page: 3, entries: [...]}

update(crud_spec, entity, params, opts \\ [])

Updates an existing resource in the database.

Parameters

  • crud_spec (CrudSpec.t()) - The spec containing the update function reference.
  • entity (struct()) - The resource to update.
  • params (map()) - Parameters to update.
  • opts (keyword()) - Ignored. Present to satisfy the behaviour contract.

Returns

tuple() - Result tuple, typically {:ok, struct()} or {:error, struct()}.

Examples

iex> crud_spec = %CrudSpec{function_spec: &MyContext.update_user/2}
iex> update(crud_spec, %User{id: 1}, %{name: "Bob"})
{:ok, %User{id: 1, name: "Bob"}}