Rat docs

Queries

Every declared model gets a CRUD surface on its database global. The shape is uniform: main_db.user.list(), main_db.user.get(id), main_db.user.add({...}), main_db.user.update(id, {...}), main_db.user.delete(id). The same calls work from pages, services, API handlers, and Tail wrappers.

Read in > server

Resolves before the page renders

Query calls in > server evaluate during the snapshot pass, so the result lands as a regular value in scope. Pair with a loop to render lists without any handler wiring.

> server
users: main_db.user.list()

> page
<ul>
    [u] in users
        <li> [u.name][u.email]

Write from a handler

Round-trip through __ratCall

Mutations inside event handlers fire over the call endpoint, run server-side, and patch the reactive spans. Pass a literal object — the field names line up with model field names.

> page
draft_name: ''

<input value[draft_name] on_input[draft_name << event.target.value]>
<button on_click[main_db.user.add({name: draft_name})]> add user

Single-record fetch

get(id) returns null if missing

Use get(id) for primary-key lookups. A miss returns null, so guard the read with (user is not null) before reading fields off it.

# pages/users/[id].rat
> server
user: main_db.user.get(args.id)

> page
(user is not null)
    <h1> [user.name]
(else)
    <h1> Not found

Next section: API — exposing functions as JSON endpoints under /api/.