-
Notifications
You must be signed in to change notification settings - Fork 30
Update and restructure SQLite integration docs #149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
9ad7026
81a692b
389adfb
c7e1414
76206ce
9a3845f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,170 @@ | ||||||||||||||||||||||||||||||||||||||||||||
| --- | ||||||||||||||||||||||||||||||||||||||||||||
| title: SQLite Client integration reference | ||||||||||||||||||||||||||||||||||||||||||||
| description: Learn how to use the Aspire SQLite Client integration to query SQLite databases from your Aspire projects. | ||||||||||||||||||||||||||||||||||||||||||||
| next: false | ||||||||||||||||||||||||||||||||||||||||||||
| --- | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| import { Aside } from '@astrojs/starlight/components'; | ||||||||||||||||||||||||||||||||||||||||||||
| import { Badge } from '@astrojs/starlight/components'; | ||||||||||||||||||||||||||||||||||||||||||||
| import InstallPackage from '@components/InstallPackage.astro'; | ||||||||||||||||||||||||||||||||||||||||||||
| import InstallDotNetPackage from '@components/InstallDotNetPackage.astro'; | ||||||||||||||||||||||||||||||||||||||||||||
| import { Image } from 'astro:assets'; | ||||||||||||||||||||||||||||||||||||||||||||
| import sqliteIcon from '@assets/icons/sqlite-icon.png'; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| <Badge text="⭐ Community Toolkit" variant="tip" size="large" /> | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| <Image | ||||||||||||||||||||||||||||||||||||||||||||
| src={sqliteIcon} | ||||||||||||||||||||||||||||||||||||||||||||
| alt="SQLite logo" | ||||||||||||||||||||||||||||||||||||||||||||
| width={100} | ||||||||||||||||||||||||||||||||||||||||||||
| height={100} | ||||||||||||||||||||||||||||||||||||||||||||
| class:list={'float-inline-left icon'} | ||||||||||||||||||||||||||||||||||||||||||||
| data-zoom-off | ||||||||||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| To get started with the Aspire SQLite integrations, follow the [Get started with SQLite integrations](../sqlite-get-started/) guide. | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| This article includes full details about the Aspire SQLite Client integration, which allows you to connect to and interact with SQLite databases from your Aspire consuming projects. | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| ## Installation | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| To get started with the Aspire SQLite client integration, install the [📦 CommunityToolkit.Aspire.Microsoft.Data.Sqlite](https://www.nuget.org/packages/CommunityToolkit.Aspire.Microsoft.Data.Sqlite) NuGet package in the client-consuming project, that is, the project for the application that uses the SQLite client. The SQLite client integration registers a [`SqliteConnection`](https://learn.microsoft.com/dotnet/api/microsoft.data.sqlite.sqliteconnection) instance that you can use to interact with SQLite. | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| <InstallDotNetPackage packageName="CommunityToolkit.Aspire.Microsoft.Data.Sqlite" /> | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| ## Add SQLite client | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| In the `Program.cs` file of your client-consuming project, call the `AddSqliteConnection` extension method to register a `SqliteConnection` for use via the dependency injection container. The method takes a connection name parameter. | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| ```csharp title="C# — Program.cs" | ||||||||||||||||||||||||||||||||||||||||||||
| builder.AddSqliteConnection(connectionName: "sqlite"); | ||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| <Aside type="tip"> | ||||||||||||||||||||||||||||||||||||||||||||
| The `connectionName` parameter must match the name used when adding the SQLite resource in the AppHost project. For more information, see [Add SQLite resource](../sqlite-host/#add-sqlite-resource). | ||||||||||||||||||||||||||||||||||||||||||||
| </Aside> | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| After adding `SqliteConnection` to the builder, you can get the `SqliteConnection` instance using dependency injection. For example, to retrieve your connection object from an example service define it as a constructor parameter: | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| ```csharp title="C# — ExampleService.cs" | ||||||||||||||||||||||||||||||||||||||||||||
| public class ExampleService(SqliteConnection connection) | ||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||
| // Use connection... | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| For more information on dependency injection, see [.NET dependency injection](https://learn.microsoft.com/dotnet/core/extensions/dependency-injection). | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| ## Add keyed SQLite client | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| There might be situations where you want to register multiple `SqliteConnection` instances with different connection names. To register keyed SQLite clients, call the `AddKeyedSqliteConnection` method: | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| ```csharp title="C# — Program.cs" | ||||||||||||||||||||||||||||||||||||||||||||
| builder.AddKeyedSqliteConnection(name: "chat"); | ||||||||||||||||||||||||||||||||||||||||||||
| builder.AddKeyedSqliteConnection(name: "queue"); | ||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| <Aside type="caution"> | ||||||||||||||||||||||||||||||||||||||||||||
| When using keyed services, it's expected that your SQLite resource configured two named databases, one for the `chat` and one for the `queue`. | ||||||||||||||||||||||||||||||||||||||||||||
| </Aside> | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| Then you can retrieve the `SqliteConnection` instances using dependency injection: | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| ```csharp title="C# — ExampleService.cs" | ||||||||||||||||||||||||||||||||||||||||||||
| public class ExampleService( | ||||||||||||||||||||||||||||||||||||||||||||
| [FromKeyedServices("chat")] SqliteConnection chatConnection, | ||||||||||||||||||||||||||||||||||||||||||||
| [FromKeyedServices("queue")] SqliteConnection queueConnection) | ||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||
| // Use connections... | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| For more information on keyed services, see [.NET dependency injection: Keyed services](https://learn.microsoft.com/dotnet/core/extensions/dependency-injection#keyed-services). | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| ## Properties of the SQLite resources | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| When you use the `WithReference` method to pass a SQLite database resource from the AppHost project to a consuming client project, Aspire exposes the data source as an environment variable with a standardized naming convention: | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| | Property Name | Description | Environment Variable Format | | ||||||||||||||||||||||||||||||||||||||||||||
| |---------------|-------------|----------------------------| | ||||||||||||||||||||||||||||||||||||||||||||
| | `DataSource` | The data source path containing the database file path. | `C:\{Path}\{Databasefile}.db` | | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| <Aside type="note"> | ||||||||||||||||||||||||||||||||||||||||||||
| Aspire exposes the data source as an environment variable named `{ResourceName}_DATASOURCE`. For instance, a resource called `sqlite` would have its data source string available as `SQLITE_DATASOURCE`. | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+86
to
+93
|
||||||||||||||||||||||||||||||||||||||||||||
| When you use the `WithReference` method to pass a SQLite database resource from the AppHost project to a consuming client project, Aspire exposes the data source as an environment variable with a standardized naming convention: | |
| | Property Name | Description | Environment Variable Format | | |
| |---------------|-------------|----------------------------| | |
| | `DataSource` | The data source path containing the database file path. | `C:\{Path}\{Databasefile}.db` | | |
| <Aside type="note"> | |
| Aspire exposes the data source as an environment variable named `{ResourceName}_DATASOURCE`. For instance, a resource called `sqlite` would have its data source string available as `SQLITE_DATASOURCE`. | |
| When you use the `WithReference` method to pass a SQLite database resource from the AppHost project to a consuming client project, Aspire exposes the SQLite connection information via environment variables with standardized naming conventions: | |
| | Property Name | Description | Environment Variable | | |
| |---------------|-------------|----------------------| | |
| | `DataSource` | The SQLite connection string or data source path containing the database file path (for example, `Data Source=C:\{Path}\{Databasefile}.db`). | - `{RESOURCE_NAME}_DATASOURCE`<br />- `ConnectionStrings__{ResourceName}` | | |
| <Aside type="note"> | |
| Aspire exposes the same `DataSource` value under multiple environment variable names: | |
| <ul> | |
| <li><code>{RESOURCE_NAME}_DATASOURCE</code> — a generic environment variable that can be used by any consumer (for example, a resource called <code>sqlite</code> would have <code>SQLITE_DATASOURCE</code>).</li> | |
| <li><code>ConnectionStrings__{ResourceName}</code> — the conventional .NET configuration key for the connection string (for example, <code>ConnectionStrings__sqlite</code>).</li> | |
| </ul> | |
| Non-.NET applications will typically read <code>{RESOURCE_NAME}_DATASOURCE</code> directly, while .NET applications can use the <code>ConnectionStrings</code> configuration section. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The sentence "it's expected that your SQLite resource configured two named databases" is grammatically awkward. It should clarify that the AppHost project should have configured two resources.