Skip to content

Connect a database

Your app can read and write your own data. This tutorial shows a list of rows from a Supabase table. MySQL works the same way with different connection details.

How it stays safe is described in How it works: the app holds no credentials, and it can only run the reads and writes you approve.

Open your project in Studio and go to Database. Choose Supabase and enter:

  • a connection name,
  • your project URL,
  • your anon (publishable) key. Never the service role key: with the anon key, your row level security rules stay in charge.

Studio tests the connection before saving it.

.ynch/connections.json names the connection the app uses. It holds no address and no key:

{
"schemaVersion": "1.0.0",
"connections": {
"primary": { "cloudConnectionId": "conn_main", "provider": "supabase" }
}
}

Say the table is topics with id, name and description. Create .ynch/models/topic.json:

{
"schemaVersion": "1.0.0",
"id": "topic",
"identityField": "id",
"fields": {
"id": { "type": "string", "required": true, "minLength": 36, "maxLength": 36 },
"name": { "type": "string", "required": true, "maxLength": 120 },
"description": { "type": "string", "required": false, "maxLength": 500 }
}
}

Create .ynch/operations/list-topics.json:

{
"schemaVersion": "1.0.0",
"id": "list-topics",
"kind": "list",
"connection": "primary",
"connector": "supabase",
"resource": "topics",
"columns": ["id", "name", "description"],
"responseModel": "topic",
"parameters": {},
"filters": [],
"sort": [{ "field": "name", "direction": "asc" }, { "field": "id", "direction": "asc" }],
"maxRows": 50,
"pagination": { "mode": "offset", "defaultLimit": 25, "maxOffset": 10000 }
}

This is the whole of what the app may do with that table: read those three columns, sorted by name, up to 50 rows at a time. A list’s sort always ends with the identity field so paging is stable.

In a screen, add a RecordList. Its one child is drawn once per row, and components inside it can show the row’s fields:

{
"id": "topic-list",
"type": "RecordList",
"props": { "pageSize": 25, "emptyText": "No topics yet" },
"bindings": {
"items": { "operationId": "list-topics", "parameters": {}, "pagination": { "offset": 0, "limit": 25 } }
},
"children": [
{ "id": "topic-item", "type": "Card", "props": {}, "preset": "panel", "children": [
{ "id": "topic-name", "type": "Text", "props": {}, "bindings": { "value": { "model": "topic", "field": "name" } } },
{ "id": "topic-description", "type": "Text", "props": {}, "bindings": { "value": { "model": "topic", "field": "description" } } }
] }
]
}
Terminal window
npx ynch check
npx ynch upload

In Studio, follow the Releases checklist: approve and sign, make it active, then Accept database changes. Studio shows each operation in plain words (“Read a list of rows from topics”) before you accept.

Open the app on your iPhone. The list loads from your database.