Alternera.Resource.UI.Host behaviour (alternera_resource_ui v0.9.0)

Copy Markdown View Source

What a resource package's UI needs from the proxy at run time, as a behaviour: the page's own way of reading and writing records (Alternera.Resource.UI.Records calls it), and messages between a package's components.

A package compiles before the proxy and cannot name its modules. The proxy's implementation is Alternera.Resources.UIHost, set with config :alternera_resource_ui, :host, …; a package's own tests run against Alternera.Resource.UI.Test.Host, which calls Ash directly.

Every callback takes the component's Alternera.Resource.UI.Context and a resource module, and reads or writes with the context's tenant and actor, so the resource's policies decide. Every {:error, _} is logged before it is returned.

Summary

Types

A record, by its id or by its lookup key's value.

Callbacks

Sends message to the components subscribed to topic, on every node. The topic is already scoped to the package and tenant (Alternera.Resource.UI.broadcast/3).

Creates a record through the resource's primary create action.

Deletes a record; refused while other records refer to it.

An error's messages, %{field, message}, field nil for a record-wide one.

A record by id or lookup key; {:error, :not_found} without one.

A page of records: {:ok, %{records, count}}. Options: :search (a text the simple search looks for in :search_in, a list of attribute names, by default the resource's list fields), :filter (attribute to value), :sort ([{attribute, :asc | :desc}]), :limit (at most 200, default

Restores the record a version holds.

Updates a record through the resource's primary update action.

A record's versions, newest first.

Functions

The configured host module.

Types

ref()

@type ref() :: {:id, term()} | {:key, term()}

A record, by its id or by its lookup key's value.

Callbacks

broadcast(topic, message)

@callback broadcast(topic :: String.t(), message :: term()) :: :ok

Sends message to the components subscribed to topic, on every node. The topic is already scoped to the package and tenant (Alternera.Resource.UI.broadcast/3).

create(t, module, map)

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

Creates a record through the resource's primary create action.

delete(t, module, struct)

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

Deletes a record; refused while other records refer to it.

field_errors(term)

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

An error's messages, %{field, message}, field nil for a record-wide one.

get(t, module, ref)

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

A record by id or lookup key; {:error, :not_found} without one.

list(t, module, keyword)

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

A page of records: {:ok, %{records, count}}. Options: :search (a text the simple search looks for in :search_in, a list of attribute names, by default the resource's list fields), :filter (attribute to value), :sort ([{attribute, :asc | :desc}]), :limit (at most 200, default

  1. and :offset.

restore(t, module, struct)

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

Restores the record a version holds.

update(t, module, struct, map)

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

Updates a record through the resource's primary update action.

versions(t, module, term)

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

A record's versions, newest first.

Functions

impl()

@spec impl() :: module()

The configured host module.