GitHub

Your first app

This walks through what Simien does for a new app, one step at a time, with what you should see after each. You need Docker running for the database. Every command prints JSON by default, for agents; --human prints it for people, as below.

1. Create the app

simien init my-app --human
✓ created my-app from https://github.com/tanaframework/template-default.git
  packages   228 installed
  → start it: cd my-app && simien dev

my-app is a Tana app: app/ holds its pages, simien/schema.ts its tables and simien.json what it is allowed to do. The template already grants the app its database:

{
  "name": "my-app",
  "security": {
    "allow": { "db": ["postgres"] },
    "deny": { "wasm": true, "run": true }
  },
  "db": { "ports": { "postgres": 54332, "zega": 54333 } }
}

The ports are the first free pair Simien found on your machine, kept for this project from now on.

2. Run it

cd my-app
simien dev --human
[database] starting Postgres + pg_zega and Zega…
[ready] zega
[ready] postgres
  VITE v8.3.5  ready in 2348 ms
  ➜  Local:   http://127.0.0.1:5173/
  Postgres   postgres://postgres@127.0.0.1:54332/postgres
  Zega       http://127.0.0.1:54333
[watch] simien/schema.ts (Ctrl+C to stop)
✓ synced
    → add table counters

The app's own Postgres and Zega started in Docker, the counters table from schema.ts was created in both, and the app is running at http://127.0.0.1:5173. The first start downloads the two database images; after that it takes seconds.

3. Watch a query stay live

Open http://127.0.0.1:5173 in two tabs and press Click in one. The count changes in the other too, without a reload.

Three files make that happen. The table, in simien/schema.ts:

export default defineSchema({
  counters: defineTable({ name: v.string(), value: v.int() }),
});

A live query, in app/queries.ts. On the server it is just the query; live() also records which tables it read:

export const clicks = live(async () =>
  (await db.counters.findFirst({ where: { name: "clicks" } })) ??
  (await db.counters.insert({ name: "clicks", value: 0 })),
);

And the page, in app/counter.tsx, which subscribes to those tables:

const counter = useQuery(clicks) ?? initial;

The button is an ordinary Server Action (app/actions.ts) that adds one with db.inc(1), so two clicks at the same moment are never lost. Any write to counters re-runs the query in every tab, even one made outside the app:

psql postgres://postgres@127.0.0.1:54332/postgres \
  -c "UPDATE counters SET value = 41 WHERE name = 'clicks'"

Both tabs show 41. How it works, and its limits today: Live queries.

4. Add a table

Add a todos table to simien/schema.ts and save:

export default defineSchema({
  counters: defineTable({ name: v.string(), value: v.int() }),
  todos: defineTable({ text: v.string(), done: v.boolean() }),
});

simien dev creates it right away:

✓ synced
    → add table todos

It also regenerates simien/_generated, so db.todos.insert({ text: "…", done: false }) is type-checked against the new table straight away.

5. Change the database, and the schema follows

Sync runs the other way too. Add a column with psql (or any database GUI):

psql postgres://postgres@127.0.0.1:54332/postgres -c "ALTER TABLE todos ADD COLUMN due bigint"
[pull] simien/schema.ts rewritten from a database change (ALTER TABLE)
● [sync] database matches schema.ts

schema.ts now has due: v.optional(v.int()) on todos. (Today that rewrite also drops the file's comments: runtime#110.)

6. Check and build

simien check --human     # ✓ no type errors
simien build --human     # Build complete.

simien build runs the app's build script and writes dist/client (static files) and dist/server/worker.js (the server, a standard export default { fetch } module).

7. Serve the build

simien serve dist/server/worker.js --assets dist/client --port 8788

That is the app as production runs it: the server module, its static files, the same database and the same live queries, under the same simien.json rules. Open http://localhost:8788 and the count is still 41.

Stop

Ctrl+C stops simien dev. The database keeps running, so the next start is instant; simien container stop stops it, and the data stays in Docker volumes until simien container reset --yes.

Next

  • The database: every table type, filters, errors and simien:db's whole API.
  • Permissions: what simien.json can allow, and how packages are sandboxed.
  • CLI: every command.