Repository navigation
Database patterns - #1939
Database patterns#1939Alistair Matthews (alistairmatthews) wants to merge 15 commits into
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Migration endpoints need access controls, and the Drizzle deployment guidance and seeding import need corrections.
3 open issues
What changed in this PR
Adds a database recipe guide that brings together common Aspire workflows and links to detailed integration documentation, addressing #1938.
Changes:
- Covers database creation, seeding, migrations, persistence, and deployment.
- Includes C# and TypeScript examples using PostgreSQL.
- Adds the guide under Data and storage.
| File | Description |
|---|---|
src/frontend/src/content/docs/fundamentals/database-patterns.mdx |
Adds database patterns and examples. |
src/frontend/config/sidebar/docs.topics.ts |
Adds sidebar navigation to the guide. |
Review effort: Balanced
David Pine (IEvangelist)
left a comment
There was a problem hiding this comment.
doc-pr-reviewer: Database patterns (head ddad0ad)
Sources (Phase A): microsoft/aspire @ main 3269582d (PR targets main). No Community Toolkit or Azure SDK claims.
Claims: about 45 non-narrative claims checked: 1 contradicted, 1 unverifiable, 3 verified-with-nuance, the rest verified.
Doc-tester (Phase B): /fundamentals/database-patterns/ served locally from the PR head. 1 critical, 3 warnings.
Verdict: Request changes (one contradicted claim, also confirmed empirically).
Phase A: contradicted
Generated server passwords are persisted, not regenerated every run (inline comment on line 648)
By default, Aspire generates a new server password on every run. Set the password as a secret or a parameter, so that the server can still open its existing data on the next run.
microsoft/aspire src/Aspire.Hosting/ParameterResourceBuilderExtensions.cs:487-490 (CreateGeneratedParameter) wraps the generated default in UserSecretsParameterDefault in run mode, and src/Aspire.Hosting/ApplicationModel/UserSecretsParameterDefault.cs saves the value to Parameters:{name} in the AppHost's user secrets. On the next run the value is read back from configuration. AddPostgres uses this path (PostgresBuilderExtensions.cs:60). Saving is best effort (it is skipped when the AppHost has no user secrets ID).
Phase A: unverifiable
- "Aspire creates the databases in your AppHost during the first deployment and doesn't recreate or delete them later." (line 659). I did not find this stated in
microsoft/aspire; the linked pageazure-postgresql-hostmakes the same statement but is documentation, not source. If kept, consider narrowing to the Azure resources it was checked for.
Phase A: verified and verified-with-nuance claims
All paths are microsoft/aspire @ 3269582d.
AddDatabasecreates the database when the server is ready, ignoring "already exists" (Aspire.Hosting.PostgreSQL/PostgresBuilderExtensions.cs:76-97, 805-850).WithCreationScriptexists on the database resource for Postgres, SQL Server and MySQL (PostgresBuilderExtensions.cs:624,SqlServerBuilderExtensions.cs:327,MySqlBuilderExtensions.cs:252). Nuance: it is declared on the database builder, not the server builder; the prose reads as if it were on the server.WithReplexists for Postgres, SQL Server, MySQL, MongoDB and Redis (also Valkey). Openspsqlwith the resource's credentials; run mode only (PostgresBuilderExtensions.cs:146-170).WithInitFiles,WithDataVolume,WithDataBindMount(PostgresBuilderExtensions.cs:532, 563, 599).WithPersistentLifetimeis experimental,ASPIREPERSISTENCE001(ResourceBuilderExtensions.cs:38, 109).WithHttpCommand,HttpCommandOptions.Method/PrepareRequest,CommandOptions.Description/ConfirmationMessage(HttpCommandOptions.cs); TypeScriptHttpCommandExportOptionshas flatmethodName,description,confirmationMessage,prepareRequestreturning{ headers }.AddEFMigrations,RunDatabaseUpdateOnStart(local run mode only),PublishAsMigrationBundle(Aspire.Hosting.EntityFrameworkCore); package is prerelease (13.6.x-preview). Migration resource withoutRunDatabaseUpdateOnStartstill gets dashboard commands (EFResourceBuilderExtensions.cs:742-830). Nuance: the source comment forRunDatabaseUpdateOnStartsays to useWaitFor; the docs and this PR useWaitForCompletion(consistent with the existing migrations page and the documentedFinishedstate).AddJavaScriptApp(name, dir, runScriptName),AddNodeApp(name, dir, scriptPath); TypeScript signatures match the generated data (runScriptNamein options,withHttpEndpoint({ env })).POSTGRES_URI/CATALOGDB_URI:PostgresServerResource.cs:175andPostgresDatabaseResource.cs:74exposeUri.RunAsContaineron Azure PostgreSQL and Azure SQL (AzurePostgresExtensions.cs:220,AzureSqlExtensions.cs:181).- Container lifetime vs data durability matches
/app-host/resource-lifetimes/. Nuance: deployment of the JS migration resource needs explicit publish configuration (PublishAsNodeServer,PublishAsPackageScript), as the PR says. - Third-party:
drizzle-kit generate --custom --name=...,drizzle-seedseed(db, schema, { count }),drizzle(connectionString)match the Drizzle docs. - All 14
<ApiReference>names resolve; all internal links and anchors resolve; no hard-coded Aspire versions.
Phase B: doc-tester report
Focus: /fundamentals/database-patterns/ (and the sidebar entry). Server: pnpm dev at http://localhost:4321/ on PR head ddad0ad8. Tester: playwright-cli.
Critical issues
1. Password behavior is wrong. Location: "Persist a database across restarts", last paragraph before the LearnMore.
The page says a new password is generated on every run. I ran the "Create" and "Persist" C# AppHost snippets from the page (13.6.3 packages) and after the first run Parameters:postgres-password was present in the AppHost's user secrets, so the same password is reused on later runs. Action: reword, for example "Aspire generates the password on first run and stores it in the AppHost's user secrets; set it yourself if you want to control or share it". The linked /fundamentals/persist-data-volumes/#create-persistent-passwords section has the same claim and may need the same fix.
Warnings
- Missing seed endpoint code. "Seed a database on startup or using a custom command" tells the reader to "expose the seed code as an endpoint" but never shows the endpoint, while the migration section shows both Drizzle and EF Core endpoints. A new user cannot complete the
/admin/seedcommand from this page alone. - Drizzle samples depend on unstated setup.
src/schema.ts(products),pg,express,drizzle-orm,drizzle-kitanddrizzle-seedare used but never introduced or installed; Drizzle snippets are fragments. - Table and structure. In the summary table the first column is blank on continuation rows, which loses context when scanning (a
rowspanwould fix it). "Run a script" under "In a dashboard REPL terminal" actually coversWithInitFilesand process-backed commands, which are not REPL features.
Passed checks
- Page renders; sidebar entry "Database patterns" present and active under Data and storage.
- 8 language tab groups render and sync (switching to C# switches all 8; 8 C# blocks visible, 0 TypeScript).
- All 14 API references resolve with C# and TypeScript links.
- All 19 linked pages return 200 and all anchors exist; internal links are site-relative with trailing slashes.
- C# AppHost snippets (create, REPL, seed command, init files, EF migrations, JavaScript migrate, migrate command, persistence) compile together against
Aspire.Hosting.PostgreSQL/JavaScript13.6.3 andEntityFrameworkCore13.6.3-preview (0 errors). - First snippet starts; the Postgres container started and the password was written to user secrets.
Knowledge gaps
- Docker is assumed for all database containers; the page does not say a container runtime is required (reasonable given the rest of the docs, but worth a link to prerequisites).
- Node.js type stripping for
.tsentry points (addNodeApp('api', ..., 'src/index.ts')) requires a recent Node.js; not stated.
Not tested
- TypeScript AppHost snippets were not executed: the installed Aspire CLI is 13.4.0 and the site targets 13.6.x. Their signatures were checked against the generated TypeScript API data instead.
- Drizzle/EF endpoint code was not run against a real database.
- Console errors during testing were Vite dev-server dependency re-optimization noise only (
504 Outdated Optimize Dep), unrelated to the PR.
docs-pr-reviewer report — [partial — phase A: key claims evaluated; phase B: not run (docs site was not served in this automated run)]Recommendation: needs human Source used: microsoft/aspire Phase A — claims
Phase B — doc-testerNot run; rendering of the changed pages is not browser-checked. 🤖 Posted by aspire.dev - squad (AI Agent) |
|
I noticed in Discord that devs wanted to know how to run EF Migrations as part of a deployment and fail the deployment if the migration failed. This is an ideal location to cover that. So I added a section to the deployment discussion about it. Checked with both doc-writer and doc-tester skills. Discord discussion is here: https://discord.com/channels/1361488941836140614/1485725414222598305 |

Summary
Adds an Aspire Database patterns document in Foundations/Model your app/Data and storage.
The document is intended as a recipe book for common database scenarios within Aspire. Tasks include:
For each of these, methods may include:
Where a task and method is already documented, it is described and then the existing location is linked to. Otherwise a complete example is given including relevant code or commands.
Fixes: #1938
Third-party links and affiliations
None
Validation
Checked with doc-tester skill.