Course lesson 08 · Building in a Mono Repo
Setting up D1 Database & Drizzle ORM
The course creates a D1 database and uses Drizzle to represent its tables as TypeScript schemas. Schema and query code lives in Data Ops so both applications can share it, and database initialization is centralized to reduce provider-specific setup scattered through the apps. This lesson uses a database-first workflow: create tables remotely, then introspect them into code.
Primary source: course video 08 · Supplied English subtitles · 00:17:49.
Understand the idea
- D1 supplies the SQL database used for this project; Drizzle adds typed schema and query-building support.
- The links table stores configuration, link clicks stores event data for analytics, and destination evaluations records background health-check results.
- Indexes support the anticipated lookup/filter patterns, with the instructor noting the read-versus-write tradeoff.
- Drizzle Kit’s pull command reads the database structure and produces schema files; it does not create meaningful application rows.
- Drizzle tooling uses an account ID, database ID, and API token, which is a different connection path from a Worker’s D1 binding.
What the course does
- Create a Smart Links database with a stage suffix to prepare for later environment separation.
- Put the account and database identifiers and a D1-capable API token in the Data Ops tooling environment file.
- Configure Drizzle output paths and run an initial pull to confirm connectivity.
- Use the database query interface and supplied course SQL to create links, link clicks, indexes, and destination evaluations.
- Pull again and inspect the generated TypeScript schemas in the shared package.
Watch for
- The D1 size limit, allowances, Workers for Platforms claims, and database suitability recommendations describe the recording’s products and opinions.
- The instructor says to exclude the environment file from Git and save the token securely.
- Added advice: centralizing an ORM connection reduces migration work, but changing database providers can still require schema, SQL, and operational changes.
Recall before revealing
Why is the first schema pull empty, and why does the second contain tables?
Reveal the explanation
The first runs before any tables exist; the second introspects the tables just created in D1.
Try it
Create the course’s three tables in a dedicated study database and explain what event or action writes to each one.
Check your result
The generated schemas match the database tables, and you can distinguish link configuration, click history, and evaluation results.
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:01 — Why introduce Drizzle and shared queries
- 00:08:01 — Drizzle configuration and introspection
- 00:12:00 — Create the D1 tooling token
- 00:14:02 — Create application tables and indexes
- 00:17:00 — Pull the resulting schemas
Companion primary documentation: Drizzle with Cloudflare D1. For recorded API names, commands, limits, and prices, check the version you use.