# The map from your code

> Derive the map from your use cases, upload it to your Cases project, and keep code and map in agreement.

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

For agents: `npx @sypnia/cases map` statically reads every useCase(...) (the case id, summary, actor, input, touches, the outcome and failure names its ctx.outcome calls can end in, file:line) and writes sypnorm.map.json, the code side of the Cases map. `--upload --project <ref>` sends the map document (each case's id, area, title, actor, tables touched, outcomes with event names, failures, start, place and path, listened events; plus the navigation tree, the event graph and the check's findings) to a Cases map on the Cases server (POST /v1/maps/{map}/uploads on cases.api.sypnia.com) as a draft proposal, or, when <ref> is a 20-letter Data project ref, to that project's «Use cases» (PUT /v1/projects/{ref}/use-cases on data.api.sypnia.com, the console's Use cases tab), which Data forwards to its linked Cases map as a draft proposal; a map with errors is not uploaded unless --force; signs in with SYPNIA_TOKEN or the session of `npx @sypnia/cases login`. `--check` fails CI when code and committed file differ; `--json` prints it; `--out` names the file. `npx @sypnia/cases link <map> --data <ref>` writes the Cases map and the Data project into the [cases] table of sypnia.toml and links them both ways. Programmatic: import { deriveMap, uploadMap } from '@sypnia/cases/map'.

## Derive it

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

Sypnorm reads your code, without running it, and finds every `useCase(...)`. It writes `sypnorm.map.json`, the **code side** of your map:

`sypnorm.map.json`

```json
{
  "app": "la-espiga",
  "useCases": [
    {
      "case": "place-order",
      "summary": "A customer orders today's bread for pick-up",
      "actor": "customer",
      "reads": ["products", "opening_hours"],
      "writes": ["orders", "order_items", "products"],
      "outcomes": ["OrderPlaced"],
      "failures": ["BakeryClosed", "OutOfStock"],
      "source": "src/cases/orders/place-order.ts:5"
    }
  ]
}
```

| Flag            | Does                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| --------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `--upload`      | sends the map to your Cases map as a **draft proposal** that a person approves ([Versions, proposals and reviews](https://cases.docs.sypnia.com/contract/versions-and-reviews)); sent to a Sypnia Data project, it lands in its **«Use cases»**, where the console shows each case with the tables it touches, and Data forwards it to the linked Cases map, also as a draft proposal ([Data](https://cases.docs.sypnia.com/links/data#maps-uploaded-to-data)) |
| `--project <x>` | where to upload: 20 lowercase letters is a Data project's ref (as in the console's address); anything else is a Cases map's slug on the Cases server (`cases.api.sypnia.com`, or `SYPNIA_CASES_API`). By default `project`, then `data`, of the `[cases]` table of `sypnia.toml`                                                                                                                                                                               |
| `--force`       | uploads even when the check finds errors in the map (by default a map with errors is not uploaded)                                                                                                                                                                                                                                                                                                                                                             |
| `--check`       | fails in CI when the code and the committed file differ, so the file stays true                                                                                                                                                                                                                                                                                                                                                                                |
| `--json`        | prints it, for [`cases check`](https://cases.docs.sypnia.com/contract/the-check) or an agent                                                                                                                                                                                                                                                                                                                                                                   |
| `--out <file>`  | writes another file than `sypnorm.map.json`                                                                                                                                                                                                                                                                                                                                                                                                                    |

The upload is more than `sypnorm.map.json`: it is the whole map as one document, the code side merged with the case files. Each case carries its area, title, actor, the tables it touches, its outcomes with their event names, its failures, its start, its place and path in the navigation, and the events it listens to; the document also carries the navigation tree, the event graph and the check's findings. It signs in with `SYPNIA_TOKEN`, or with the session `npx @sypnia/cases login` saved.

`npx @sypnia/cases check` uses the same reading: it compares the code side with the written map and reports what is missing, unmapped or drifting.

## Link it

```bash
npx @sypnia/cases login
npx @sypnia/cases link la-espiga --data <ref>      # this repository's Cases map and its Sypnia Data project
```

`login` signs you in with your Sypnia account, the same session as `npx @sypnia/data login`. `link` writes the Cases map into the `[cases]` table of `sypnia.toml` (`project = "la-espiga"`) and, with `--data`, the Data project (`data = "<ref>"`), and links the map with the [Sypnia Data project](https://cases.docs.sypnia.com/links/data#one-link-both-ways), both ways; from then on each case shows its tables and their policies. After that, `map --upload` knows where to go (or name it: `npx @sypnia/cases map --upload --project la-espiga`).

## From your own scripts

```ts
import { deriveMap, uploadMap } from '@sypnia/cases/map';

const map = await deriveMap({ root: '.', include: ['src/cases/**/*.ts'] });
await uploadMap(map, { project: '<ref>', token: process.env.SYPNIA_TOKEN! });   // a Data project's ref; Data forwards it to the linked map
```

## What comes from code, what from people

The code owns the data and the allowed actors, and must end only in the outcomes the case declares; people own the title, the goal, how it starts, where it lives, the steps and the messages. [The map from code](https://cases.docs.sypnia.com/format/from-code) has the full table and how `cases sync --from-code` fills the files.
