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

# Write a mountable app

> Package an app as a function from its installer's choices to app(...), or a library as a function returning a subtree.

## Start an app package

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
bijection create comments --template app --package-name @acme/comments
```

The starter's default export is a function from the installer's choices to
`app(...)`:

```ts src/app.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { app } from "@bijectionhq/bijection";
import type { AnyRole } from "@bijectionhq/bijection";
import { commentFunctions } from "./comments.js";

export type CommentsOptions = {
  /** The roles that may add and read comments. */
  readonly allow: readonly [AnyRole, ...AnyRole[]];
  readonly prefix?: string;
  readonly maxLength?: number;
};

export default function comments({ allow, prefix = "", maxLength = 2000 }: CommentsOptions) {
  return app(commentFunctions(allow, { prefix, maxLength }));
}
```

`commentFunctions` declares the tables and functions afresh on each call, so
each mount has its own:

```ts src/comments.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { mutation, query, t, table } from "@bijectionhq/bijection";
import type { AnyRole } from "@bijectionhq/bijection";

export function commentFunctions(
  allow: readonly [AnyRole, ...AnyRole[]],
  terms: { prefix: string; maxLength: number },
) {
  const comments = table(
    { text: t.string, author: t.principal, target: t.string, at: t.instant },
    { index: [["target", "at"]] },
  );

  const list = query({ allow, args: { target: t.string } }, (ctx, { target }) =>
    ctx.list(comments.where({ target }).by("at").descending().first(100)),
  );

  const add = mutation(
    { allow, args: { target: t.string, text: t.string }, refuses: { CommentTooLong: null } },
    async (ctx, { target, text }) => {
      if (text.length > terms.maxLength) return ctx.refuse("CommentTooLong", "The comment is too long.");
      return await ctx.insert(comments, {
        text: terms.prefix + text,
        author: ctx.frame.as.id,
        target,
        at: ctx.frame.now.entry,
      });
    },
  );

  return { comments, list, add };
}
```

An app's contract is its function's parameters. Take roles, labels and
functions as arguments rather than declaring the installer's choices yourself.
Give `allow` only to what the installer's callers should reach; everything
else stays internal to the mount.

## Start a library package

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
bijection create counters --template library --package-name @acme/counters
```

A library is a module, or a function returning a subtree that the including
app places in its tree. Its tables and functions take their paths there. A
helper that acts takes `ctx` first and uses the `ctx` verbs; no library adds a
verb to `ctx`.

## Test on the real kernel

Mount the app in a root app and run it on the test kernel:

```ts src/comments.test.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { api } from "@bijectionhq/bijection/client";
import { expect, it } from "vitest";
import { app, role } from "@bijectionhq/bijection";
import { kernel } from "@bijectionhq/bijection/test";
import comments from "./app.js";

it("adds and lists comments", async () => {
  const root = app({ blog: comments({ allow: [role.signedIn], prefix: "> " }) });
  const k = kernel(root, { now: Temporal.Instant.from("2026-01-01T00:00:00Z") });
  const alice = k.as("person:alice");
  await alice.mutate(api(root).blog.add, { target: "post-1", text: "first" });
  const listed = await alice.query(api(root).blog.list, { target: "post-1" });
  expect(listed.ok && listed.value.map((c) => c.text)).toEqual(["> first"]);
  expect(await k.as("anonymous:test").mutate(api(root).blog.add, { target: "post-1", text: "hi" }))
    .toMatchObject({ ok: false, code: "Disallowed" });
});
```

See [Testing](https://docs.bijection.com/development/testing/test-library.md). When you publish a new
version, its installers review the change as a `bijection.lock` diff.


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