Skip to content

Database patterns - #1939

Open
Alistair Matthews (alistairmatthews) wants to merge 15 commits into
microsoft:mainfrom
alistairmatthews:database-patterns
Open

Alistair Matthews (alistairmatthews) wants to merge 15 commits into
microsoft:mainfrom
alistairmatthews:database-patterns

Conversation

@alistairmatthews

@alistairmatthews Alistair Matthews (alistairmatthews) commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

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:

  • Creating a database
  • Seeding DBs with data.
  • Migrating schema changes.
  • Deploying databases.
  • Ensuring that ORM migrations run successfully during deployments.

For each of these, methods may include:

  • Use code in the AppHost
  • Use code in an orchestrated app
  • Use a script in a REPL prompt

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

@IEvangelist David Pine (IEvangelist) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 page azure-postgresql-host makes 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.

  • AddDatabase creates the database when the server is ready, ignoring "already exists" (Aspire.Hosting.PostgreSQL/PostgresBuilderExtensions.cs:76-97, 805-850).
  • WithCreationScript exists 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.
  • WithRepl exists for Postgres, SQL Server, MySQL, MongoDB and Redis (also Valkey). Opens psql with the resource's credentials; run mode only (PostgresBuilderExtensions.cs:146-170).
  • WithInitFiles, WithDataVolume, WithDataBindMount (PostgresBuilderExtensions.cs:532, 563, 599).
  • WithPersistentLifetime is experimental, ASPIREPERSISTENCE001 (ResourceBuilderExtensions.cs:38, 109).
  • WithHttpCommand, HttpCommandOptions.Method/PrepareRequest, CommandOptions.Description/ConfirmationMessage (HttpCommandOptions.cs); TypeScript HttpCommandExportOptions has flat methodName, description, confirmationMessage, prepareRequest returning { headers }.
  • AddEFMigrations, RunDatabaseUpdateOnStart (local run mode only), PublishAsMigrationBundle (Aspire.Hosting.EntityFrameworkCore); package is prerelease (13.6.x-preview). Migration resource without RunDatabaseUpdateOnStart still gets dashboard commands (EFResourceBuilderExtensions.cs:742-830). Nuance: the source comment for RunDatabaseUpdateOnStart says to use WaitFor; the docs and this PR use WaitForCompletion (consistent with the existing migrations page and the documented Finished state).
  • AddJavaScriptApp(name, dir, runScriptName), AddNodeApp(name, dir, scriptPath); TypeScript signatures match the generated data (runScriptName in options, withHttpEndpoint({ env })).
  • POSTGRES_URI / CATALOGDB_URI: PostgresServerResource.cs:175 and PostgresDatabaseResource.cs:74 expose Uri.
  • RunAsContainer on 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-seed seed(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

  1. 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/seed command from this page alone.
  2. Drizzle samples depend on unstated setup. src/schema.ts (products), pg, express, drizzle-orm, drizzle-kit and drizzle-seed are used but never introduced or installed; Drizzle snippets are fragments.
  3. Table and structure. In the summary table the first column is blank on continuation rows, which loses context when scanning (a rowspan would fix it). "Run a script" under "In a dashboard REPL terminal" actually covers WithInitFiles and 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/JavaScript 13.6.3 and EntityFrameworkCore 13.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 .ts entry 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.

Copy link
Copy Markdown
Member

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 main (API surface only)
PR status: non-draft, new article by a docs author (alistairmatthews), targets main, mergeable, opened 2026-10-21 (not stale); 2 files.

Phase A — claims

  • ✅ verified: AddPostgres, AddDatabase, WithDataVolume, WithDataBindMount, WithInitFiles, WithCreationScript, WithRepl (PostgresBuilderExtensions), WithPersistentLifetime, AddEFMigrations and RunDatabaseUpdateOnStart exist.
  • ❓ The article is long new narrative guidance (seeding, EF Core migrations, deployment patterns). Behavioral claims, the decision table and both language samples need an editorial/technical review by a human; this run only checked API names.

Phase B — doc-tester

Not run; rendering of the changed pages is not browser-checked.

🤖 Posted by aspire.dev - squad (AI Agent)

@alistairmatthews

Copy link
Copy Markdown
Collaborator Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Database patterns examples

3 participants