Course lesson 30 · Building AI Workflows
Saving AI output in our DB
This lesson connects the AI classification to persistent application data. Sampled video frames show a database-save workflow step, shared evaluation queries, and a fix that initializes the database inside the workflow entrypoint. The final D1 Studio frame shows an evaluation saved with UNKNOWN_STATUS, preserving uncertainty instead of silently treating it as an available product. This summary covers visible implementation; the narration was unavailable.
Primary source: course video 30 · Sampled video frames; narration unavailable · 00:09:20.
Understand the idea
- An evaluation combines the model's status and reason with the link ID, account ID, and destination URL so the application can attribute and display the result.
- A shared query helper inserts the evaluation and returns its generated ID; the workflow can use that ID in later work.
- Initializing the database in the ordinary Worker entrypoint does not establish initialization in the workflow entrypoint. The video adds initDatabase(this.env.DB) inside run().
- The visible read queries scope evaluations by account, support a list of unavailable products, and order general evaluation history by creation time.
What the course does
- Add a named database-save step after the AI classification and pass the workflow payload identifiers plus the model's status and reason.
- Implement addEvaluation in the shared data-ops package, insert into destinationEvaluations, and return the ID.
- Add account-filtered read helpers for unavailable evaluations and evaluation history, then rebuild the shared package for the consuming service.
- Inspect the deployed workflow's failed save step and the database initialization code; add initialization at the start of the workflow run method.
- Check the completed step history and query destination_evaluations in D1 Studio to verify the saved record.
Watch for
- Evidence limitation: sampled 28 frames at 20-second intervals from 00:00:05 to 00:09:05; no subtitle file or narration transcript. Intermediate edits and the precise explanation of the failure may be missing.
- Visible code caveat: the createdBefore filter uses gt while sorting descending. Added advice: test that pagination actually returns older rows; do not copy that comparison blindly.
- Later course correction: lesson 34 moves ID generation and R2 artifact storage earlier and passes the existing ID into the final database save. Use that patched ordering when completing the workflow.
Follow the final workflow correction in lesson 34 when combining these steps.
Recall before revealing
Why might the workflow fail to save when the ordinary Worker already initializes the database?
Reveal the explanation
The workflow has its own entrypoint and execution path. Its database helper must be initialized with the binding available to that path; initialization in another entrypoint cannot be assumed.
Try it
Sketch the fields in a saved evaluation and mark which come from the workflow payload and which come from the AI result. Add where database initialization occurs.
Check your result
Link/account/destination identifiers come from the payload, status/reason come from the model result, and the workflow initializes its database access before querying.
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:45 — Shared insert helper generates an ID and writes evaluation fields
- 00:02:25 — Account-filtered history query and the createdBefore comparison
- 00:03:05 — Workflow database-save step passes model output and identifiers
- 00:07:05 — Database initialization added inside the workflow run method
- 00:09:05 — D1 Studio displays the persisted UNKNOWN_STATUS evaluation
Companion primary documentation: Drizzle with Cloudflare D1. For recorded API names, commands, limits, and prices, check the version you use.