Views
Incrementally maintain derived Durable State collections with declarative circuits that run as projections.
@streamsy/views maintains derived Durable State collections incrementally. A circuit consumes input State pages, updates indexed operator state, appends changed output rows, and saves its checkpoint in one owner transaction.
Views supports SQLite (Bun and Cloudflare Durable Objects), Postgres, and MySQL for SQL operator state, plus a memory host that keeps operator state in the heap. Use View.live to read committed snapshots in a browser or any Effect client without output streams.
Install
@streamsy/views runs circuits with @streamsy/projection and declares streams with @streamsy/core. Install it with Effect 4:
bun add @streamsy/views @streamsy/projection @streamsy/core effectCircuits store operator state on the same storage owner as their checkpoints.
What a circuit does
A circuit compiles one or more relational views into a single transactional Projection.process declaration. Within each unit:
- The runner reads new pages from the input State streams discovered from
View.from. - The circuit checks its stored plan hash for the binding's namespace.
- The Views engine updates shared integrals, arrangements, and accumulators for the affected keys only.
- The circuit coalesces the resulting row changes and appends at most one change per collection and key pair to each generation-addressed output stream, carrying
txid: unit.key. - Operator state, plan metadata, output appends, and the input checkpoint commit together in the storage owner's transaction.
When a unit produces no net output changes, the circuit appends no output messages and still advances its checkpoint.
Where Views sits among projections
Streamsy offers four layers for deriving and indexing data. Choose the smallest one that fits your workload.
| Layer | Constructor or API | What it does |
|---|---|---|
| Hand-written projection | Projection.fold, emit, mapAccum, mapAccumBatch, process | Runs custom entry or batch handlers over JSON or State streams, keeping a checkpoint value or writing custom SQL tables. See Projections |
| Keyed fold | FoldByKey.process | Folds input records into one State collection, loading previous rows from the output's StateTable. Good for event-to-row state machines. See Keyed folds |
| State table | StateTable.define | Indexes an existing State stream in SQLite for get, getMany, and list reads by key. Does not join, filter, aggregate, or rank across collections. See State tables |
| View circuit | View.circuit | Maintains joins, filters, projections, grouped aggregates, bounded top-N, unions, and ancestor closures over State collections incrementally, writing derived State streams |
Output streams written by a circuit are normal State streams. Readers can materialize them directly or index them with StateTable.define.
Example
The private examples/views-shop workspace package defines an Orders input stream, a generation-addressed ShopViews output stream, and a circuit that maintains customerTotals and each customer's highest-value topOrder.
export const shopCircuit = (generation: number, minimumTotal?: number) => {
const orders = View.from(Orders, "order");
const selected =
minimumTotal === undefined ? orders : orders.where((row) => row.total.gte(minimumTotal));
return View.circuit({
id: "views-shop",
params: Orders.paramCodecs,
generation,
output: Projection.perGeneration(ShopViews, "generation"),
views: {
customerTotals: selected
.groupBy((row) => ({ customerId: row.customerId }))
.aggregate((row) => ({ orders: View.count(), revenue: View.sum(row.total) })),
topOrder: selected.top({
partitionBy: (row) => row.customerId,
by: (row) => [row.total.desc(), row.orderId.asc()],
limit: 1,
}),
},
});
};Run the example smoke script from the repository root:
bun run build
bun run smoke:views-shopSupported operators and current scope
Supported relational operators and features:
where,select, andselectRow, with typed expressions and pureView.fnhelpers- equality
joinandleftJoinwith optional-preserving nested references groupBywithView.count,View.sum,View.min,View.max, andView.list- bounded
topwith optionalpartitionBy concatto combine two relations into oneclosureto compute bounded ancestor hierarchies with optional path valuesView.routedto route one circuit binding's output rows across multiple streams- binding parameter expressions via
views: (params) => ({ ... })
Features outside the current scope include avg, distinct, fact folds, dynamic parameters-as-data, frontier rows, and old_value on emitted upserts.
Next
- Build relations — operators, expressions, and
View.fn - Declare and run a circuit — State classes,
View.circuit, and routed outputs - Read live snapshots — scoped in-memory reads with
View.live - Lifecycle and rebuilds — generations, side-by-side cutovers, and retirement
- Ordering and limits — UTF-16 order,
maxAffectedKeys, faults, and scope - Hosts: SQLite, Postgres, MySQL, and Memory
- Reference — full
@streamsy/viewsAPI reference