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

# Bulk operations

> Apply a bounded set of changes in one mutation or process it in durable chunks.

Use one mutation for a bounded group that needs one atomic result. For a
change larger than one transaction, keep the selection and progress in a fact
and process one chunk per task.

## Apply a bounded group

`remediate` is a mutation that takes one order's key and a reason. One
enclosing mutation calls it once per order. The calls join the transaction
and see the writes of the calls before them; a refusal in any call refuses
the whole group.

```ts bijection/groups.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { mutation, t } from "@bijectionhq/bijection";
import { remediate } from "./orders";
import { operator } from "./roles";
import { orders } from "./schema";

export const applyGroup = mutation(
  {
    allow: [operator],
    args: { orders: t.array(t.key(orders)), reason: t.string },
    refuses: { GroupSize: null },
  },
  async (ctx, { orders: keys, reason }) => {
    if (keys.length < 1 || keys.length > 16) return ctx.refuse("GroupSize", "Pass 1 to 16 orders.");
    for (const order of keys) await ctx.call(remediate, { order, reason });
  },
);
```

The app chooses 16 as its bound. Choose one that fits what each call reads,
writes and submits within the [transaction limits](https://docs.bijection.com/operate/limits.md#transactions).
Tasks submitted by an accepted group commit with its writes and are
performed separately.

## Call it and read your write

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { api } from "@bijectionhq/bijection/client";
const result = await client.mutate(api().groups.applyGroup, { orders, reason }, { once: batch });
if (!result.ok) throw new Error(result.message);
const summary = await client.query(api().groups.summary, { batch }, { seen: result.version });
```

`once` makes a retry of the same group answer the original result. Pass a
committed result's `version` as `seen` so the next read observes it.

## Process a large selection in chunks

Keep a fact per batch. Each chunk is one mutation that reads a bounded
selection, applies it and submits itself for the next chunk until nothing
is left:

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

export const batches = table({ reason: t.string, chunks: t.number, done: t.boolean });

export const chunk = mutation({ args: { batch: t.key(batches) } }, async (ctx, { batch }) => {
  const fact = await ctx.get(batches(batch));
  if (fact === null || fact.done) return;
  const flagged = await ctx.list(orders.where({ state: "flagged" }).first(50));
  for (const order of flagged) await ctx.patch(orders(order.id), { state: "remediated", reason: fact.reason });
  if (flagged.length < 50) return await ctx.patch(batches(batch), { done: true });
  await ctx.patch(batches(batch), { chunks: fact.chunks + 1 });
  await ctx.submit(chunk, { batch }, { once: ["chunk", batch, fact.chunks + 1] });
});
```

Each chunk commits on its own, and its `once` names exactly one next
chunk. `orders` declares `index: [["state"]]` to serve the selection. To
walk a selection the chunks do not change, use
`ctx.page(selection, { size, cursor })` and keep `next` in the batch fact.


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