diff --git a/src/frontend/config/sidebar/docs.topics.ts b/src/frontend/config/sidebar/docs.topics.ts index d7741f947..a660c11ed 100644 --- a/src/frontend/config/sidebar/docs.topics.ts +++ b/src/frontend/config/sidebar/docs.topics.ts @@ -840,6 +840,10 @@ export const docsTopics = { 'zh-CN': '数据与存储', }, items: [ + { + label: 'Database patterns', + slug: 'fundamentals/database-patterns', + }, { label: 'Persist data with volumes', slug: 'fundamentals/persist-data-volumes', diff --git a/src/frontend/src/content/docs/app-host/resource-lifetimes.mdx b/src/frontend/src/content/docs/app-host/resource-lifetimes.mdx index 1cb959939..a73dab002 100644 --- a/src/frontend/src/content/docs/app-host/resource-lifetimes.mdx +++ b/src/frontend/src/content/docs/app-host/resource-lifetimes.mdx @@ -512,5 +512,5 @@ await postgres.withDataVolume(); For **caches or other ephemeral state**, `WithPersistentLifetime()` alone may be sufficient because losing data on container recreation is acceptable. :::tip -For more details on volumes and bind mounts, see [Persist data using volumes](/fundamentals/persist-data-volumes/). +For more details on volumes and bind mounts, see [Persist data using volumes](/fundamentals/persist-data-volumes/). For how persistence affects database seed data and initialization scripts, see [Persist a database across restarts](/fundamentals/database-patterns/#persist-a-database-across-restarts). ::: diff --git a/src/frontend/src/content/docs/fundamentals/custom-resource-commands.mdx b/src/frontend/src/content/docs/fundamentals/custom-resource-commands.mdx index ae4daba42..0bf46d82b 100644 --- a/src/frontend/src/content/docs/fundamentals/custom-resource-commands.mdx +++ b/src/frontend/src/content/docs/fundamentals/custom-resource-commands.mdx @@ -12,7 +12,7 @@ import customClearCacheCommandSucceeded from '@assets/fundamentals/custom-clear- import customClearCacheCommandSucceededLight from '@assets/fundamentals/custom-clear-cache-command-succeeded-light.png'; import ApiReference from '@components/ApiReference.astro'; -Each resource in the Aspire app model is represented as an `IResource` and when added to the distributed application builder, it's the generic-type parameter of the `IResourceBuilder` interface. You use the _resource builder_ API to chain calls, configuring the underlying resource, and in some situations, you might want to add custom commands to the resource. Some common scenario for creating a custom command might be running database migrations or seeding/resetting a database. In this article, you learn how to add a custom command to a Redis resource that clears the cache. +Each resource in the Aspire app model is represented as an `IResource` and when added to the distributed application builder, it's the generic-type parameter of the `IResourceBuilder` interface. You use the _resource builder_ API to chain calls, configuring the underlying resource, and in some situations, you might want to add custom commands to the resource. Some common scenario for creating a custom command might be running database migrations or seeding/resetting a database. For database examples, see [Aspire database patterns](/fundamentals/database-patterns/). In this article, you learn how to add a custom command to a Redis resource that clears the cache. + + For how persistence affects seed data and initialization scripts, see [Persist a database across restarts](/fundamentals/database-patterns/#persist-a-database-across-restarts). + + ## Add MongoDB server resource with data bind mount To add a data bind mount to the MongoDB server resource, call the method: @@ -575,6 +579,10 @@ Data [bind mounts](https://docs.docker.com/engine/storage/bind-mounts/) have lim Data bind mounts rely on the host machine's filesystem to persist the MongoDB server data across container restarts. The data bind mount is mounted at the `C:\MongoDB\Data` on Windows (or `/MongoDB/Data` on Unix) path on the host machine in the MongoDB server container. For more information on data bind mounts, see [Docker docs: Bind mounts](https://docs.docker.com/engine/storage/bind-mounts). + + For how persistence affects seed data and initialization scripts, see [Persist a database across restarts](/fundamentals/database-patterns/#persist-a-database-across-restarts). + + ## Add MongoDB server resource with init files Use initialization files to seed the MongoDB server with data or run scripts at startup. Call `WithInitFiles(...)` in a C# AppHost or `withInitFiles(...)` in a TypeScript AppHost to copy the files from the specified directory into the container's initialization folder. @@ -640,6 +648,10 @@ builder.AddGoApp("api", "./api"). MongoDB executes any JavaScript or shell scripts found in the init folder when the container first starts. This is useful for pre-populating collections or creating indexes. + + For other ways to seed a database, see [Seed a database with sample data](/fundamentals/database-patterns/#seed-a-database-with-sample-data). + + ## Enable transactions and change streams with a single-member replica set MongoDB transactions and change streams require replica-set mode, even for a single server. Call `WithReplicaSet` (C#) or `withReplicaSet` (TypeScript) directly on the `MongoDBServerResource` to configure **and initialize** a single-member replica set, without creating a separate logical replica-set resource or configuring split-horizon TLS. Continue using , , and as usual — initialization runs during the resource lifecycle, and readiness waits for both initialization and primary election, so consumers can use transactions and change streams as soon as `WaitFor` completes. @@ -1048,6 +1060,10 @@ Use `exit` before closing the terminal tab. Closing the tab alone can leave the The client uses the resource's configured credentials and can modify or delete data. Enable it only for trusted dashboard users, including anyone accessing the dashboard through a tunnel or remote development session. ::: + + For examples that use the REPL to create, seed, or change a database, see [Aspire database patterns](/fundamentals/database-patterns/). + + ## Connection properties For the full reference of MongoDB connection properties — and how consuming apps in C#, TypeScript, Python, and Go read them — see [Connect to MongoDB](../mongodb-connect/). diff --git a/src/frontend/src/content/docs/integrations/databases/mysql/mysql-host.mdx b/src/frontend/src/content/docs/integrations/databases/mysql/mysql-host.mdx index 2778ce5b1..12a1f8fbb 100644 --- a/src/frontend/src/content/docs/integrations/databases/mysql/mysql-host.mdx +++ b/src/frontend/src/content/docs/integrations/databases/mysql/mysql-host.mdx @@ -264,6 +264,10 @@ builder.AddGoApp("api", "./api"). The script is executed only when the database is first created. If you need to run scripts on every startup, consider using init files or application-level migrations instead. + + For other ways to create a database, see [Create a database](/fundamentals/database-patterns/#create-a-database). + + ## Add MySQL resource with phpMyAdmin [**phpMyAdmin**](https://www.phpmyadmin.net/) is a popular web-based administration tool for MySQL. Call the method to add a phpMyAdmin container resource that connects to the MySQL container: @@ -468,6 +472,10 @@ Some database integrations, including the MySQL integration, can't successfully You can resolve the problem by deploying to a Kubernetes cluster, such as Azure Kubernetes Services (AKS). For more information, see [Deploy your first Aspire app](/get-started/deploy-first-app/). + + For how persistence affects seed data and initialization scripts, see [Persist a database across restarts](/fundamentals/database-patterns/#persist-a-database-across-restarts). + + ## Add a MySQL resource with a data bind mount Add a data bind mount to the MySQL server resource as shown in the following examples: @@ -536,6 +544,10 @@ builder.AddGoApp("api", "./api"). Data bind mounts rely on the host machine's filesystem to persist the MySQL server data across container restarts. The data bind mount is mounted at the `C:\MySQL\Data` on Windows (or `/MySQL/Data` on Unix) path on the host machine in the MySQL server container. For more information on data bind mounts, see [Docker docs: Bind mounts](https://docs.docker.com/engine/storage/bind-mounts). + + For how persistence affects seed data and initialization scripts, see [Persist a database across restarts](/fundamentals/database-patterns/#persist-a-database-across-restarts). + + ## Add MySQL resource with init files Use initialization files to seed or configure the MySQL server at startup. Call `WithInitFiles(...)` in a C# AppHost or `withInitFiles(...)` in a TypeScript AppHost to copy the files from the specified directory into the container's initialization folder. @@ -598,6 +610,10 @@ builder.AddGoApp("api", "./api"). The init files are executed when the MySQL server container starts. Place executable shell scripts or `.sql` files in the directory to initialize the database schema, seed data, or perform other setup tasks. + + For other ways to seed a database, see [Seed a database with sample data](/fundamentals/database-patterns/#seed-a-database-with-sample-data). + + ## Add MySQL resource with parameters When you want to provide a root MySQL password and optional port explicitly, you can pass them as parameters: @@ -799,6 +815,10 @@ Use `quit` before closing the terminal tab. Closing the tab alone can leave the The client uses the resource's configured credentials and can modify or delete data. Enable it only for trusted dashboard users, including anyone accessing the dashboard through a tunnel or remote development session. ::: + + For examples that use the REPL to create, seed, or change a database, see [Aspire database patterns](/fundamentals/database-patterns/). + + ## Connection properties For the full reference of MySQL connection properties — and how consuming apps in C#, TypeScript, Python, and Go read them — see [Connect to MySQL](../mysql-connect/). diff --git a/src/frontend/src/content/docs/integrations/databases/postgres/postgres-host.mdx b/src/frontend/src/content/docs/integrations/databases/postgres/postgres-host.mdx index 914992958..bc4203caa 100644 --- a/src/frontend/src/content/docs/integrations/databases/postgres/postgres-host.mdx +++ b/src/frontend/src/content/docs/integrations/databases/postgres/postgres-host.mdx @@ -254,6 +254,10 @@ The preceding example creates a database named `app_db`. The script is run when The connect to a database command (`\c`) isn't supported when using the creation script. + + For other ways to create a database, see [Create a database](/fundamentals/database-patterns/#create-a-database). + + ## Add PostgreSQL pgAdmin resource Add the [**dpage/pgadmin4**](https://www.pgadmin.org/) container to the PostgreSQL resource to get a web-based admin dashboard, as shown in the following examples: @@ -577,6 +581,10 @@ The data volume is used to persist the PostgreSQL server data outside the lifecy You can resolve the problem by using the managed service **Azure Database for PostgreSQL** to host the deployed database instead of a container in ACA, which is the recommended approach regardless of this issue. For that managed-service flow, see [Azure Database for PostgreSQL Hosting integration](/integrations/cloud/azure/azure-postgresql/azure-postgresql-host/). + + For how persistence affects seed data and initialization scripts, see [Persist a database across restarts](/fundamentals/database-patterns/#persist-a-database-across-restarts). + + ## Add PostgreSQL server resource with data bind mount Add a data bind mount to the PostgreSQL server resource as shown in the following examples: @@ -642,6 +650,10 @@ builder.AddGoApp("api", "./api"). Data bind mounts rely on the host machine's filesystem to persist the PostgreSQL server data across container restarts. The data bind mount is mounted at the `C:\PostgreSQL\Data` on Windows (or `/PostgreSQL/Data` on Unix) path on the host machine in the PostgreSQL server container. For more information on data bind mounts, see [Docker docs: Bind mounts](https://docs.docker.com/engine/storage/bind-mounts). + + For how persistence affects seed data and initialization scripts, see [Persist a database across restarts](/fundamentals/database-patterns/#persist-a-database-across-restarts). + + ## Add PostgreSQL server resource with init files Use initialization files to seed the PostgreSQL server. Call `WithInitFiles(...)` in a C# AppHost or `withInitFiles(...)` in a TypeScript AppHost to copy the files from the specified directory into the container's initialization folder. @@ -704,6 +716,10 @@ The Go AppHost doesn't expose the obsolete bind-mount API for PostgreSQL initial The files in the specified directory are copied into the PostgreSQL server container's _init_ folder. This folder is used for initialization, running any executable shell scripts or _.sql_ command files after the _postgres-data_ folder is created. + + For an example initialization script that creates and seeds a database, see [Seed a database in a dashboard REPL terminal](/fundamentals/database-patterns/#seed-a-database-in-a-dashboard-repl-terminal). For other ways to seed a database, see [Seed a database with sample data](/fundamentals/database-patterns/#seed-a-database-with-sample-data). + + ## Add PostgreSQL server resource with parameters When you want to explicitly provide the username and password used by the container image, you can provide these credentials as parameters. Consider the following alternative examples: @@ -980,6 +996,10 @@ Use `\connect` to switch databases and `\q` to exit before closing the terminal The client uses the resource's configured credentials and can modify or delete data. Enable it only for trusted dashboard users, including anyone accessing the dashboard through a tunnel or remote development session. ::: + + For examples that use the REPL to create, seed, or change a database, see [Aspire database patterns](/fundamentals/database-patterns/). + + ## Connection properties For the full reference of PostgreSQL connection properties — and how consuming apps in C#, TypeScript, Python, and Go read them — see [Connect to PostgreSQL](../postgres-connect/). diff --git a/src/frontend/src/content/docs/integrations/databases/sql-server/sql-server-host.mdx b/src/frontend/src/content/docs/integrations/databases/sql-server/sql-server-host.mdx index 25d793da9..d4365ed0d 100644 --- a/src/frontend/src/content/docs/integrations/databases/sql-server/sql-server-host.mdx +++ b/src/frontend/src/content/docs/integrations/databases/sql-server/sql-server-host.mdx @@ -320,6 +320,10 @@ if err := app.Run(); err != nil { The preceding example creates a database named `app-db` with a single `todos` table. The script is executed when the database resource is created in the context of the SQL Server resource. + + For other ways to create a database, see [Create a database](/fundamentals/database-patterns/#create-a-database). + + ## Add SQL Server resource with data volume Add a data volume to the SQL Server resource as shown in the following examples: @@ -395,6 +399,10 @@ The data volume is used to persist the SQL Server data outside the lifecycle of The password is stored in the data volume. When using a data volume and if the password changes, it will not work until you delete the volume. + + For how persistence affects seed data and initialization scripts, see [Persist a database across restarts](/fundamentals/database-patterns/#persist-a-database-across-restarts). + + ## Add SQL Server resource with data bind mount Add a data bind mount to the SQL Server resource as shown in the following examples: @@ -470,6 +478,10 @@ if err := app.Run(); err != nil { Data bind mounts rely on the host machine's filesystem to persist the SQL Server data across container restarts. The data bind mount is mounted at the `C:\SqlServer\Data` on Windows (or `/SqlServer/Data` on Unix) path on the host machine in the SQL Server container. For more information on data bind mounts, see [Docker docs: Bind mounts](https://docs.docker.com/engine/storage/bind-mounts). + + For how persistence affects seed data and initialization scripts, see [Persist a database across restarts](/fundamentals/database-patterns/#persist-a-database-across-restarts). + + ## Add SQL Server resource with parameters When you want to explicitly provide the password used by the container image, you can provide it as a parameter: @@ -870,6 +882,10 @@ Use `USE [db];` followed by `GO` to switch databases, and `QUIT` to exit before The client runs as `sa`, can modify or delete data, and can execute server-side operating system commands when enabled. Enable it only for trusted dashboard users, including anyone accessing the dashboard through a tunnel or remote development session. ::: + + For examples that use the REPL to create, seed, or change a database, see [Aspire database patterns](/fundamentals/database-patterns/). + + ### Select the client executable :::note[Breaking change]