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

# Database

> Declare tables, series and timelines, address their facts, and read and write them in transactions.

An app's data lives in **tables** it declares in its [app tree](https://docs.bijection.com/mountable-apps).
A declaration's name is its path in the tree, so a table needs no string name.

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

/** One fact per order; the host mints each key. */
export const orders = table(
  { customer: t.string, total: t.decimal, placed: t.instant },
  { index: [["customer", "placed"]] },
);

/** A customer's 20 latest orders, newest first. */
export const recent = query({ allow: [role.signedIn], args: { customer: t.string } }, (ctx, { customer }) =>
  ctx.list(orders.where({ customer }).by("placed").descending().first(20)),
);

/** Place an order; answers its key. */
export const place = mutation(
  { allow: [role.signedIn], args: { customer: t.string, total: t.decimal } },
  (ctx, { customer, total }) => ctx.insert(orders, { customer, total, placed: ctx.frame.now.entry }),
);
```

```ts bijection/app.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { app } from "@bijectionhq/bijection";
import * as orders from "./orders";

// The table is `orders.orders`; the functions are `orders.recent` and `orders.place`.
export default app({ orders });
```

## Three kinds of table

| Declaration | Holds | Example |
| - | - | - |
| `table(fields, options)` | One fact per key | Orders, customers |
| `series(fields, options)` | One sample per key and instant | Meter readings |
| `timeline(fields, options)` | One fact per key and valid period | Prices in force from one date to another |

## The verbs

Every read and write names an **address**: a point (`orders(key)`) or a
selection (`orders.where({ customer })`). The verb says what happens; the
address says where.

| Reads | Writes (in a mutation) |
| - | - |
| `ctx.get(point)` | `ctx.insert(table \| point, fact)`, assumes absent |
| `ctx.has(point)` | `ctx.replace(point, fact)`, assumes present |
| `ctx.list(selection.first(n))` | `ctx.patch(point, fields)`, assumes present |
| `ctx.page(selection, { size, cursor })` | `ctx.delete(point)`, assumes present |
| `ctx.count(selection)` | `ctx.put(point, fact)`, `ctx.retract(point)`, blind |

## In this section

* [Tables](https://docs.bijection.com/database/tables.md): declarations, keys and options.
* [Value types](https://docs.bijection.com/database/types.md): `t.*`, `Temporal` and `Decimal`.
* [Addresses](https://docs.bijection.com/database/addresses.md): points, selections, ranked selections and change logs.
* [Reading](https://docs.bijection.com/database/reading.md) and [Writing](https://docs.bijection.com/database/writing.md): the five reads and six writes.
* [Indexes](https://docs.bijection.com/database/indexes.md): declared orders, matched by structure.
* [Protected tables](https://docs.bijection.com/database/protected-tables.md): what a labelled, erasable or expiring table answers, and what the store sees of it.
* [Transactions and conflicts](https://docs.bijection.com/database/transactions-and-conflicts.md): what a mutation relies on.
* [Series and timelines](https://docs.bijection.com/database/series-and-timelines.md): valid time.
* [History and lifetime](https://docs.bijection.com/database/history-and-lifetime.md): versions, pins, lifetimes and erasable facts.
* [Table capabilities](https://docs.bijection.com/database/capabilities.md): presence, totals, state machines, marks, writers and private tables.
* [Family tables](https://docs.bijection.com/database/family-tables.md): one partition per family member.
* [System tables](https://docs.bijection.com/database/system-tables.md): the host's own facts, through `sys`.

Labels decide who may know a fact: see [Access](https://docs.bijection.com/access).


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