Storage bindings
Store links in D1
Create a D1 database and migration, then use prepared statements and bound values for Link Vault CRUD operations.
8 minute lesson
D1 provides managed SQLite semantics through a Worker binding. It fits Link Vault’s relational link records and queries.
Create the database, generate a migration, apply it locally, and use prepare().bind() for every request value. Keep local and remote migration commands explicit; a migration applied locally has not changed production.
npx wrangler d1 create link-vault
npx wrangler d1 migrations create link-vault create-links
npx wrangler d1 migrations apply link-vault --local
Make constraints carry business invariants that should survive every code path: primary keys, required fields, and any uniqueness rule belong in the schema. Prepared statements with bound values separate SQL from user input, but they do not replace URL validation or authorization.
Keep migration state explicit. Apply migrations to an isolated local database in tests, then use the remote command only after confirming the account, environment, and database name. Prefer additive changes during a deployment window so old and new Worker versions can both operate. If a change cannot be reversed, define a data recovery step before applying it rather than calling a code rollback the whole plan.
Create links with ID, URL, title, created timestamp, and archived flag, then implement list and create routes.
Lesson completed