🐟 TunaScript Playground 🍣
Sample
TODOリスト
01 ユニオン型とswitch
02 ?演算子のエラーハンドリング
03 JSONのパース
Format & Run
import { log } from "prelude" import { map } from "array" import { create_server, add_route, listen, response_html, type Request, type Response, type JSX } from "http" // create_table文でTODOリスト用のテーブルを定義します create_table todo { id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, scheduled_at TEXT NOT NULL, completed TEXT NOT NULL } // ひとつのタスクを描画するコンポーネント // create_tableで定義したテーブルは、同名の型としても定義されます。 function TodoListItem(props: { row: todo }): JSX { const { row } = props const { title, scheduled_at, completed } = row return
{title}
{scheduled_at}
} function root(req: Request): Response { // データベースからTODOリストを取得します const fetched = fetch_all { SELECT id, title, scheduled_at, completed FROM todo ORDER BY id } // swtich式でユニオン型に応じて処理を分岐します return switch (fetched) { // エラーが発生した場合 case e as error: response_html(
DB error: {e.message}
) // 正常に取得できた場合 case rows as todo[]: { // HTMLレスポンス全体をレンダリングします response_html(
SQL TODO DEMO
🐟TunaScript TODO List🍣
{map(rows, function (row) { return
})}
) } } } export function main(): void | error { // サンプルデータをデータベースに追加しておきます execute { INSERT INTO todo (title, scheduled_at, completed) VALUES ('買い物', '2026-02-05 10:00', '0'), ('犬の散歩', '2026-02-05 18:30', '1'), ('ランチ会', '2026-02-06 12:00', '0') }? // Playgroundでは、ルートのページだけ出力されます const server = create_server() server.add_route("get", "/", root) server.listen(":8888") // log関数で標準出力できます log("Server is running at http://localhost:8888/") }
Stdout
exitCode :0
Server is running at http://localhost:8888/
HTML Preview