# Entities

Structured business records through the governed door — RLS-scoped, and cached beside your app when it serves them in public.

`env.entities` is how an app reads and writes the organization's structured
records — customers, projects, orders, whatever the organization's data
plane holds. Records are typed rows with identity and links, not documents
in a bucket.

> [!NOTE]
> **Early access**
>
> Needs an organisation in Pilvia Mieli, which an early-access account does
> not include: see [Data & AI](/data/overview.md).

```ts
const customer = await env.entities.get({ id });
const member = await env.entities.get({ identifier: 'slug=aalto', type: 'member' });
const open = await env.entities.list({ type: 'project', match: { status: 'open' } });
await env.entities.upsert({ type: 'note', data: { name: 'Call back', about: id } });
await env.entities.link(projectId, 'customer_of', customerId);
```

`get` takes an id, an identifier (`"kind=value"`, or `{ kind, value }`), and
optionally a `type`: a row of another type then answers "not found". `list`
takes a `type` and the data plane's exact filters — `match`, `contains`,
`range` — and a `limit` of at most 200; it answers `{ count, entities }`,
where `count` is the full total.

Authorization is not your code's job. On an authenticated route these calls
run as the signed-in user and row-level security decides what exists for
them; a public route sees nothing unless the manifest granted the app its
own [`app_data` access](/auth/overview.md).

## Cached reads

A content site reads its content on every request, and that content's system
of record is the data plane. Declare an `entities` block in the manifest and
the platform keeps a **cache of the app's own reads** beside it, kept fresh
by the data plane's change feed:

```json
{
  "auth": "public",
  "app_data": true,
  "entities": { "mirror": ["member", "news"] }
}
```

The block reaches the platform in deploys made with `pilvia` 0.2.9 or newer
(`pilvia --version`); an older CLI leaves it out of the manifest it sends.
A push deploy builds with a current one for you.

`env.entities.get` and `list` stay exactly the same calls. A read the cache
holds is answered from the app's own store, without a network round trip;
a miss reads through the door and is kept. Nothing in the app polls,
follows a cursor, or invalidates anything.

**What is cached.** Only the reads the app makes as **its own principal on
anonymous requests** — the public serving path. Reads on behalf of a
signed-in user always go to the data plane live: different users see
different rows, and a shared cache must never blur that. It also means an
editor reads their own writes immediately. Machine calls (a job, a
trigger-token call) run as the app too, and read live, because a sync job
has to see another writer's row the moment it exists.

**The freshness contract:**

- A committed write through the door is visible to reads **on the same
  node at ack** — read-your-writes where you wrote.
- A write held for review by the data plane changes nothing until enacted —
  the change event is the truth and the cache follows it.
- Across nodes, and for writes made anywhere else: **bounded staleness**,
  as long as the change feed takes to deliver the change. Measured on our
  isle, a write in the data plane was served by every node within 0.2 s
  (p90), the first read after it asking the data plane once.
  If the feed stops answering, the cache is trusted for one more minute;
  after that reads go back to the data plane, and the cache is only their
  fallback.

A list is cached by the exact question asked, and any change to a row of
its type refreshes every cached list of that type. So a list whose filter
holds the current time is a new question every request and is never
answered from the cache: filter time in your code instead.

## Mirrored types

`entities.mirror` names types the platform keeps **whole** beside the app.
For those, a `get` that names the type answers "not found" from the cache
itself — a miss means the row does not exist — and a changed row is read
again before the old one is replaced, so the site keeps serving through a
data-plane outage. Their cached lists are asked again the moment the type
changes. A mirrored type holds at most 20,000 rows and 64 MiB; above that it
is read through like any other.

## When the data plane is unreachable

| Read | Answered from the cache |
| --- | --- |
| `get` naming a mirrored type | yes — the row, or "not found" |
| `get` the cache holds | yes |
| `list` the cache holds (the exact question) | yes |
| anything else | no: it fails with `error.code === 'entities_unavailable'` |

What the cache answers during an outage is as fresh as the outage is long.
A data plane that stops answering costs one slow read per node every 10
seconds, at most 3 seconds long where the cache has something to answer
with; the reads in between answer at once.

If the data plane **refuses** the app's principal — an expired or revoked
token — the cache is emptied and nothing is served from it: reads fail with
`error.code === 'entities_refused'` until the principal is fixed. A new
token (`pilvia app secret`) is tried at once.
