# @sypnia/data

Auth, the Data API and Sypnorm (the use-case ORM) for [Sypnia Data](https://data.docs.sypnia.com): an open-source backend on any Postgres.

```bash
npm install @sypnia/data
```

| Import | For |
|---|---|
| `@sypnia/data/client` | browsers, phones, any app: auth and the query builder (publishable key) |
| `@sypnia/data/server` | servers: a secret key or a machine client, SQL, admin, migrations, use cases on a direct connection |
| `@sypnia/data/sypnorm` | the ORM: `useCase`, `fail`, typed tables |

The CLI comes with it: `npx @sypnia/data init`, `login`, `pull`, `migrate`, `map`.

## Connect an app and its coding agent

```bash
npx @sypnia/data init --project <ref>            # add --server for the backend's machine client, --cases for Sypnorm + Casos de uso
```

It signs this machine in (device flow, approved in the browser), writes `SYPNIA_URL` and `SYPNIA_PUBLISHABLE_KEY` to `.env.local`, adds the MCP server `https://mcp.data.sypnia.com` to `.mcp.json` / `.cursor/mcp.json`, installs the agent skills into `.claude/skills/` (plus an `AGENTS.md` section) and pulls the types. Nothing secret is printed or written outside `.sypnia/client-key.json` (mode 600, git-ignored).

## In an app

```ts
import { createClient } from '@sypnia/data/client';

export const sypnia = createClient('https://<ref>.data.sypnia.com', 'syp_pk_<ref>_…');

await sypnia.auth.signInWithPassword({ email, password });
const { data, error } = await sypnia.from('orders').select('id, total, customer:customers(name)').eq('status', 'paid');
```

## On a server

```ts
import { createClient, createServer } from '@sypnia/data/server';

const admin = createClient('https://<ref>.data.sypnia.com', { secretKey: process.env.SYPNIA_SECRET_KEY! });
await admin.sql`select count(*) from orders where status = ${'paid'}`;

const worker = createClient(url, { client: { id: 'cli_…', privateKey: process.env.SYPNIA_CLIENT_KEY!, dpop: true } });
```

## Use cases

```ts
import { useCase, fail } from '@sypnia/data/sypnorm';

export const pedirParaHoy = useCase('PedirParaHoy', {
  summary: "A customer orders today's bread for pick-up",
  actor: 'customer',
  input,                                   // a zod (or any Standard Schema) schema
  touches: { reads: [db.products], writes: [db.orders, db.products] },
  failures: { OutOfStock: (p: { name: string; left: number }) => `Only ${p.left} left of ${p.name}` },
  preconditions: [({ actor }) => actor.signedIn || fail('Unauthorized')],
  async run({ tx, actor, input, failures }) { /* … */ },
});

const app = createServer({ databaseUrl: process.env.DATABASE_URL!, cases: [pedirParaHoy], tables: db });
const result = await app.run(pedirParaHoy, input, { actor: { userId, role: 'customer', signedIn: true } });
```

A whole example: [examples/espiga](https://github.com/sespinosav/sypnia-data/tree/main/examples/espiga).

Docs: https://data.docs.sypnia.com · License: MIT
