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

# Family tables

> One table and one set of functions for many organizations, each fact in its member's own label.

A **label family** is one label per key: a member per organization, team or
customer. A table labelled with a family puts each fact in the frame's member.
There is no member column: the member is part of every key, index and read,
taken from the frame. This page follows the `teams` starter.

```ts bijection/tenants.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { label, t } from "@bijectionhq/bijection";

/** One label per organization. */
export const tenants = label.family(t.string);
```

A family has no fixed size. Each member gets its own capacity when it is
registered (`ctx.register(tenants)`, see
[System tables](https://docs.bijection.com/database/system-tables.md)), so nothing is reserved for members
that are not registered. Registration mints the member's random, opaque tag.
A founding runs at the public label and registers the member in its own
step. It needs the `register` right for the family from both the caller and
the code: the caller holds it through a role's `register: [tenants]` or a
class grant the app declares
(`app(tree, { classes: { signedIn: { register: [tenants] } } })`), and the
code through the founding mutation's `register: [tenants]` (see
[Found a family member](https://docs.bijection.com/access/grants.md#found-a-family-member)). Set
`{ members: n }` only to cap how many are registered at once, charged at the
public label.

Roles clear every member, and a grant of a role is held under one member
(see [Grants](https://docs.bijection.com/access/grants.md)):

```ts bijection/roles.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { role } from "@bijectionhq/bijection";
import { tenants } from "./tenants";

export const member = role({ clear: [tenants.all] });
export const admin = role({ clear: [tenants.all], include: [member], issue: true });
```

## Register and look up a member

With `keyed: true`, `ctx.register(tenants, { key })` associates a business key
with a random member tag. A fresh registration validates the key and refuses
`Exists` if a live member already holds it. Erasing that member frees the key
for a fresh registration, which mints a new tag.

While its receipt is retained, a retry of the same registration occurrence
returns the original tag, even after erasure or reassignment of the key.
The receipt ignores the supplied business-key value and carries no information
about current membership. This also applies to registrations made without a key.
A new occurrence or an absent receipt follows the fresh registration path.

If a decision depends on who currently holds a business key, base it on
`await ctx.get(sys.members({ family: tenants, key }))` in the live world. A
keyed family declares `presence: "public"`, so whether a key is taken is a
public fact. The lookup returns the live member's fact
or `null` when the key is absent. For a write that depends on the lookup, read it
in the same mutation so the decision carries the read dependency.

## Declare the table

```ts bijection/tasks.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { mutation, query, t, table } from "@bijectionhq/bijection";
import { admin, member } from "./roles";
import { tenants } from "./tenants";

/** A tenant's tasks: each fact is in the frame's member. */
export const tasks = table({ title: t.string, done: t.boolean, by: t.string }, { label: tenants });
```

`label: tenants.and(other)` keeps each fact at its member joined with another
label.

## Write functions under the member

A function with `under: tenants` runs under the caller's member. The caller
chooses it with `client.under(tenants.of(organization))`, and the frame
clears it only through a grant of the member. Every read and write then
addresses the member's partition. Here `organization` is the opaque ID
suffix of the member tag returned by registration or lookup.

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
/** One page of the caller's tenant's tasks. */
export const list = query(
  { under: tenants, allow: [member, admin], args: { size: t.number, cursor: t.cursor } },
  (ctx, { size, cursor }) => ctx.page(tasks, { size, cursor }),
);

/** Add a task to the caller's tenant; answers its key. */
export const add = mutation(
  { under: tenants, allow: [member, admin], args: { title: t.string } },
  (ctx, { title }) => ctx.insert(tasks, { title, done: false, by: ctx.frame.as.id }),
);

/** Mark a task done. Another tenant's key, or a deleted task, refuses `Absent`. */
export const complete = mutation(
  { under: tenants, allow: [member, admin], args: { task: t.key(tasks) } },
  async (ctx, { task }) => {
    await ctx.patch(tasks(task), { done: true });
  },
);
```

## Another member's partition

`table.of(member)` addresses one member's partition instead of the frame's.
A read through it needs a frame that clears that member. A blind write
(`put`, `retract`, or `insert` into a minted table) needs only that the
frame's label flows to the member's, so a lower frame can write up into a
member without reading anything there.

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
await ctx.insert(tasks.of(tenants.of(organization)), { title: "Welcome", done: false, by: ctx.frame.as.id });
```

One member's selection is also a reaction source:
`on: tasks.of(tenants.of(organization)).changes`.

## Membership

`members({ roles, under: tenants })` from `@bijectionhq/library-members` is a
subtree of membership functions; with `under`, the functions that issue roles run
under one member, and `accept` runs at the public label.

```ts bijection/team.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { members } from "@bijectionhq/library-members";
import { admin, member } from "./roles";
import { tenants } from "./tenants";

export const team = members({ roles: [member, admin], under: tenants });
```

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

export default app({ tenants, access, tasks, team });
```

See [Grants](https://docs.bijection.com/access/grants.md) for inviting members and granting the first
owner.

## Erasing a member

`ctx.erase(tenants.of(organization))` destroys the member's unit and makes the
facts and values under its label unreadable in every version. See
[History and lifetime](https://docs.bijection.com/database/history-and-lifetime.md#erasure).


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