Skip to main content
Tables store the facts your app writes. Most apps also need business objects built from those facts: a customer together with its order count, an invoice together with the customer it belongs to. Bijection gives you two schema declarations for this:
  • A view computes a keyed, read-only collection from your tables. Each view defines an object type, and each row is one identified object.
  • A link type is an ordinary table whose rows associate two records, with the cardinality you declare enforced on every write.
Both live in your schema, and both are read with the same ctx.db calls you already use in queries. Here is a view that summarizes each customer’s orders:
bijection/schema.ts
And a query that reads it like any table:
bijection/customers.ts
The result is typed from the view’s expression. When an order is inserted, every client subscribed to summary for that customer receives the new count, just as with any other query.

Views

A view is declared with defineView({ key, expression }). The expression is built with the q builder from a fixed set of relational operators: scan, filter, projection, equality joins and grouping. Bijection checks the whole definition when you deploy, infers the output type, and evaluates it for you. You never write to a view: its rows change when its inputs change. A view is virtual by default, which means it’s evaluated when it’s read. Add .materialize() and Bijection stores its output instead, keeping it current in the same transaction as every write to its inputs. Both forms return the same results. A link type is a table with two typed ID fields, its endpoints, and a .link(...) declaration that states how many links each record may take part in:
bijection/schema.ts
Here a customer can have any number of invoices and an invoice belongs to at most one customer. You create and end links with ordinary mutations, and Bijection refuses any transaction whose final state breaks the declared bounds.

Defining views

Declare a view with a key and a q expression.

Joins and aggregates

Combine tables with equality joins and group with counts and sums.

Reading views

Read views from queries, with indexes, pagination and reactivity.

Materialized views

Store a view’s output and keep it current on every write.

Link types

Declare associations between records with enforced cardinality.
To control who can read a view or a link table, see Access rules.