# Ynch models, operations, lists and forms

How an app reads and writes the owner's database. The app declares; the owner approves in Studio; nothing here grants access by itself.

Full docs: https://docs.ynch.ai/llms.txt (every page is markdown at its address plus `.md`).

## Rules

1. Edit only files inside `.ynch`. Never change `projectId` in `project.json`, and never edit `ynch.lock`.
2. Use only the components, props, style options and icons listed here. There are no custom components, no CSS, no JavaScript and no SQL. An unknown property is an error.
3. Every JSON file starts with `"schemaVersion": "1.0.0"`. Strict JSON: no comments, no trailing commas.
4. Ids are lowercase letters, digits and hyphens, starting with a letter. A file's name is its id.
5. After every change run `npx ynch check` and fix everything it reports. Do not remove a requested feature to silence an error.
6. Never write passwords, keys or tokens into any file.
7. Do not run `ynch upload`. The person uploads and approves each version in Ynch Studio.

## Connection

`.ynch/connections.json`: `{"schemaVersion":"1.0.0","connections":{"primary":{"cloudConnectionId":"conn_main","provider":"supabase"}}}`. Provider is `supabase` or `mysql`. At most one connection. No hosts, passwords or keys, ever.

## Models

`{"schemaVersion":"1.0.0","id":"workout","identityField":"id","fields":{"id":{"type":"integer","required":true,"generated":true},"name":{"type":"string","required":true,"maxLength":80}}}`

- Types: string (minLength, maxLength, enum), number, integer (minimum, maximum), boolean, date (YYYY-MM-DD).
- Every field has `type` and `required`. Only the identity field may be `generated`.
- Hyphens in names map to underscores in the database (`topic-id` is column `topic_id`).

## Operations

Common to all: `id`, `kind`, `connection`, `connector` (same as the connection's provider), `resource` (table or view), `columns`, `responseModel`, `parameters`, `maxRows`.

- `list`: `filters`, `sort` (ends with the identity field), `pagination: {"mode":"offset","defaultLimit":25,"maxOffset":10000}`, `maxRows` 1 to 100.
- `get`, `update`, `delete`: a filter with `eq` on the identity field; `maxRows` 1.
- `insert`, `update`: `values` maps field to parameter name. Never assign a generated field.
- Filters: `{"field","operator","parameter"}`, operators eq, lt, lte, gt, gte. No joins, no OR, no SQL.
- Every declared parameter must be used.

## On a screen

- `RecordList`: `"bindings":{"items":{"operationId":"list-x","parameters":{},"pagination":{"offset":0,"limit":25}}}` and exactly one child, the row template.
- Show a field: `"bindings":{"value":{"model":"x","field":"name"}}` on Text, Badge or an input, inside a RecordList or Form.
- `Button` running an operation: `"action":{"type":"operation","operationId":"delete-x","parameters":{"id":{"source":"field","model":"x","field":"id"}}}`.
- `Form`: `"props":{"model":"x","submitLabel":"Save"}`, a `form` configuration (`mode` create or edit, `initialData`), an operation `action`, and inputs bound to the model's fields. Read https://docs.ynch.ai/reference/components/form.md before writing one.
- Parameter sources: `{"source":"literal","value":1}`, `{"source":"field","model":"x","field":"id"}`, `{"source":"route","parameter":"id"}`.

## App sign-in (Supabase only)

Add `"userAuth":{"userColumn":"user-id"}` to the connection. Include that field in models and operation columns for per-user tables. Do not set it from the app: Studio fills it on insert and filters by it on read.
