Course lesson 09 · Building in a Mono Repo
Creating SQL Queries in our Mono Repo
This lesson implements the first real end-to-end write: submitting a link form creates a database row and returns its generated short-link ID. A validated tRPC mutation calls a reusable Data Ops query, which gets the initialized Drizzle database and inserts the row. The debugging sequence makes the difference between package exports, runtime initialization, resource bindings, and local versus remote data concrete.
Primary source: course video 09 · Supplied English subtitles · 00:22:23.
Understand the idea
- Zod describes the accepted create-link input, and its inferred TypeScript type is reused by the query function.
- The account ID is supplied through server context rather than being part of the browser’s create-link input; authentication remains a placeholder here.
- The query generates a short ID, stores destination configuration as serialized JSON, and returns the new ID.
- Package exports and workspace dependencies make the built query available to the user application.
- The course initializes a shared database accessor when the Worker receives a request, before tRPC invokes database operations.
What the course does
- Trace the Create button to its client mutation and matching server procedure.
- Implement the create-link query in Data Ops using the generated links schema, then rebuild the package.
- Replace the dummy mutation result with the query call and supply the account information from context.
- Diagnose the missing-initialization error, add the D1 binding, regenerate types, and initialize the database at the Worker entrypoint.
- Distinguish an empty local SQLite database from the hosted D1 database; enable the course’s remote development integration.
- Verify the new row in D1 and return its actual generated ID so navigation opens the correct link page.
Watch for
- The Vite and Wrangler experimental remote flags are version-specific.
- Added advice: the module-level database accessor is the course pattern; review concurrent requests and environment boundaries before extending it to multiple database tenants.
- Forgetting to return the created ID leaves the frontend navigating to the dummy identifier.
Recall before revealing
Why can a D1 query still fail after its binding has been declared?
Reveal the explanation
The shared Drizzle accessor may not be initialized, or development may be using a separate local database whose tables were never created.
Try it
Create a link and follow its ID from form submission to the database row and the page URL.
Check your result
The stored destination and name match the form, and the final page URL contains the ID actually returned by the database operation.
Practice prompts and answer checks are added teaching material. Answer from memory first, then compare and explain any difference.
Rewatch only what you need
Open this lesson in your original course library and seek to the times below. Videos are not hosted on this site.
- 00:03:00 — Shared input schema and query creation
- 00:06:00 — Database initialization and accessor
- 00:14:01 — Diagnose database not initialized
- 00:18:01 — Remote database integration
- 00:21:00 — Return the actual generated link ID
Companion primary documentation: Drizzle with Cloudflare D1. For recorded API names, commands, limits, and prices, check the version you use.