Course lesson 40 · Advanced Durable Objects
Build a SQL backed Durable Object
The tracker becomes a SQLite-backed Durable Object with a small geographic click table and an insertion method. A temporary HTTP endpoint reads the table so storage can be verified before WebSockets are introduced. The redirect’s background helper now enqueues the original analytics event and then writes usable geographic data to the correct account object.
Primary source: course video 40 · Supplied English subtitles · 00:16:36.
Understand the idea
- The object obtains its SQL interface from context storage and initializes its table when the instance starts.
- The lesson wraps initialization in blockConcurrencyWhile so subsequent object work does not run ahead of initialization.
- SQL parameter bindings supply values to placeholders; they are different from Cloudflare resource bindings.
- A stub is the handle used to invoke an identified Durable Object, including its fetch handler.
- Each account name maps to its own object and consequently its own SQLite contents.
What the course does
- Create the tracker class and a table containing latitude, longitude, country, and time.
- Implement a parameterized insert method and a temporary query that returns up to 100 rows as JSON.
- Export the class, add its Durable Object binding and SQLite-class migration, then regenerate environment types.
- Extract the redirect’s background work into a helper that first sends the queue message.
- Resolve the account’s object, require usable geographic fields, and call its insert method.
- Proxy a temporary Hono route to the object, deploy, click a redirect link, and inspect the returned data.
Watch for
- The instructor corrects a query ordering by a nonexistent ID and changes the migration to new SQLite classes before deploying.
- Geographic metadata may be absent; the helper skips map tracking when required fields are missing.
- The temporary query endpoint is a teaching scaffold, and the instructor’s Drizzle migration difficulties describe their experience at recording time.
Recall before revealing
What determines whether two clicks share the same tracker database?
Reveal the explanation
They share it when their links belong to the same account and therefore resolve to the same Durable Object identity.
Try it
Explain how you would test account isolation using the temporary endpoint.
Check your result
You predict that clicks for account A appear under A’s endpoint while another account’s object remains separate.
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:02:17 — Create the geographic click table
- 00:04:29 — Parameterized SQL bindings
- 00:10:22 — Queue-first background capture
- 00:13:21 — Correct SQLite class migration
Companion primary documentation: What are Durable Objects?. For recorded API names, commands, limits, and prices, check the version you use.