Alternera.Resource.UI.Records (alternera_resource_ui v0.9.0)

Copy Markdown View Source

The records a component shows, read and written the way the Resources page does: through the host (Alternera.Resource.UI.Host), with the context's tenant and actor, so the resource's policies decide, the history records the change, a cached resource drops its key, and every error is logged.

resource defaults to the context's own resource. Calling Ash directly on the package's resources with Alternera.Resource.UI.opts/1 is just as allowed, and reaches what these do not (a custom action, a calculation, an aggregate); log every {:error, _} there yourself.

Summary

Functions

Every record of resource that opts select (list/3's options but limit and offset), read a page at a time, up to max: records (1000 by default): {:ok, %{records, count, truncated?}}, where truncated? says there are more than max and count how many. Show the reader when it is true rather than leave the rest out unsaid.

Creates a record from params (attribute name to value).

Deletes record.

An error as %{field, message}, for showing it on the field it names.

A record by {:id, id} or {:key, lookup_key_value}.

A page of records of resource (the context's own when left out); see Alternera.Resource.UI.Host.list/3 for the options.

Restores the record version holds.

Updates record with params.

The versions of the record with id, newest first.

Functions

all(ctx, resource \\ nil, opts \\ [])

@spec all(Alternera.Resource.UI.Context.t(), module() | nil, keyword()) ::
  {:ok, %{records: list(), count: non_neg_integer(), truncated?: boolean()}}
  | {:error, term()}

Every record of resource that opts select (list/3's options but limit and offset), read a page at a time, up to max: records (1000 by default): {:ok, %{records, count, truncated?}}, where truncated? says there are more than max and count how many. Show the reader when it is true rather than leave the rest out unsaid.

{:ok, %{records: units, truncated?: more?}} = Records.all(ctx, MyPackage.Unit)

create(ctx, resource \\ nil, params)

@spec create(Alternera.Resource.UI.Context.t(), module() | nil, map()) ::
  {:ok, struct()} | {:error, term()}

Creates a record from params (attribute name to value).

delete(ctx, record)

@spec delete(Alternera.Resource.UI.Context.t(), struct()) :: :ok | {:error, term()}

Deletes record.

field_errors(error)

@spec field_errors(term()) :: [%{field: String.t() | nil, message: String.t()}]

An error as %{field, message}, for showing it on the field it names.

get(ctx, resource \\ nil, ref)

@spec get(
  Alternera.Resource.UI.Context.t(),
  module() | nil,
  Alternera.Resource.UI.Host.ref()
) ::
  {:ok, struct()} | {:error, term()}

A record by {:id, id} or {:key, lookup_key_value}.

list(ctx, resource_or_opts \\ [], opts \\ [])

@spec list(Alternera.Resource.UI.Context.t(), module() | keyword(), keyword()) ::
  {:ok, %{records: list(), count: non_neg_integer()}} | {:error, term()}

A page of records of resource (the context's own when left out); see Alternera.Resource.UI.Host.list/3 for the options.

Records.list(ctx, limit: 10)
Records.list(ctx, MyPackage.Currency, search: "kr")

restore(ctx, resource \\ nil, version)

@spec restore(Alternera.Resource.UI.Context.t(), module() | nil, struct()) ::
  {:ok, struct()} | {:error, term()}

Restores the record version holds.

update(ctx, record, params)

@spec update(Alternera.Resource.UI.Context.t(), struct(), map()) ::
  {:ok, struct()} | {:error, term()}

Updates record with params.

versions(ctx, resource \\ nil, id)

@spec versions(Alternera.Resource.UI.Context.t(), module() | nil, term()) ::
  {:ok, list()} | {:error, term()}

The versions of the record with id, newest first.