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

# Writing

> The six writes: insert, replace, patch and delete assume a fact's state; put and retract are blind.

A mutation writes with six verbs. Each one states what it assumes about the
fact, and what it leaves.

| Verb | Assumes | Leaves | Refuses |
| - | - | - | - |
| `ctx.insert(table \| point, fact)` | The fact is absent | The whole fact; returns the key | `Exists` |
| `ctx.replace(point, fact)` | The fact is present | The whole fact | `Absent` |
| `ctx.patch(point, fields)` | The fact is present | The named fields changed | `Absent` |
| `ctx.delete(point)` | The fact is present | Nothing | `Absent` |
| `ctx.put(point, fact)` | Nothing | The whole fact | |
| `ctx.retract(point)` | Nothing | Nothing | |

A query has no write verb: a write in a query is a type error.

## Strict writes

`insert`, `replace`, `patch` and `delete` are **strict**: each assumes the
fact's state, and the assumption is a read. A false assumption refuses the
whole transaction with `Exists` or `Absent`, and nothing it wrote commits.

```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"]] });

/** Add a note; answers its key. */
export const add = mutation({ allow: [role.signedIn], args: { text: t.string } }, (ctx, { text }) =>
  ctx.insert(notes, { text, written: ctx.frame.now.entry }),
);

/** Change a note's text. A deleted note refuses `Absent`. */
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 });
  },
);

/** Remove a note. Its history stays readable for the table's `history`. */
export const deleteNote = mutation({ allow: [role.signedIn], args: { note: t.key(notes) } }, async (ctx, { note }) => {
  await ctx.delete(notes(note));
});
```

On a table that mints keys, `ctx.insert(table, fact)` returns the new key. On
a table with a declared `key`, insert at the point: `ctx.insert(prices(sku), { amount })`. An insert also refuses `Exists` when a `unique` field set is
taken.

## Blind writes

`put` and `retract` read nothing and never refuse. Readers see them exactly:
a blind write conflicts with a read only when it changes what that read
returned or counted. `ctx.put(point, fact)`
makes the fact be this, whatever was there; `ctx.retract(point)` makes the
point hold nothing, and does nothing when it already holds nothing.

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
await ctx.put(stock({ warehouse, sku }), { onHand });
await ctx.retract(stock({ warehouse, sku }));
```

On a table that mints its keys, `ctx.retract` also takes a `unique` field
set, `ctx.retract(users({ email }))`. The host removes the fact holding it
without reading it into the transaction: neither its fields nor whether one
was there reach your code, and erasing that fact later never closes what
the transaction wrote. `ctx.put` still takes only the key: to change the
fact a unique field set names, `ctx.patch` or `ctx.replace` it.

A blind write may go above its function's `under`, unless the table declares
a constraint. `unique`, a capacity, a `pin` quota and a `t.shared` field are
reads, so a write into such a table stays at or below `under`
([Constraints are reads](https://docs.bijection.com/database/tables.md#constraints-are-reads)). A
`lifetime` is not a constraint: a blind write into a lifetime table expires
`lifetime` after its own commit.

Use a blind write when the new state does not depend on the old one, such as
a value copied from a source of truth. Use a strict write when the change only
makes sense against the fact you expect.

## Writes at valid time

On a series or timeline, a write names its valid time in the address.

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
await ctx.put(readings(meter).at(instant), { kwh });
await ctx.put(tariffs(sku).over({ from: nov1, to: null }), { amount });
await ctx.retract(tariffs(sku).over({ from: nov10, to: nov20 }));
```

See [Series and timelines](https://docs.bijection.com/database/series-and-timelines.md).

## Refusing a write yourself

A mutation refuses with `ctx.refuse(code, message)`, with one of its declared
`refuses` codes. The refusal leaves the mutation: nothing it wrote commits,
even if its own code catches it. A caller that entered it with `ctx.try` gets
the refusal as a `Result` and can still commit
([calls](https://docs.bijection.com/functions/errors.md#call-a-function-and-go-on)).

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
export const reserve = mutation(
  { allow: [role.signedIn], args: { warehouse: t.string, sku: t.string, quantity: t.number }, refuses: { OutOfStock: null } },
  async (ctx, { warehouse, sku, quantity }) => {
    const fact = await ctx.get(stock({ warehouse, sku }));
    if (fact === null || fact.onHand < quantity) return ctx.refuse("OutOfStock", "Not enough stock in this warehouse.");
    await ctx.patch(stock({ warehouse, sku }), { onHand: fact.onHand - quantity });
  },
);
```

See [Errors](https://docs.bijection.com/functions/errors.md) for `Result` and the host codes.

## What a write may touch

* A write into a labelled table needs a function whose `under` flows to the
  table's label. See [Write rules](https://docs.bijection.com/access/labels.md).
* The `sys` tables are read-only to the six writes: grants, tasks, branches,
  pins, disclosures and erasures change only through their grave acts. See
  [System tables](https://docs.bijection.com/database/system-tables.md).
* A [guard](https://docs.bijection.com/access/guards.md) may refuse a write it covers. A refused write
  refuses the transaction with `Forbidden`.

## Transactions

Every write in a mutation commits together, or none does. See
[Transactions and conflicts](https://docs.bijection.com/database/transactions-and-conflicts.md).

## Operational data fixes

Declare a mutation with `allow: [operator]` and include it in `bijection.lock`.
The table's `writers` applies to the mutation. Operators call it with
`bijection mutate`. See [CLI verbs](https://docs.bijection.com/development/cli/verbs.md#change-data).


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