Reading and writing data
Reading and writing data
Pages, automations, transforms and the agent all read and write through ctx, in the same way. A read describes the records it wants as one JSON tree. A write names the model verbs it calls.
Read requests
Each top-level key is a model. Inside it go where, fields, order_by (including order_by: 'id' or { id: 'desc' } for chronological UUIDv7 order), limit and search, and any link of the model's output, with its own options. The answer has the same shape: for each model, rows, next (a cursor for the next page) and total.
<script lang="ts">
import type { PageContext } from '@norbital-ai/bolt/page';
let { ctx }: { ctx: PageContext } = $props();
const day = ctx.clock('day');
// today's open jobs, each with its site's name; live while the page is open
const today = $derived(
ctx.query({
job_assignments: {
where: { scheduled_for: { eq: day.today }, status: { eq: 'pending' } },
fields: ['title', 'status'],
order_by: { title: 'asc' },
site_id: { fields: ['name'] }
}
})
);
</script>
{#if today.current}
{#each today.current.job_assignments.rows as job (job.id)}
<p>{job.title} · {job.site_id?.name}</p>
{/each}
{/if} A read uses one output of the model (default, unless you pass output) and returns only rows your policies allow. Your where narrows those rows; it can never widen them.
What happens when it runs
- The page opens and the query subscribes. It lists today's pending jobs with their site names.
- A contractor completes one of them on their phone.
- The completed job no longer matches the
where, so it disappears from this page within moments, with no reload.
Live and once
On a page, ctx.query returns a live query. Reading .current, .loading or .error in markup subscribes to it, and the page updates when the rows change: only the rows that changed travel, not the whole answer. The same read asked twice, on one page or in two tabs of the same person, is one subscription. In an event handler, await ctx.query(…) reads once. In server code, yield* ctx.query(…) reads once.
Time is explicit. On a page, ctx.clock('day').today and ctx.clock('minute').now move forward on their own. Automations and transforms use ctx.today and ctx.now.
Writes
ctx.act takes a map of model verbs, each with a list of rows. Everything in one map is saved together or not at all. Use new_id(model) when you need a typed, time-ordered UUIDv7 id up front (for example to wire a parent and child row in the same batch). An update or delete names its target: the row you read, which carries its revision. If someone changed the row since, the write fails as a conflict instead of overwriting their change.
// on a page: an Effect, run from a click with fork()
import { fork } from '@norbital-ai/ui/effect';
const complete = (job: { id: Id<'job_assignments'>; revision: number }) =>
fork(
ctx.act({ 'job_assignments.update': [{ target: job, set: { status: 'completed' } }] }),
(failure) => (problem = failure._tag)
);
// in an automation: several models in one commit
yield* ctx.act({
'bookings.update': [{ target: booking, set: { status: 'cancelled' } }],
'customer_notices.create': [{ customer: booking.customer, subject, body }]
}); Every write ends in one outcome: saved, held for approval, refused, in conflict or unknown. Write outcomes lists them.
Starting automations
A page's ctx.run('name', input) starts an automation; a call automation answers in the same request. Server code uses ctx.schedule: the run starts after the current write is saved, at a given time or under a key that replaces an earlier queued run.
History
Every saved change is kept as a revision with who, when and why. ctx.history(model, id) lists them, and a read's as_of shows rows as they stood at a moment. A record's History tab can restore a past revision as a new change.
There are three ways back, and they are different. Restore this version on a record's History puts that one record back, as a new change. Roll back in Studio returns live to an earlier version, source and data together. Point-in-time recovery restores the whole database from the host's backups, and is an operator's job.