# alternera_resource_sdk v0.9.0 - API Reference

## Modules

- [Alternera.Resource](Alternera.Resource.md): An Ash resource of a resource package: reference data the proxy hosts, with
a UI, a REST API, MCP tools and pipeline lookups built from the definition.
- [Alternera.Resource.Calculations.ReferenceKey](Alternera.Resource.Calculations.ReferenceKey.md): The lookup key of the record a `belongs_to` names (`EUR` for an exchange
rate's `base_currency`), the calculation `<relationship>_<key>` that
`Transformers.AddReferenceKeys` adds. An expression, so the database
answers it in the same query and the API can filter and sort on it.

- [Alternera.Resource.Changes.CheckTenantReferences](Alternera.Resource.Changes.CheckTenantReferences.md): On every create and update: a `belongs_to` that names a record of another
tenant is refused with an error on that field.
- [Alternera.Resource.Changes.NotifyHost](Alternera.Resource.Changes.NotifyHost.md): Tells the host that a record changed, once its transaction has committed,
so the proxy's lookup cache drops it on every node
(`c:Alternera.Resource.Host.changed/3`). Only a resource with `cache`
on carries it (`Alternera.Resource.Transformers.AddCacheChange`).
- [Alternera.Resource.Changes.RefuseReferencedDelete](Alternera.Resource.Changes.RefuseReferencedDelete.md): On every destroy of a resource-package resource: when the database refuses
the delete because other records reference the record (AshPostgres reports
"would leave records behind"), the error becomes
`Alternera.Resource.Errors.Referenced`, which the REST API answers with
409 and the other surfaces can tell from a validation error.

- [Alternera.Resource.Changes.SetActorId](Alternera.Resource.Changes.SetActorId.md): Stamps a version row with the id of the actor that made the change
(`c:Alternera.Resource.Host.actor_id/1`), on the version resource's
create. AshPaperTrail's `belongs_to_actor` needs the actor's resource
module, which a package cannot name.

- [Alternera.Resource.Changes.SetReferenceByKey](Alternera.Resource.Changes.SetReferenceByKey.md): On a create or update that accepts a `belongs_to`'s attribute: the
argument `<relationship>_<key>` names the record by its lookup key
(`base_currency_code: "EUR"`) instead of its id, and this change sets the
attribute to that record's id, found in the changeset's tenant as its
actor. A key no record has is an error on the argument; `nil` clears the
reference. Without the argument the attribute is left as it is, so the
id still works (`Transformers.AddReferenceKeys`).

- [Alternera.Resource.Checks.Available](Alternera.Resource.Checks.Available.md): Whether the host serves resources now
(`c:Alternera.Resource.Host.available?/0`). Every resource's policies
forbid create, read, update and generic actions while it does not;
destroy is not refused, so records can always be removed. What makes the
host answer `false` is the host's business, not the package's.

- [Alternera.Resource.Checks.System](Alternera.Resource.Checks.System.md): The bypass every resource's policies start with: the system actor and a
super admin, as the host decides (`c:Alternera.Resource.Host.system_actor?/1`).

- [Alternera.Resource.Checks.TenantRole](Alternera.Resource.Checks.TenantRole.md): Whether the actor's role in the tenant allows the action: a read, and a
generic action in the resource's `read_only_actions`, needs any role
there, everything else needs `tenant_admin`, as the host decides
(`c:Alternera.Resource.Host.role_allows?/3`).
- [Alternera.Resource.Domain](Alternera.Resource.Domain.md): The Ash domain of a resource package: one per package, and its id names the
package everywhere (the Postgres schema `resources_<id>`, the resource names
`"<id>.<resource id>"`, the REST paths).
- [Alternera.Resource.DomainExtension](Alternera.Resource.DomainExtension.md): Lists each resource's version resource in a package's domain, so that
`ash.codegen` writes the versions tables and Ash finds the versions.
- [Alternera.Resource.Extension](Alternera.Resource.Extension.md): The `alternera_resource` DSL section: how the proxy presents and exposes a
resource. `use Alternera.Resource` adds the extension; its verifiers
refuse a resource that breaks the contract, naming the resource and the
rule (`Alternera.Resource.Verifiers`).

- [Alternera.Resource.Extension.Action](Alternera.Resource.Extension.Action.md): `action :id, "Label"` in `alternera_resource`'s `ui`: something a user does
to the resource, a record or a selection of records, drawn by the host
wherever it fits (`Alternera.Resource.UI`, kind E). `run` names an
Ash action of the resource; `handler` a module with `handle_action/4`
for work that is not one Ash action.

- [Alternera.Resource.Extension.Argument](Alternera.Resource.Extension.Argument.md): `argument :name, type` inside an `action` with a `handler`: an input the
host asks for in the action's dialog. An action that runs an Ash action
takes that action's own arguments and accepted attributes instead.

- [Alternera.Resource.Extension.Cell](Alternera.Resource.Extension.Cell.md): `cell :field, Module` in `alternera_resource`'s `ui`: a function component
that draws one attribute's value in the table, the tree and a record's
details, in place of the plain text (`Alternera.Resource.UI`, kind A).

- [Alternera.Resource.Extension.Field](Alternera.Resource.Extension.Field.md): `field :attribute, Module` in `alternera_resource`'s `ui`: a function
component drawn as one attribute's control in the record form and an
action's dialog, in place of the one the form picks by type
(`Alternera.Resource.UI`, kind B).

- [Alternera.Resource.Extension.Panel](Alternera.Resource.Extension.Panel.md): `panel :id, "Label", Module` in `alternera_resource`'s `ui`: a LiveComponent
shown as a tab of an opened record beside its history
(`Alternera.Resource.UI`, kind D).

- [Alternera.Resource.Extension.Surface](Alternera.Resource.Extension.Surface.md): A LiveComponent the host mounts in a place of its own
(`Alternera.Resource.UI`, kinds H and I): `page :id, "Label", Module`
in a domain's `alternera_ui`, at `/resources/<package id>/pages/<id>`, and
`workbench_item :id, "Label", Module` in a resource's `ui` or a domain's
`alternera_ui`, as a Workbench tab. `kind` says which.

- [Alternera.Resource.Extension.View](Alternera.Resource.Extension.View.md): `view :id, "Label", Module` in `alternera_resource`'s `ui`: a LiveComponent
offered beside Table and Tree, fed the records the search matches
(`Alternera.Resource.UI`, kind F).

- [Alternera.Resource.Host](Alternera.Resource.Host.md): What a resource package needs from the proxy at run time, as a behaviour.
- [Alternera.Resource.Info](Alternera.Resource.Info.md): Reads a resource's `alternera_resource` section and its identity.
- [Alternera.Resource.Migrations](Alternera.Resource.Migrations.md): Runs a resource package's migrations into its own Postgres schema.
- [Alternera.Resource.Repo](Alternera.Resource.Repo.md): The AshPostgres repo every resource package's resources live on.
- [Alternera.Resource.Test](Alternera.Resource.Test.md): The database side of a resource package's own tests, with no proxy around.
- [Alternera.Resource.Test.Contract](Alternera.Resource.Test.Contract.md): The contract test kit for a resource package, run with no proxy around.
- [Alternera.Resource.Test.Host](Alternera.Resource.Test.Host.md): The host a package's own tests run against, with no proxy around.
- [Alternera.Resource.Test.Samples](Alternera.Resource.Test.Samples.md): Builds a valid record of any resource, for the contract kit and for tests.
- [Alternera.Resource.Transformers.AddCacheChange](Alternera.Resource.Transformers.AddCacheChange.md): For a resource with `cache` on, adds `Alternera.Resource.Changes.NotifyHost`
to every create, update and destroy, so a committed write drops the record
from the proxy's lookup cache on every node. A resource without the cache
reads the database on every lookup and needs no notice.

- [Alternera.Resource.Transformers.AddJsonApi](Alternera.Resource.Transformers.AddJsonApi.md): Writes the resource's `json_api` section from `expose api:`: the type
`"<package id>.<resource id>"`, the base
`/tenants/:tenant_id/resources/<package id>/<resource id>` and one route per
exposed kind on the primary action of its type. With nothing exposed the
section stays empty and the proxy serves no route for the resource.
- [Alternera.Resource.Transformers.AddReferenceKeys](Alternera.Resource.Transformers.AddReferenceKeys.md): Names the records a resource refers to by their lookup key outside the
proxy, never by their internal id.
- [Alternera.Resource.Transformers.AddTenantReferences](Alternera.Resource.Transformers.AddTenantReferences.md): Keeps every reference between records inside one tenant, in the database.
- [Alternera.Resource.Transformers.Verify](Alternera.Resource.Transformers.Verify.md): Runs the contract's rules (`Alternera.Resource.Verifiers`) once every
other transformer has run, and fails the compile with one error naming the
resource and each rule it breaks.

- [Alternera.Resource.Verifiers](Alternera.Resource.Verifiers.md): The contract a resource of a resource package must keep, one module per
rule (`data-model.md` § Validation rules)
- [Alternera.Resource.Verifiers.Cache](Alternera.Resource.Verifiers.Cache.md): Rule 8: `cache` is `false`, `true` or `[ttl_ms: n]` with a positive integer n.

- [Alternera.Resource.Verifiers.DataLayer](Alternera.Resource.Verifiers.DataLayer.md): Rule 2: AshPostgres on `Alternera.Resource.Repo`, in the package's
schema `resources_<package id>`.

- [Alternera.Resource.Verifiers.Expose](Alternera.Resource.Verifiers.Expose.md): Rule 5: every route in `expose api:` has a primary action of its type
(`:index` and `:get` a read, `:post` a create, `:patch` an update,
`:delete` a destroy), every other name in it and every name in
`read_only_actions` is a generic action of the resource (`action :name,
type`), and every action in `expose mcp:` exists.

- [Alternera.Resource.Verifiers.ListFields](Alternera.Resource.Verifiers.ListFields.md): Rule 6: the list fields are public attributes, at least one.
- [Alternera.Resource.Verifiers.LookupKey](Alternera.Resource.Verifiers.LookupKey.md): Rule 4: a lookup key names an attribute that an identity on it alone makes
unique; multitenancy makes the identity per tenant.

- [Alternera.Resource.Verifiers.Multitenancy](Alternera.Resource.Verifiers.Multitenancy.md): Rule 1: attribute multitenancy on `tenant_id`, not global.
- [Alternera.Resource.Verifiers.PaperTrail](Alternera.Resource.Verifiers.PaperTrail.md): Rule 7: the paper trail `use Alternera.Resource` turns on is still on,
in `:snapshot` mode with a version on destroy and the SDK's version mixin,
so every version holds the whole record (a restore needs it) and its actor.

- [Alternera.Resource.Verifiers.Policies](Alternera.Resource.Verifiers.Policies.md): Rule 3: the policy prefix `use Alternera.Resource` writes comes first,
so no policy of the developer's can let an actor past tenant scoping, the
availability or the tenant role.

- [Alternera.Resource.Verifiers.TenantReferences](Alternera.Resource.Verifiers.TenantReferences.md): Rule 9: every reference stays inside the tenant. Each `belongs_to` has an
AshPostgres reference matched on `tenant_id`, and the resource has the
identity `:alternera_tenant_key` on its primary key that such a key points
at (`Alternera.Resource.Transformers.AddTenantReferences` writes both).

- [Alternera.Resource.Verifiers.UI](Alternera.Resource.Verifiers.UI.md): Rule 10: the `ui` section names what exists. A `cell` and a `field` name a
public attribute, each at most once; a panel, an action, a view and a
Workbench item each have an id of their own kind no other has, and no view
is called `:table` or `:tree` (the Resources page's own views); an action
gives exactly one of `run` and `handler`, an action that runs an Ash action
declares no arguments of its own (it takes that action's), and the Ash
action exists with a type that fits what it acts on: an update or a
destroy for a record or a selection, a create or a generic action for the
resource. Only a `handler` action may be `read_only`. `screen` is set once, in `ui` or in the section.
- [Alternera.Resource.VersionMixin](Alternera.Resource.VersionMixin.md): Mixed into each resource's version resource (`paper_trail do mixin …`).

- Exceptions
  - [Alternera.Resource.Errors.Referenced](Alternera.Resource.Errors.Referenced.md): A delete refused because other records still reference the record through
a foreign key (`ON DELETE RESTRICT`). Renders as JSON:API 409 `referenced`.
`Alternera.Resource.Changes.RefuseReferencedDelete` turns AshPostgres's
"would leave records behind" into it.

