SQLite CRUD (ORM + migrations)

defineModel gives a typed repository over bext's embedded SQLite; migrate() applies a tracked, idempotent schema (no more lazy CREATE TABLE IF NOT EXISTS). The loader reads via the query builder, the action writes via the model, and a 303 redirect re-runs the loader so the list reflects the mutation.

Tip
The model returns real typed objects — it zips the bridge's positional rows for you, so no more .columns/.rows juggling. migrate() is idempotent: safe to call every request, it runs only the migrations missing from the _bext_migrations ledger.

Add a note

Notes (20)

smoke-note2026-12-05 17:02:53
smoke-note2026-12-04 23:20:08
smoke-note2026-12-04 19:37:32
smoke-note2026-12-04 00:17:21
smoke-note2026-12-04 00:09:58
smoke-note2026-12-03 12:32:08
smoke-note2026-12-02 14:52:33
smoke-note2026-12-02 14:22:17
smoke-note2026-12-02 13:32:23
smoke-note2026-12-02 13:18:35
smoke-note2026-12-01 19:40:52
smoke-note2026-11-27 21:38:20
smoke-note2026-11-23 15:48:57
smoke-note2026-11-23 10:28:04
smoke-note2026-11-22 19:28:45
smoke-note2026-11-22 11:52:51
smoke-note2026-11-22 11:22:29
smoke-note2026-11-22 10:04:08
smoke-note2026-11-21 20:29:45
smoke-note2026-11-21 19:35:14
src/app/examples/db-crud/page.tsxTSX
import { defineModel } from "@bext-stack/framework/orm";
import { migrate } from "@bext-stack/framework/migrate";

// Tracked, idempotent schema — no lazy CREATE TABLE IF NOT EXISTS.
const migrations = [{
  id: "0001_create_notes",
  up: (db) => db.schema.createTable("notes", (t) => {
    t.id();
    t.text("body").notNull();
    t.timestamps();
  }),
  down: (db) => db.schema.dropTable("notes"),
}];

const Notes = defineModel<Note>({ table: "notes", db: ".bext/data/notes.db", timestamps: true });

export async function loader() {
  migrate({ db: DB }, migrations);            // applies pending only
  const notes = Notes.query().orderBy("id", "desc").limit(20).all();
  return { notes };                            // real typed objects
}

export async function action({ request }) {
  const form = await request.formData();
  if (form.get("op") === "add")    Notes.create({ body: form.get("body") });
  if (form.get("op") === "delete") Notes.delete(Number(form.get("id")));
  return new Response(null, { status: 303, headers: { Location: "/examples/db-crud" } });
}