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

# Queries

> A query reads a consistent version and commits nothing, so a client can run it once or watch it.

A query reads one version of the world and commits nothing. A client can run
it once with `client.query` or watch it with `client.watch` and `useWatch`.
The host records what the query read and runs it again when that changes.

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

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

export const latest = query({ allow: [role.anyone] }, (ctx) =>
  ctx.list(notes.by("written").descending().first(20)),
);

export const get = query({ allow: [role.anyone], args: { note: t.key(notes) } }, (ctx, { note }) =>
  ctx.get(notes(note)),
);
```

## Reads

A query's `ctx` has the five reads, `open(sealed)`, `call` and `refuse`.
Opening through a [declared release](https://docs.bijection.com/access/disclosure.md) requires a mutation.

| Read | Answers |
| - | - |
| `ctx.get(point)` | The fact, or `null` |
| `ctx.has(point)` | Whether the fact is present. Presence may carry a lower label than the fact, and a field change does not wake it. |
| `ctx.list(selection.first(n))` | Every fact of a bounded selection |
| `ctx.page(selection, { size, cursor })` | One window, `{ facts, next }` |
| `ctx.count(selection)` | The exact number of facts |

Addresses say what to read: `notes(key)` is a point, and
`notes.where({ author }).by("written").descending()` is a selection. An index
must serve every selection; see [Reading data](https://docs.bijection.com/database/reading.md) and
[Indexes](https://docs.bijection.com/database/indexes.md). A query that writes, submits or performs a grave
act does not type-check: the sentence names `mutation(…)` as the fix.

## Paging

A query that pages takes `{ size, cursor }` and returns `ctx.page(…)`:

```ts bijection/notes.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
export const pages = query(
  { allow: [role.anyone], args: { size: t.number, cursor: t.cursor } },
  (ctx, { size, cursor }) => ctx.page(notes.by("written").descending(), { size, cursor }),
);
```

`t.cursor` admits `null`, the first page. `next` is the cursor of the
following page, or `null` after the last. Pass the opaque `next` back to the
same selection and order. An ordinary index cursor marks a position in
that order. Each invocation chooses the version it reads, so rerunning an
earlier page can move the boundary for the pages that follow.
React's [`usePages`](https://docs.bijection.com/clients/react) holds such a query's pages.

## Labels and exposure

`allow` exposes the query at its tree path; without it the query is internal
and only `ctx.call` reaches it. `under` is the label it answers under: it reads
facts at or below that label, and its caller's frame must clear it. A query
open only to `role.anyone` or `role.signedIn` clears only the public label.
If its `under` requires more clearance, entry refuses `Disallowed` with
reason `NotCleared`. See [Functions](https://docs.bijection.com/functions#spec-keys) and
[Labels](https://docs.bijection.com/access/labels.md).

## Other worlds and instants

A query reads the head at the present by default, or at `ctx.frame.at` when
the call names a valid instant.
A client picks another world or instant with `client.in(world)` and
`client.at(instant)`; inside a function, `ctx.call(query, args, { in, at })`
and the address methods `.in(world)` and `.at(instant)` do the same. A watch
in a branch or at a version is one read; the head is watched live.

## Run a query from a client

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
const listed = await client.query(app.notes.latest);
if (listed.ok) render(listed.value);
```

`client.query` answers a [`Result`](https://docs.bijection.com/functions/errors). In React,
`useWatch(app.notes.latest)` is `undefined` while loading and then the
latest `Result`, kept current. See [JavaScript clients](https://docs.bijection.com/clients/javascript.md)
and [React](https://docs.bijection.com/clients/react).


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