Start in ten minutes · The Plain Web Stack · diadem.fun
The Plain Web Stack10 / 15

Start in ten minutes

You need Bun 1.4 or newer.

mkdir my-app && cd my-app
bun init -y
bun add tradjs sqlite-zod-orm measure-fn
mkdir app
// app/page.tsx
import { db } from "../db";
export default function Home() {
  const notes = db.notes.select().orderBy("id", "desc").all();
  return <main>
    <h1>Notes</h1>
    <form method="post" action="/api/notes"><input name="text" required /><button>Add</button></form>
    <ul>{notes.map((n) => <li key={n.id}>{n.text}</li>)}</ul>
  </main>;
}
// db.ts
import { Database, z } from "sqlite-zod-orm";
export const db = new Database("app.db", { notes: z.object({ text: z.string() }) });
// app/api/notes/route.ts
import { db } from "../../../db";
export async function POST(req: Request) {
  db.notes.insert({ text: String((await req.formData()).get("text")) });
  return new Response(null, { status: 303, headers: { Location: "/" } });
}

Set JSX to TradJS in tsconfig.json:

{ "compilerOptions": { "jsx": "react-jsx", "jsxImportSource": "tradjs/client" } }

Run bunx tradjs serve and open the address it prints (http://localhost:3000, or the next free port if 3000 is taken). Add a note: it is stored in app.db and listed after the redirect. This exact app was run while writing this chapter. From here:

  • add app/layout.tsx for the page shell;
  • add app/middleware.ts for sessions;
  • add page.client.tsx where a page needs interaction. The coming starter, tradjs init --template app, sets all of this up with accounts, sessions, editing, pages and live updates. Source and documentation
  • TradJS: https://github.com/7flash/TradJS
  • sqlite-zod-orm: https://github.com/7flash/sqlite-zod-orm
  • measure-fn: https://github.com/7flash/measure-fn