Derived values
A derived value is worked out from other records for each record of a resource, such as a verdict on an objective from its tasks. You write a small TypeScript function that reads records and the clock; Protobase records what it read, computes it again when a change touches that or when a time check flips, and stores the result. No timer is involved, and a value that did not change is not written. It works on any Postgres, with no extension.
import { f, resource } from '@protobase/schema'
export const objectives = resource('objectives') .table('objectives') .fields({ id: f.integer().readOnly(), title: f.text(), verdict: f.enum(['done', 'open', 'late']).optional().readOnly() }) .primaryKey((r) => r.id) .derived('verdict', { into: (r) => r.verdict, reads: ['tasks'] })
export const tasks = resource('tasks') .table('tasks') .fields({ id: f.integer().readOnly(), objectiveId: f.integer(), status: f.enum(['open', 'closed']), createdAt: f.timestamp().readOnly() }) .primaryKey((r) => r.id)import type { DerivedCompute } from '@protobase/server'
// "late" once an open task is older than two hours, "open" while any is open, else "done": the oldest open task decides.export default (async (key, { db, clock }) => { const oldest = await db.selectFrom('tasks').select(['created_at']).where('objective_id', '=', Number(key)).where('status', '=', 'open').orderBy('created_at').limit(1).executeTakeFirst() if (!oldest) return 'done' return clock.olderThan(oldest.created_at, '2 hours') ? 'late' : 'open'}) satisfies DerivedComputeDeclaring a derived value
Section titled “Declaring a derived value”.derived(name, spec) on the resource whose records get a value. Its compute function is the default export of derived/<resource>.<name>.ts, typed with DerivedCompute from @protobase/server.
| Key | |
|---|---|
into: (r) => r.field |
The read-only field of the record the value goes into. |
into: { resource, field } |
A field of another resource’s record of the same key, added when it has none. Its other fields need defaults. |
job |
A job of the config, enqueued with { resource, key, value } each time the value changes, in place of into or beside it. |
reads |
The other resources the function reads. Their changes are captured as events’ are. |
track: false |
Analyses none of its queries: only the dependencies the function names and its clock count. |
The function gets the record’s key as its URL has it, and returns the value as JSON can hold it; undefined is null. Beside the key it gets:
db |
The app’s database, in a read-only transaction, so a write throws before it happens, and so does opening a transaction of its own. Each query sent through it is analysed, raw SQL included. |
clock |
The time; see The clock. |
depends(resource, where?) |
Names a dependency itself: every record of the resource, or those whose fields hold these values, such as depends('tasks', { status: ['open', 'late'] }). |
signal |
Aborted when the run times out or the worker shuts down. |
What it depends on
Section titled “What it depends on”Each query the function sends is parsed with Postgres’s own parser, however it was built. For each table it reads, Protobase keeps the query’s conditions on that table: comparisons of its columns with constants and parameters, IN, = ANY, BETWEEN and null tests, joined with AND, OR and NOT, with the conditions of inner joins, and of an outer join’s ON for its optional side. The condition is the dependency, so a new row that comes to match is caught as well as a row that leaves it. A part it cannot work out, such as a function call or a column of another table, counts as matching every row: a query it cannot read at all reads the whole table. Nested queries, WITH queries and both sides of a UNION count too. An update counts only when it changes a column the query used.
A table that is no resource of reads or the record’s own stops the run with an error naming it: add its resource to reads. A table that is no resource at all becomes one and goes in reads when it holds data of its own, since otherwise no change to it is captured; for one whose changes come from resources, such as a view, name the resources behind it with depends() and turn tracking off. List every resource the function reads there, those it names with depends() included: only their changes are captured, and depends() on any other stops the run too. depends() adds a dependency by hand to a listed resource and never stands in for reads; with track: false, the dependencies it names are the only ones, for a function whose queries say less than it knows.
The clock
Section titled “The clock”clock.isPast(at) |
Whether the time at has come. |
clock.olderThan(at, duration) |
Whether more than duration has passed since at: milliseconds, or text such as 2 hours. |
clock.now() |
When the function runs. |
Each check that has not flipped yet records the moment it will; the value is computed again then. now() records nothing, so a value that shows the time itself is not kept current.
When it runs
Section titled “When it runs”- A new record is computed once it is created, and a value declared for records that exist already computes each of them once, from the migration that adds it.
- A change that touches a recorded read, through the API or not, marks the value stale in the dispatch of captured changes, and its run waits a second, so a burst of changes runs it once.
- A change that lands while the function runs and touches one of the reads that run made drops its result, and the value is computed again after the same second’s wait; a change to what it did not read leaves the result kept.
- Two runs of one record at once, such as a change’s and a time check’s: only the one that started last keeps its result.
- The value is written only when it differs, so an unchanged value raises no event, live update or notification.
- A deleted record’s value is dropped.
- A value no longer declared loses its state at the next migration, so declaring it again computes every record again.
Runs are derived.compute jobs, four at a time per worker, retried like any job when the function throws.
protobase migration adds a derived schema, applied with the project’s other migrations, and protobase dev and protobase serve do not start without it:
| Table | |
|---|---|
derived.values |
A row per record and value: status (current, stale or recalculating), value as JSON, computed_at, and flips_at, the clock’s next flip. |
derived.reads |
What each record’s value read: the resource, the condition, and the fields it used. |
derived.pending |
The changes that landed while a record’s run was under way, weighed against that run’s reads when it ends. |
derived.declared |
The values whose records the migrations have computed once. |
Limits
Section titled “Limits”-
A derived value cannot fill a list of rows in another resource, such as one row per item that needs attention, because syncing a set of rows is not implemented. A derived value cannot see the reads inside a database function or view it calls, because reading their definitions is not implemented. Name them withdepends().-
A derived value whose function threw cannot show staleuntil its retry runs, because marking a failed run is not implemented; it showsrecalculatingmeanwhile. -
A derived value cannot recompute the records it holds when its function changes, because tracking the function’s code is not implemented; each record takes the new function at its next change or clock flip. -
A derived value cannot keep a result while a change touching what it read lands during every run, because bounding how often a run yields is not implemented; it keeps its last value meanwhile, and a record never computed has none. -
A derived value’s status cannot be read through the REST API, because serving derived.valuesas a resource is not implemented.
Reusing the detection
Section titled “Reusing the detection”The matching of committed changes against recorded reads is not tied to derived values. The same records of which conditions a reader depends on could later drive other features, each weighed on its own and possibly not worth it:
- Live pages: a list page’s query recorded as a read, so the page refreshes only when a change can alter what it shows, instead of on every change of the resource.
- Notifications: a saved condition, such as “tasks assigned to me that are late”, recorded once, so a change or a clock flip that makes a record match notifies its subscriber.
- Job queues: a job whose input is a query, run again when a change touches what it read, as
derived.computeis.