GitHub

The database

Postgres stores the data and Zega, kept in step by pg_zega in the same transaction, answers graph questions. The app describes its tables once, in simien/schema.ts, and reads and writes them through simien:db.

Give a project a database

simien db init

Adds a db section to simien.json (the Postgres and Zega ports: the first free pair from 54330), grants the app the db permission it needs to use its database, and creates simien/schema.ts. --name, --port and --force change what it writes. Templates with a database already have all three.

The grant is one line of the app's policy; db init adds it next to whatever the policy already says:

{ "security": { "allow": { "db": ["postgres"] } } }

schema.ts

import { defineSchema, defineTable, defineEdge, v } from "simien:schema";

export default defineSchema({
  users: defineTable({ name: v.string(), age: v.optional(v.int()) }),
  posts: defineTable({ title: v.string(), published: v.boolean() }),
  wrote: defineEdge("users", "posts"),
});
type stores
v.string() text
v.int() a whole number
v.float() a number
v.boolean() true or false
v.optional(x) x, or null

Every table also gets id, createdAt and updatedAt, set by the database (the timestamps are milliseconds since 1970, UTC). defineEdge(from, to, fields?) is a relationship Zega can traverse.

Tables in Zega

Every table is also in Zega: each row is a node, kept in step by pg_zega in the same transaction as the Postgres write, and each defineEdge is a relationship between two tables' rows. Nothing to set up or sync by hand.

The exception is a log: a table whose name starts with log_ (log_visits, log_events). Its rows are only ever appended, so it stays in Postgres only and has no updatedAt, just id and createdAt.

Sync, both ways

While simien dev runs:

  • Save schema.ts and the database follows: new tables and fields are added, removed ones dropped. A change that would orphan or break existing data is refused with what's in the way; nothing is half-applied. A rename is applied as a rename, with a warning.
  • Change the database directly (psql, a GUI) and schema.ts is rewritten to match.

Each sync regenerates simien/_generated, the types the app's queries are checked against.

simien:db

// simien/db.ts
import { connect } from "simien:db";
import schema from "./schema";
export const db = connect(schema);

simien:db is server-only: Server Components and Server Actions use it; a client component that imports it fails to build.

await db.posts.findMany({ where: { published: true }, orderBy: { createdAt: "desc" }, limit: 10 });
await db.posts.findFirst({ where: { title: "Hello" } });   // a row, or null
await db.posts.count({ where: { published: false } });
await db.posts.insert({ title: "Hello", published: false });  // the new row
await db.posts.update({ where: { id: 1 }, set: { published: true } });
await db.counters.update({ where: { name: "clicks" }, set: { value: db.inc(1) } });  // atomic
await db.posts.delete({ where: { id: 1 } });
  • columns: { title: true } returns only those fields.
  • update and delete need a where: one that would touch every row is refused.
  • db.inc(n) adds to a number in the database itself, so concurrent increments are never lost.

Filters

A field equal to a value is { field: value }; { field: null } matches null. For more, give the field an object:

operator matches
eq, ne equal, not equal
gt, gte, lt, lte greater, less
in one of a list
contains, startsWith, endsWith text

AND, OR (lists of filters) and NOT (a filter) combine them: { OR: [{ age: { lt: 18 } }, { age: null }] }.

Errors

A failed query throws SimienDataError with a kind:

kind means
invalid the query doesn't fit schema.ts: a field or table that isn't there, a wrong type. The message names it.
database Postgres refused or failed the statement.
refused the app may not use its database here: no database yet, or the db permission isn't granted. The message says how to fix it.

Every query is checked against schema.ts before it reaches Postgres.

Live queries

A query can stay up to date in the browser: see Live queries.