# `Alternera.Resource.UI.Host`
[🔗](https://github.com/10e-ab/alternera-sdk/blob/v0.9.0/alternera_resource_ui/lib/alternera/resource/ui/host.ex#L1)

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.

# `ref`

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

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

# `broadcast`

```elixir
@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`

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

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

# `delete`

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

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

# `field_errors`

```elixir
@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`

```elixir
@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`

```elixir
@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
50) and `:offset`.

# `restore`

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

Restores the record a version holds.

# `update`

```elixir
@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`

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

A record's versions, newest first.

# `impl`

```elixir
@spec impl() :: module()
```

The configured host module.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
