Cloudflare tutor

A morning desk for Kotha · Gold Coast

· Australia/Brisbane · Lesson

D1: a small SQL database for your Workers

D1 (SQL database)

What it is

D1 is Cloudflare’s serverless SQL database, built on SQLite. You get real tables, rows, columns, and SQL queries such as SELECT, INSERT, UPDATE, and DELETE, all reachable from a Worker without running your own database server.

Like KV, a D1 database is attached to a Worker through a binding. In wrangler.toml that is a [[d1_databases]] block with a binding name (for example DB), the database name, and its database_id. In the script you then write something like env.DB.prepare("SELECT * FROM notes WHERE id = ?").bind(id).first(). The ? placeholder plus .bind() keeps user input out of the SQL text itself.

You change the shape of a D1 database with SQL too: CREATE TABLE, ALTER TABLE, and so on. Wrangler can run these as numbered migration files (wrangler d1 migrations create, then apply), so the schema history lives next to your code. The dashboard also has a console where you can type a query and see the result.

Why it matters

Yesterday’s lesson said KV is good for simple keyed values and poor for queries across rows. D1 is the other half of that choice. When an app needs to ask questions such as “all entries from this week, newest first” or “count by status”, you want SQL, and D1 gives you that on the same account and bill as your Workers.

Many of your small GP and personal apps are really lists of records: notes, tasks, events, heartbeat checks, or log lines. Those fit naturally in a D1 table. Keeping the data in Cloudflare next to the Worker also means one less outside service to sign in to and keep alive.

D1 is still SQLite, so it suits small and medium apps, not a large shared clinical system. Never put identifiable patient data in a learning database; keep lessons to made-up rows.

On your account

This tutor (cloudflare-tutor on cloudflare.kosan.xyz) has no database at all; the lessons are an array in worker.js, and its wrangler.toml has no [[d1_databases]] block. That stays true after today.

A typical small kosan.xyz app that keeps records, such as a heartbeat or uptime checker, would bind one D1 database as DB, keep a table like checks (id, site, status, checked_at), and run INSERT on each check plus a SELECT for the page that lists the latest results. That is exactly the kind of “rows and queries” job that would be awkward in KV.

The pattern is: one database per app, bound only to that app’s Worker, with its tables created by a migration. Nothing in this lesson creates, binds, or changes a database on your account.

Do this yourself

These stay in the dashboard, on your side. This site does not change DNS, routes, or other Workers.

  1. In the Cloudflare dashboard, open Storage & Databases → D1 (the menu wording may differ slightly) and note which databases already exist, read only. Then, as a throwaway only, create a database named tutor-d1-lesson, open its Console, run CREATE TABLE notes (id INTEGER PRIMARY KEY, body TEXT); then INSERT INTO notes (body) VALUES ('lesson7'); then SELECT * FROM notes; to see your row, and finally delete that same tutor-d1-lesson database. Do not bind it to, or touch the databases of, cloudflare-tutor, ainews, aiweekly, or any live app.