Skip to Content
Views

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 effect

Circuits 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:

  1. The runner reads new pages from the input State streams discovered from View.from.
  2. The circuit checks its stored plan hash for the binding's namespace.
  3. The Views engine updates shared integrals, arrangements, and accumulators for the affected keys only.
  4. 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.
  5. 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.

LayerConstructor or APIWhat it does
Hand-written projectionProjection.fold, emit, mapAccum, mapAccumBatch, processRuns custom entry or batch handlers over JSON or State streams, keeping a checkpoint value or writing custom SQL tables. See Projections
Keyed foldFoldByKey.processFolds 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 tableStateTable.defineIndexes 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 circuitView.circuitMaintains 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.

examples/views-shop/src/streams.ts
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-shop

Supported operators and current scope

Supported relational operators and features:

  • where, select, and selectRow, with typed expressions and pure View.fn helpers
  • equality join and leftJoin with optional-preserving nested references
  • groupBy with View.count, View.sum, View.min, View.max, and View.list
  • bounded top with optional partitionBy
  • concat to combine two relations into one
  • closure to compute bounded ancestor hierarchies with optional path values
  • View.routed to 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

On this page