API Reference alternera_resource_sdk v#0.9.0

Copy Markdown View Source

Modules

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.

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.

On every create and update: a belongs_to that names a record of another tenant is refused with an error on that field.

Tells the host that a record changed, once its transaction has committed, so the proxy's lookup cache drops it on every node (Alternera.Resource.Host.changed/3). Only a resource with cache on carries it (Alternera.Resource.Transformers.AddCacheChange).

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.

Stamps a version row with the id of the actor that made the change (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.

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).

Whether the host serves resources now (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.

The bypass every resource's policies start with: the system actor and a super admin, as the host decides (Alternera.Resource.Host.system_actor?/1).

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 (Alternera.Resource.Host.role_allows?/3).

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).

Lists each resource's version resource in a package's domain, so that ash.codegen writes the versions tables and Ash finds the versions.

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.

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).

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.

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.

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).

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).

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).

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.

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).

What a resource package needs from the proxy at run time, as a behaviour.

Reads a resource's alternera_resource section and its identity.

Runs a resource package's migrations into its own Postgres schema.

The AshPostgres repo every resource package's resources live on.

The database side of a resource package's own tests, with no proxy around.

The contract test kit for a resource package, run with no proxy around.

The host a package's own tests run against, with no proxy around.

Builds a valid record of any resource, for the contract kit and for tests.

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.

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.

Names the records a resource refers to by their lookup key outside the proxy, never by their internal id.

Keeps every reference between records inside one tenant, in the database.

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.

The contract a resource of a resource package must keep, one module per rule (data-model.md § Validation rules)

Rule 8: cache is false, true or [ttl_ms: n] with a positive integer n.

Rule 2: AshPostgres on Alternera.Resource.Repo, in the package's schema resources_<package id>.

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.

Rule 6: the list fields are public attributes, at least one.

Rule 4: a lookup key names an attribute that an identity on it alone makes unique; multitenancy makes the identity per tenant.

Rule 1: attribute multitenancy on tenant_id, not global.

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.

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.

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).

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.

Mixed into each resource's version resource (paper_trail do mixin …).