Appearance
A list with live data
A shopping list everyone shares, kept in SQLite with std/db. The page reads it with a live query: when anyone's write changes the list, the server sends every open page the difference.
db/schema.weft
weft
import { table, schema, primary, default, index, public } from "std/db"
import type { Principal } from "std/http"
export type Item = {
@primary @default(uuid) id: string,
name: string,
@index @default(false) bought: boolean,
@default(now) added: number,
}
// Anyone may read and change the list; see Data for policies per user.
export let items = table<Item, Principal>("items", { read: public(), write: public() })
export let app = schema({ tables: { items } })lib/server/items.weft
weft
import { sqlite, push, identify } from "std/db"
import { app, items } from "db/schema"
import type { Item as Row } from "db/schema"
/// A row of the list, as the page sees it.
export type Item = Row
export type ItemName = string where it.trim() != "" && it.length <= 80
// In data/, which `weft dev` doesn't watch. `push` applies the schema at start (for development).
let pool = try await sqlite(app, "data/list.db")
try await push(pool)
// Everyone shares this list: no one signs in.
identify(pool, async () => null)
export query fn listItems(): Item[] {
return try await pool.query.items().sortBy(i => i.bought).sortBy(i => i.added)
}
export mutation fn addItem(name: ItemName): void {
try await pool.insert.items({ name: name.trim() })
}
export mutation fn setBought(id: string, bought: boolean): void {
try await pool.update.items({ id }, { bought })
}
export mutation fn clearBought(): void {
try await pool.delete.items({ bought: true })
}routes/page.weft
weft
import { plural } from "std/format"
import { listItems, addItem, setBought, clearBought } from "lib/server/items"
import type { ItemName } from "lib/server/items"
let mut draft = ""
derived items = await listItems()
derived left = (items.value ?? []).count(i => !i.bought)
async fn add() {
let name, bad = try draft as ItemName
if bad != null { return }
let _, err = try await addItem(name)
if err != null { return } // keep the draft to try again
draft = ""
}
// A handler can't fail: a write that fails changes nothing, and the list keeps showing what is stored.
async fn toggle(id: string, bought: boolean) {
try await setBought(id, bought) else null
}
async fn clear() {
try await clearBought() else null
}
---
<Card footer className="w-96">
<CardHeader>
<CardTitle>Shopping</CardTitle>
<CardDescription>{plural(left, "thing")} to get</CardDescription>
</CardHeader>
<CardContent className="flex flex-col gap-3">
<Box class="flex gap-2">
<Input bind:value={draft} onsubmit={add} placeholder="Milk" />
<Button onpress={add}>Add</Button>
</Box>
@for item of items.value ?? [] key item.id {
<Checkbox checked={item.bought} label={item.name} onchange={() => toggle(item.id, !item.bought)} />
} @empty {
<Text class="text-sm text-muted-foreground">Nothing on the list.</Text>
}
</CardContent>
<CardFooter>
<Button variant="ghost" size="sm" onpress={clear}>Clear bought</Button>
</CardFooter>
</Card>listItemsis aquery fn, so awaiting it in a derived value keeps the call open: it is live.std/dbrecords what the query read; a committed write that could change its result (here, any write toitemsthat matches) runs it again, and only the rows that changed are sent, by their@primarykey.- Handlers can't fail, so each write handles its error where it is called (
try … else nulldrops it here;addkeeps the draft to try again). - The writes are
mutation fns and return nothing: every page showing the list, this one included, gets the change through the live query when the mutation commits. Two tabs side by side show each other's changes as they commit. - The page renders on the server with the list already in it, and the browser continues the live call from that version: no second fetch, no flash of an empty list.
itemsis anAsync<Item[]>, so the markup readsitems.value. A live list of records needs a key: the compiler rejects one whose records have neither a@primaryfield nor anid.
sh
mkdir data && weft dev .For production, weft db migrate writes migrations instead of push (Data). On Postgres, live = true in weft.toml's [db] also installs triggers, so writes from outside the app reach live pages too. How it works: std/live.