# Data

> How a Cases project and a Data project link, where the map lives, and how agents set both up in one step.

Applies to: the Sypnia engine (open source, self-hosted) and Sypnia Cloud (paid service).  
Source: https://cases.docs.sypnia.com/links/data/

For agents: Maps live in Cases, not in Data; Sypnorm, the use-case ORM, is part of Cases (@sypnia/cases/sypnorm), and Data works with any ORM. A Cases map links to one Data project (PUT /v1/maps/{map}/data-project { data_project: <ref> }, or the MCP tool link_data_project): Cases checks the caller can open the Data project, links the map, and asks Data's control plane to link back (PUT /v1/projects/{ref}/cases-map { map, url }, DELETE to unlink; the answer's reverse_link says it did); a Data project links to one map (409 otherwise). `npx @sypnia/cases link <map> --data <ref>` writes both into the [cases] table of sypnia.toml and makes the link both ways. On each case Cases lists the tables it reads and writes from the Data project's schema (GET /v1/projects/{ref}/engine/sql/v1/schema), whether RLS is on, its policies in plain words and SQL, and warnings (table missing, RLS off, RLS without policies). `npx @sypnia/cases map --upload --project <x>`: a 20-letter Data ref sends the map document to the Data project's «Use cases» (PUT /v1/projects/{ref}/use-cases on data.api.sypnia.com), and Data forwards it to the linked Cases map as a draft proposal on behalf of the same person (answer: { stored, cases: { status: forwarded|failed, map, proposal } }); any other value is a Cases map, and the upload goes straight to the Cases server (POST /v1/maps/{map}/uploads) as a draft proposal. Uploads made before the link are not imported. Data's console «Use cases» tab shows the last uploaded map: cases, actors, tables each touches, failures. In Sypnia Cloud, Cases calls Data's control plane on behalf of the person it verified (Authorization: Service <key> shared by the two products, with x-sypnia-user), so every session works, DPoP-bound ones included; Data accepts it only on GET /v1/me, GET /v1/projects/{ref}, PUT|DELETE …/cases-map and the linked project's sql/v1/schema and sql/v1/query, and still checks the person's role. Outcomes someone listens to are saved in the outbox by the area's service (direct Postgres transaction), never over the Data API. Agents: `npx @sypnia/data init --cases` installs @sypnia/cases in the app (with its package manager; --no-install skips it) and the sypnia-cases skill; the agent uses the Cases MCP tools create_project, link_data_project, upload_map, read_map.

**The use-case map lives in Cases, and so does Sypnorm.** Sypnia Data is the data layer: sign-in, tables, the API, SQL, row-level security and MCP, with any ORM or none. Cases keeps the map, and Sypnorm, its ORM, derives the code side of the map. The two products link, so each shows what it needs from the other.

## One link, both ways

A **Cases map links to one Data project**, and asks that Data project to link back to it. You make the link from Cases: the [MCP tool](https://cases.docs.sypnia.com/tools/mcp) `link_data_project`, or the [API](https://cases.docs.sypnia.com/api):

```bash
curl -X PUT https://cases.api.sypnia.com/v1/maps/la-espiga/data-project \
  -H "Authorization: Bearer $SYPNIA_TOKEN" -H "Content-Type: application/json" \
  -d '{ "data_project": "<ref>" }'
```

Cases checks that you can open the Data project, links the map to it, and asks Sypnia Data's control plane to link back (`PUT /v1/projects/{ref}/cases-map`, with the map and its address). The answer's `reverse_link` says Data linked back, and Data's `GET /v1/projects/{ref}/use-cases` then names the map (`cases: { map, url }`). A Data project links to one map: linking it to a second map answers `409`. Unlinking (`DELETE /v1/maps/{map}/data-project`) removes both sides.

| Where                                         | What you see                                                                                                                                                                                                                                                              |
| --------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **In Cases**, on each use case                | the tables it reads and writes, from the Data project's real schema; whether row-level security is on; the policies, in plain words and in SQL; a warning when a table the case names does not exist, when row-level security is off, or when it is on without any policy |
| **In Data**, in the console's «Use cases» tab | the map last uploaded with `map --upload`: how many cases, each case's actor, the tables it reads and writes and the failures it can end in, and the command that uploads it again. Once linked, every upload there also reaches the Cases map, as a draft proposal       |

The warnings are suggestions for the team: Cases never changes a policy, and Data never changes the map.

In Sypnia Cloud, Cases reads the Data project **as you**. It checks your session first, then calls Data's control plane with a key the two products share (`Authorization: Service <key>`) and the id of the person it verified. So every kind of session works, the key-bound (DPoP) one `npx @sypnia/cases login` saves included. Data accepts these calls only for what the link needs (who you are, the project, the link itself, and the linked project's schema and SQL), and still checks your role in the project: a Data viewer cannot replay deliveries, for example ([Authentication](https://cases.docs.sypnia.com/api#authentication)).

## Linking a repository

```bash
npx @sypnia/cases link la-espiga --data <ref>
```

writes the Cases map and the Data project into the `[cases]` table of `sypnia.toml`, so the CLI knows both, and links them, both ways. Then

```bash
npx @sypnia/cases map --upload
```

sends the code side of the map to the Cases map (`la-espiga`) as a **draft proposal** ([The map from your code](https://cases.docs.sypnia.com/sypnorm/the-map)). Named with `--project <ref>`, a Data project's 20-letter ref sends it to that project instead: its use cases, with their tables, outcomes, starts and places, appear in the console's «Use cases» tab, and Data forwards the map to the linked Cases map, also as a draft proposal. The code never changes an approved version: a person approves it ([Versions, proposals and reviews](https://cases.docs.sypnia.com/contract/versions-and-reviews)).

Sypnorm runs on the Data project through its API, with each person's token, or with a direct connection from your server ([Runners](https://cases.docs.sypnia.com/sypnorm/runners)). Nothing about the link requires Sypnorm: a Data project used with Prisma or plain SQL links to Cases the same way, and Cases reads its tables and policies.

### Outcomes saved with the data

When a case's outcome has listeners, the area's service saves it in the outbox **in the same Postgres transaction** as the rows the case wrote, over its direct connection to the Data project. The browser never writes it: a case some case listens to is called at `POST /_cases/<case-id>` on its area's service, with the person's Sypnia Data session, and runs there under the project's row-level security. The Data API runner refuses such a case, since the Data API never exposes the schema `sypnia_cases` ([Runners](https://cases.docs.sypnia.com/sypnorm/runners#a-case-someone-listens-to-runs-on-the-server)). Cases nobody listens to can still run over the [Data API](https://data.docs.sypnia.com/data/rest-api/#transactions) with `createRunner(client, { atomic: true })`. The outbox and its delivery tables live in the schema `sypnia_cases` of the same Data project ([Delivery](https://cases.docs.sypnia.com/runtime/delivery#the-tables)).

### Maps uploaded to Data

A map uploaded to Sypnia Data's control plane (`PUT /v1/projects/{ref}/use-cases`) is kept with the Data project, one per project: each upload replaces the last one. When the project is linked, Data forwards the same document to the linked Cases map, as a draft proposal made on behalf of the person who uploaded it, and its answer says what happened: `{ "stored": 11, "cases": { "status": "forwarded", "map": "la-espiga", "proposal": "<id>" } }`. When Cases does not take it at that moment, `status` is `failed` and the Data side is still saved; upload again to send it. A map uploaded before the link stays in Data only. The document carries each case's id, area, title, actor, the tables it touches, its outcomes with their event names, its failures, its start, place and path in the navigation, the events it listens to, plus the navigation tree, the event graph and the check's findings.

## Starting Cases from Data

You use the same **Sypnia account and session** in both products. Data's «Use cases» tab explains in plain words what use cases are, and links to [Sypnia Cases](https://cases.sypnia.com). With the same account, a Cases map is created in your Sypnia organization (`create_project` in the [MCP tools](https://cases.docs.sypnia.com/tools/mcp), or `POST /v1/maps` in the [API](https://cases.docs.sypnia.com/api)) and linked to the Data project as above.

## Agents set both up in one step

`npx @sypnia/data init` asks which ORM the app will use (plain SQL, Prisma, Drizzle or **Sypnorm**) and whether it will use **Cases**. Choosing Sypnorm or Cases installs `@sypnia/cases` in the app, with the app's package manager (`--no-install` skips it), and the `sypnia-cases` skill, and the coding agent, signed in with your Sypnia account, uses the [Cases MCP tools](https://cases.docs.sypnia.com/tools/mcp) in one go:

1. `create_project`: the Cases map, in the same organization as the Data project.
2. `link_data_project`: the link, both ways.
3. `upload_map`: the map Sypnorm derives from the code, as a draft proposal.
4. `read_map`: to work from the approved map afterwards.
