Alternera.Resource (alternera_resource_sdk v0.9.0)

Copy Markdown View Source

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.

defmodule Finance.Currency do
  use Alternera.Resource,
    domain: Finance.Resources,
    id: "currency",
    table: "currencies"

  alternera_resource do
    label "Currencies"
    list_fields [:code, :name]
    lookup_key :code
  end

  attributes do
    uuid_primary_key :id
    attribute :code, :string, allow_nil?: false, public?: true
    attribute :name, :string, allow_nil?: false, public?: true
  end

  identities do
    identity :code, [:code]
  end

  actions do
    defaults [:read, :destroy, create: :*, update: :*]
  end
end

use expands to use Ash.Resource with AshPostgres, the policy authorizer and the extensions Alternera.Resource.Extension, AshJsonApi.Resource and AshPaperTrail.Resource, and writes what every resource of a package must have:

  • the table resources_<package id>.<table> on Alternera.Resource.Repo;
  • attribute multitenancy on tenant_id, the attribute and a unique index on (tenant_id, <primary key>). There is no foreign key to the proxy's tenants: the resource repo may be on another database, so the proxy deletes a tenant's records itself;
  • every belongs_to matched on the tenant too, so the database refuses a reference to another tenant's record (Alternera.Resource.Transformers.AddTenantReferences), and a check before each create and update that answers it as an error on the field (Alternera.Resource.Changes.CheckTenantReferences);
  • the paper trail in :snapshot mode with a version on destroy and the actor's id on each version;
  • the policy prefix: the system bypass, availability and the tenant role. Policies of your own are added after it and can only narrow access;
  • a change on every destroy that reports a delete the database refuses for references as Alternera.Resource.Errors.Referenced (409 over REST);
  • with cache on in alternera_resource, a change on every create, update and destroy that tells the host, after the transaction, which record changed (Alternera.Resource.Transformers.AddCacheChange).

Alternera.Resource.Verifiers lists the rules the compile enforces. Other use Ash.Resource options (extensions:, notifiers:, …) pass through.

Summary

Functions

The major of the resource contract this SDK implements.

Whether id is a valid package or resource id.

Functions

sdk_version()

@spec sdk_version() :: pos_integer()

The major of the resource contract this SDK implements.

valid_id?(id)

@spec valid_id?(term()) :: boolean()

Whether id is a valid package or resource id.