Course lesson 15 · Smart Routing Service
Connecting our database
The data service reuses the existing Data Ops get-link query to read the same configuration created through the dashboard. The lesson connects that reusable code to this Worker’s own D1 binding and initializes the shared database accessor in the WorkerEntrypoint constructor. It also demonstrates how a missing await can hide the real error by returning before the asynchronous query finishes.
Primary source: course video 15 · Supplied English subtitles · 00:08:08.
Understand the idea
- The dynamic Hono path parameter supplies the link ID used by the shared database query.
- Importing query code does not itself connect this separate Worker to D1; each deployed service needs its own binding configuration.
- Remote development selects the hosted database used by the dashboard rather than a separate local SQLite file.
- The shared database initialization must run before query functions access it.
- The constructor forwards its execution context and environment through super, then performs the demonstrated dependency initialization before handlers execute.
What the course does
- Read the ID through Hono’s request-param helper and call the imported get-link query.
- Copy the intended D1 configuration into the data service and regenerate Cloudflare environment types.
- Run locally against a link ID that already exists in the user application’s database.
- Add the missing await so failures occur in the request’s actual control flow.
- Diagnose database-not-initialized and initialize the Data Ops accessor from the constructor’s D1 environment binding.
- Repeat the request and confirm that the returned JSON contains the stored link configuration.
Watch for
- A Promise that has not been awaited is not the query result; returning too early can obscure the initialization failure.
- The experimental remote option is recording-specific and must match the tooling version used.
- Added advice: the shared module-level accessor is an architectural choice in the sample. Review initialization and concurrency carefully before using it with varying per-request database bindings.
Recall before revealing
Why did importing a working query from the dashboard package not make it immediately work in the data service?
Reveal the explanation
The separate Worker still needed its D1 resource binding and the shared database accessor initialized for its own execution path.
Try it
Use a link created through the dashboard to retrieve its configuration through the data-service ID route.
Check your result
The returned name and destinations match D1, and you can explain the binding and initialization steps that made the shared query work.
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:01:01 — Extract the ID and reuse get-link
- 00:03:01 — Configure the data service D1 binding
- 00:05:02 — Missing await reveals the initialization error
- 00:06:00 — Initialize the database in the constructor
Companion primary documentation: Cloudflare resource bindings. For recorded API names, commands, limits, and prices, check the version you use.