> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bijection.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Mutations

> A mutation is one transaction under one label: its writes, submits and grave acts commit together or not at all.

A mutation runs as one transaction under one label, its `under`. Its writes,
its submits and its grave acts commit together, or the transaction refuses and
nothing happens.

```ts bijection/notes.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { mutation, role, t, table } from "@bijectionhq/bijection";

export const notes = table({ text: t.string, written: t.instant }, { index: [["written"]] });

export const add = mutation(
  { allow: [role.signedIn], args: { text: t.string }, refuses: { NoteText: null } },
  async (ctx, { text }) => {
    if (!text.trim()) return ctx.refuse("NoteText", "Enter a note.");
    return await ctx.insert(notes, { text, written: ctx.frame.now.entry });
  },
);

export const rename = mutation(
  { allow: [role.signedIn], args: { note: t.key(notes), text: t.string } },
  async (ctx, { note, text }) => {
    await ctx.patch(notes(note), { text });
  },
);
```

`rename` needs no existence check of its own: `patch` assumes the note is
present, and a deleted note refuses the transaction with the host code
`Absent`.

## Writes

Four writes are strict: each assumes something, and its assumption is part of
the transaction's read set. Two are blind: they read nothing and never
conflict.

| Write | Assumes | Answers or refuses |
| - | - | - |
| `ctx.insert(table, fact)` | the key is absent (the host mints it) | the key |
| `ctx.insert(table(key), fact)` | the declared key is absent | refuses `Exists` |
| `ctx.replace(point, fact)` | the fact is present | refuses `Absent` |
| `ctx.patch(point, fields)` | the fact is present | refuses `Absent` |
| `ctx.delete(point)` | the fact is present | refuses `Absent` |
| `ctx.put(point, fact)` | nothing: make the fact be this | never refuses |
| `ctx.retract(point)` | nothing: make the point hold nothing | never refuses |

A series or timeline fact is written at a timed point:
`ctx.put(prices(sku).over({ from, to }), fact)`. See
[Writing data](https://docs.bijection.com/database/writing.md). The `sys` tables are read-only to all six
writes; only the [grave acts](https://docs.bijection.com/functions/grave-acts.md) change them.

## Effects and grave acts

A mutation's `ctx` also has:

* `ctx.submit(target, args, { once, then, due, within })`, which commits intent
  toward a [target](https://docs.bijection.com/functions/effects.md) atomically with the writes;
* the [grave acts](https://docs.bijection.com/functions/grave-acts.md): `grant`, `revoke`, `open(sealed, { release })`,
  `erase`, `branch`, `land`, `discard`, `pin`, `unpin`, `answer` and
  `withdraw`.

Nothing outside the transaction happens inside it. A target's work starts
after the commit, as a task.

## Conflicts and retries

Every read, and every strict write's assumption, joins the read set. The host
checks the read set at commit and runs the mutation again when something it
read changed. A change hidden above the mutation's label never aborts it. See
[Transactions and conflicts](https://docs.bijection.com/database/transactions-and-conflicts.md).

The transaction must commit by `ctx.frame.now.entry + ctx.frame.within`; past that it
refuses `Expired`.

## Run a mutation from a client

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
const added = await client.mutate(app.notes.add, { text }, { once: requestId });
if (!added.ok) show(added.message);
```

`client.mutate` answers a [`Result`](https://docs.bijection.com/functions/errors). A committed result
carries the commit `version`; pass it as `seen` to a later read to read your
own write. `once` is the call's identity: the same `once` and args return the
original result, and the same `once` with other args refuses `Invalid`. When a
reply is lost the result is `Unknown`. Retry with the original arguments and
the refusal's `once`. With React's `useMutation`, pass `{ once }` as the
second argument to the callable.

## Reactions

A mutation with `on` is a reaction: the host runs it for each firing of a
standing source (a clock, a route or a change log), holding the `roles` it
declares. See [Reactions](https://docs.bijection.com/functions/reactions.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.