Conditional append over a context: admit only if nothing relevant changed since read.
A resource that declares the context_boundary section is backed by an append-only
fact table whose rows carry a monotonic seq, a type, and a tags array. The two
operations compose into a dynamic consistency boundary:
{:ok, %{facts: facts, cutoff: cutoff}} =
AshOnetime.Boundary.read(resource, {:tag, "cart:42"}, tenant: tenant)
# ... decide on those facts inside the caller's own transaction ...
AshOnetime.Boundary.append_if(resource, {:tag, "cart:42"}, cutoff, facts_to_append,
tenant: tenant
)append_if/5 runs inside the caller's open PostgreSQL transaction. It takes a
transaction-scoped advisory lock for every tag the query names and every tag the
appended facts carry — sorted, keyed by tenant and tag — then checks that no fact
matching the query has a seq above cutoff, and only then appends. A changed
context answers {:error, {:context_changed, max_seq}}; the caller re-reads and
decides again.
Because every matching fact carries a named tag (queries are tag-anchored; see
AshOnetime.Boundary.Query's validation), writers of one context serialize on those
tags, their facts commit in seq order, and writers with no tag in common never
block each other.
Every call requires :tenant and fails closed without one; lock keys include it.
Use AshOnetime.Boundary.Change to place the same append on a create action, and see
the Context boundaries guide for worked examples.
Summary
Types
The cutoff a read returns: the highest matching seq the caller saw.
A fact row as read or appended: its seq, type, tags, and insert timestamp.
An input fact to append: type plus the tags it carries.
Functions
Appends facts only when no fact matching the query changed the context past cutoff.
Reads the facts a query matches, newest first, with the context cutoff.
Types
@type cutoff() :: non_neg_integer()
The cutoff a read returns: the highest matching seq the caller saw.
@type fact() :: %{ seq: pos_integer(), type: binary(), tags: [binary()], inserted_at: DateTime.t() }
A fact row as read or appended: its seq, type, tags, and insert timestamp.
An input fact to append: type plus the tags it carries.
Functions
@spec append_if( Ash.Resource.t(), AshOnetime.Boundary.Query.expr(), cutoff(), [fact_input()], keyword() ) :: {:ok, [fact()]} | {:error, {:context_changed, pos_integer()}} | {:error, AshOnetime.Error.t()}
Appends facts only when no fact matching the query changed the context past cutoff.
Runs inside the caller's open PostgreSQL READ COMMITTED transaction — it never
starts one. For every tag the query names and every tag the appended facts carry,
a transaction-scoped advisory lock is taken in sorted order (keyed by tenant and
tag), so concurrent writers of one context serialize while writers with no tag in
common proceed. When a matching fact has committed above the cutoff the append is
refused with {:error, {:context_changed, max_seq}} — re-read and decide again.
On success returns the appended facts with their assigned seq values.
:tenant is required; lock keys include it.
@spec read(Ash.Resource.t(), AshOnetime.Boundary.Query.expr(), keyword()) :: {:ok, %{facts: [fact()], cutoff: cutoff()}} | {:error, AshOnetime.Error.t()}
Reads the facts a query matches, newest first, with the context cutoff.
The cutoff is the highest seq among ALL facts the query matches (not just the
returned page), or 0 for an empty context. Pass it back to append_if/5.
:tenant is required. :limit bounds the returned page (default 256,
ceiling 1000); the cutoff always spans every matching fact.