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

# Documents

> Collaborative notes and templates, and reports rendered from them on your own renderer.

> [!NOTE]
>
> These examples describe the current Bijection CLI and SDK source. Use builds
> from the same source commit for these APIs. The public CLI and npm SDK
> are published independently and may lag this reference.

The documents library, `@bijectionhq/library-documents`, keeps
collaboratively authored documents for the app that places it: notes and
templates, their collaborative text and history, comments, locked embeds,
published templates, and the reports rendered from them. `documents({...})`
returns a subtree. Place it in the app tree and its tables and functions
take their paths there: `docs.documents.create`, `docs.tables.reports`.

```ts app.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { app, label, role, secret } from "@bijectionhq/bijection";
import { documents } from "@bijectionhq/library-documents";

export const workspace = label();
export const editor = role({ clear: [workspace] });
export const viewer = role({ clear: [workspace] });
export const rendererKey = secret();   // bijection secret set rendererKey
export const uploadKey = secret();     // bijection secret set uploadKey

export default app({
  workspace, editor, viewer, rendererKey, uploadKey,
  docs: documents({
    label: workspace,
    editors: [editor],
    readers: [viewer],
    roles: [editor],
    renderer: { endpoint: "https://renderer.internal.example.com", bearer: rendererKey, upload: uploadKey },
  }),
});
```

| Option | Meaning |
| - | - |
| `label` | The label of every fact and every function. Authored content never leaves it except as a render request to your renderer. |
| `editors` | The roles that write documents. |
| `readers` | The roles that read and comment (editors included). |
| `publishers` | The roles that publish templates and endorse versions (editors by default). |
| `releasers` | The roles that generate and release reports (publishers by default). |
| `roles` | The roles the hourly sweep and the renderer's upload route run with. |
| `templates` | Templates kept in source, declared with `noteTemplate`. |
| `capture` | Adapters that capture what each embed shows when a report is generated. |
| `renderer` | Optional: the renderer that turns reports into files. |
| `publish` | Optional: releasing reports to a lower audience through a declared release. |

A client reaches the functions as `api<typeof App>().docs.<group>.<function>`.
Every function refuses with the codes in `DOCUMENTS_REFUSES`
(`Unavailable`, `NotAuthor`, `InvalidRequest`, `InvalidInput`,
`StateConflict`, `BoundExceeded`, `HistoryUnavailable`, `NotSignedIn`).
A call whose version moved since it was read refuses the host code
`Conflict`.
Arguments that may be absent are `null`. Listings take `{ size, cursor }`
and answer `{ facts, next }`, so `usePages` reads them.

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { api } from "@bijectionhq/bijection/client";
import type App from "./app";

const result = await client.mutate(api<typeof App>().docs.documents.create, {
  space: "project-42", title: "Launch plan", kind: "note", sharing: "shared", content: null,
});
```

## The model

* **Spaces.** Documents are grouped under a `space` key the app chooses,
  such as a project or a notebook. Listings and bounds are per space, up to
  10,000 documents.
* **Text.** A ProseMirror document in a closed node set, with Tiptap names.
  Editors submit steps at a version (`text.submitSteps`); a client behind
  the newest version is told to rebase. Snapshots (`text.submitSnapshot`)
  and server edits (`text.append`) are checked against the node set and its
  900 KiB bound. The library keeps the latest 100 autosave revisions and
  every named one.
* **Private documents** answer their author only, and only the author
  shares or removes them.
* **Comments** are anchored to a selection and follow later edits, or
  become orphaned when the range is replaced. Replies are one level deep.
* **Locked embeds.** Your own function reads what an embed shows and calls
  `docs.helpers.lockEmbed(ctx, { space, doc, embed, payload })` in the same
  transaction, so a report captures the locked value, not a live one.
* **Templates** publish immutable versions with typed inputs
  (`templates.publish`). `docs.helpers.instantiate(ctx, { space, source,
  inputs, resolved })` creates a document from one.

| Group | Functions |
| - | - |
| `documents` | `create`, `rename`, `share`, `remove`, `list`, `get`, `forObject` |
| `text` | `getSnapshot`, `latestVersion`, `getSteps`, `submitSteps`, `submitSnapshot`, `append` |
| `revisions` | `list`, `get`, `name`, `endorse`, `restore` |
| `comments` | `list`, `add`, `resolve`, `remove` |
| `embeds` | `snapshots`, `setSnapshots`, `unlock` |
| `templates` | `publish`, `version`, `installed` |
| `reports` | `get`, `list`, `generate`, and `release` with `publish` |
| `published` | `list`, with `publish` |

Your own functions use the in-transaction helpers under `docs.helpers`:
`createDocument`, `lockEmbed`, `instantiate`, `generate`, `release`,
`current` and `transform`.

## Collaborative editing

`@bijectionhq/library-documents/sync` binds a Tiptap editor to the `text`
functions over the client:

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { api } from "@bijectionhq/bijection/client";
import { loadTiptapSync } from "@bijectionhq/library-documents/sync";
import type App from "./app";

const sync = await loadTiptapSync({ client, ...api<typeof App>().docs.text }, { space, doc });
```

## Reports

A report renders a document's current text, or a report your app composed,
into PDF, DOCX, PPTX, CSV or image files. Rendering runs on a renderer you
host, so Bijection runs no rendering capacity. See
[generate your first document](https://docs.bijection.com/files/documents/quickstart.md) and
[generate, continue and release reports](https://docs.bijection.com/files/documents/reports.md).

## Document databases

To mirror collections from MongoDB, DynamoDB, Cosmos DB, Elasticsearch and
other document stores into your app, mount the document source app,
`@bijectionhq/app-documents`: `documents({ data, provider })` with a
receiver bound by one of its binders. See the
[MongoDB](https://docs.bijection.com/integrations/native-mongodb.md) and
[DynamoDB](https://docs.bijection.com/integrations/native-dynamodb.md) guides.


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