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

# Define customers

> Keep local decisions in their own table beside source-owned account observations

The [OpenAPI CRM example](https://github.com/bijectionhq/bijection/tree/main/examples/openapi-crm)
keeps the observed CRM accounts in a table only the feed writes. A separate
`customers` table holds the local identity, its associated account, the
requested name and how the CRM answered the latest rename.

## Declare the tables

```ts bijection/crm.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
/** The CRM's accounts as last observed: one fact per `external_id`. Only the
 * `customers` feed's landing writes it. */
export const accounts = table(
  { external_id: t.string, name: t.string, email: t.string, revision: t.string },
  { label: crm, unique: [["external_id"]], index: [["external_id"]] },
);
```

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

export { accounts } from "./crm";

export const customers = table(
  {
    label: t.string,
    account: t.string,
    requested_name: t.maybe(t.string),
    rename: t.maybe({ name: t.string, status: t.string, detail: t.maybe(t.string) }),
  },
  { label: crm, unique: [["account"]] },
);
```

Both tables carry the `crm` label, so only frames that clear `crm` read or
write them. `unique: [["external_id"]]` makes `accounts({ external_id })` a
point. Both tables are [protected](https://docs.bijection.com/database/protected-tables.md): the store
keeps only a token of each unique field, so `customers({ account })` answers
with the account fixed, and a range or an order over it is read with
`ctx.list(selection.atMost(n))`.
The tree places both under `data`, as `data.accounts` and `data.customers`.
[Tables](https://docs.bijection.com/database/tables.md) and [indexes](https://docs.bijection.com/database/indexes.md) cover the
declarations.

## Associate and read

`customers.associate` checks that the account was observed and that no
customer exists for it before inserting one:

```ts bijection/customers.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
export const associate = mutation(
  { under: crm, allow: [writer], args: { account: t.string, label: t.string }, refuses: { AccountUnavailable: null } },
  async (ctx, { account, label }) => {
    if (!(await ctx.has(accounts({ external_id: account }))))
      ctx.refuse("AccountUnavailable", "The CRM account has not been observed");
    const existing = await ctx.get(customers({ account }));
    if (existing !== null) return existing.id;
    return await ctx.insert(customers, { label, account, requested_name: null, rename: null });
  },
);
```

* `ctx.has` reads only the account's presence. `ctx.get` reads a fact, or
  `null`.
* `ctx.insert(customers, fact)` mints the key and returns it. It assumes the
  fact is absent.
* `ctx.refuse` leaves the transaction with a code from `refuses`.

`customers.directory` reads the first 100 customers, each with its observed
account, and answers the CRM's `observed_name` beside the app's
`requested_name`. Continue to [control access](https://docs.bijection.com/start/tutorial/control-access.md).


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