| title | Atomic single-record mutations |
|---|---|
| description | The contract of Table.atomicMutation, which checks one record's revision or one field value and patches or deletes that record in one database mutation. |
| mode | reference |
Table.atomicMutation(id, request) checks one immutable, instance-scoped record identity and one condition inside the database mutation. It returns Query<AtomicMutationOutcome>. It does not provide multi-record transactions, a lock service, receipts, or coordination of external effects.
Generic filter(...).update(...) and filter(...).delete() do not give this guarantee. Call it on a direct schema.instance(instanceId).table(tableId) with one literal string identity. The default instance is supported. CROSS_INSTANCE, filtered selections, expression keys, cursors, and upserts are not.
@antelopejs/interface-database and @antelopejs/interface-database/atomic export the public types. An update checks the current revision and installs the patch and the next revision together. A delete checks the revision inside the deletion.
const outcome = await table
.atomicMutation(recordId, {
type: "update",
revisionField: "revision",
expectedRevision: observed.revision,
nextRevision: freshRevision,
patch: { title: "Updated title" },
})
.run();
const deletion = await table
.atomicMutation(recordId, {
type: "delete",
revisionField: "revision",
expectedRevision: observed.revision,
})
.run();Revision tokens are nonempty strings. Each update must install a changed token that the caller guarantees was never used for that identity. Every writer of protected state must advance the revision. If one does not, a successful comparison does not prove that the observed state is current. Initialize new records with fresh revisions, and use a new identity for each incarnation, including recreation after deletion.
expectedRevision: { kind: "missing" } matches an absent revision field on an existing record. It does not match a stored null, an empty string, or a missing record. An update can use this expectation to add revisions to existing unversioned records atomically. A delete can use it when the domain permits deleting an unversioned record. A backend that cannot tell absence from null must reject this expectation rather than weaken it. In PostgreSQL, declared SQL columns lose this distinction, and an overflow JSON field can keep it. Review how your mapper serializes the record before you choose where to store the revision.
The patch replaces each top-level field it supplies. Nested objects and arrays replace their entire previous values. They do not merge recursively. Unspecified fields remain unchanged, and null stores null rather than removing the field. There is no unset operation.
A patch holds only constant strings, booleans, finite numbers, null, valid Dates, dense arrays, and plain objects. ValidateAtomicMutation rejects undefined, functions, query proxies, cycles, and field names that contain . or start with $. The patch cannot contain id, _id, the revision field, or scope fields that the adapter owns. Adapters must keep their normal Date storage representation, and must not run objects that resemble query stages as expressions.
deleteIfEqual is a narrower condition for domains where equality of one field is enough. An example is deleting an expired record whose observed timestamp has not changed.
const outcome = await table
.atomicMutation(recordId, {
type: "deleteIfEqual",
field: "lastSeenAt",
expectedValue: observed.lastSeenAt,
})
.run();expectedValue accepts a string, a finite number, a boolean, or a valid Date. Equality is exact and type-sensitive, and Dates compare by their stored instant. A scalar must not match an array that contains that scalar. Missing fields never match. Null, objects, arrays, identity fields, and scope fields are not supported.
This variant does not check or advance a revision, and it does not prevent ABA: a value can change and later return to its observed value. The domain must prove that equality alone authorizes the deletion. It does not replace revision checks on state transitions.
| Outcome | Meaning |
|---|---|
applied |
The adapter received definitive acknowledgement that the one mutation applied. |
not-applied |
The adapter received definitive acknowledgement that the target was missing or its scope or condition did not match. |
unknown |
The adapter cannot determine whether the mutation applied, including acknowledgement loss. |
Definitive validation errors and unsupported-operation errors throw. Neither an exception nor unknown means a mismatch. The interface rejects invalid adapter results, including undefined, zero, and generic modified counts, with AtomicMutationUnsupportedError. Adapters must reject unsupported stages explicitly. There is no fallback to generic filtered writes and no separate capability probe. Use an adapter whose implementation passed verification before you rely on this API.
The interface and the adapter must not retry an uncertain mutation on their own. Do not repeat external effects, and do not infer failure from a later mismatch. Domain-specific recovery can reconcile durable evidence and receipts, but a fresh read alone cannot prove whether an earlier operation or external effect occurred. Any deliberate replay requires a domain proof, and it does not turn not-applied into evidence about the original attempt.
The canonical terminal stage is { stage: "atomicMutation", options: undefined, args: [id, request] }, right after schema, instance, and table. AtomicMutation<T> is the union of AtomicUpdate<T>, AtomicDelete<T>, and AtomicDeleteIfEqual<T>.
Adapters can reuse ValidateAtomicMutation(id, request, protectedFields) and ValidateAtomicMutationTable(prefixStages). They must also validate the terminal envelope and the restrictions of their schema. Scope, immutable identity, and the revision or equality condition must take part in the native mutation on the target record, not only in an earlier selection or identity subquery. Backend tests must prove behavior under contention, scope isolation, the distinction between a missing field and null, top-level replacement, and behavior on acknowledgement loss.
The metadata of an adapter's package does not show whether the adapter implements this contract. Check its implementation and its backend tests.