コンテンツにスキップ

Get started with the SurrealDB integrations

このコンテンツはまだ日本語訳がありません。

⭐ Community Toolkit SurrealDB logo

SurrealDB is a native, open-source, multi-model database that lets you store and manage data across relational, document, graph, time-series, vector & search, and geospatial models—all in one place. The Aspire SurrealDB integration enables you to connect to existing SurrealDB instances or create new instances from Aspire using the docker.io/surrealdb/surrealdb container image.

In this introduction, you’ll see how to install and use the Aspire SurrealDB integrations in a simple configuration. If you already have this knowledge, see SurrealDB hosting integration for full reference details.

To begin, install the Aspire SurrealDB Hosting integration in your Aspire AppHost project. This integration allows you to create and manage SurrealDB instances from your Aspire hosting projects:

Aspire CLI — CommunityToolkit.Aspire.Hosting.SurrealDb パッケージを追加
aspire add communitytoolkit-surrealdb

Aspire CLI は対話的です。求められたら適切な結果を選択してください:

Aspire CLI — 出力例
Select an integration to add:
> communitytoolkit-surrealdb (CommunityToolkit.Aspire.Hosting.SurrealDb)
> Other results listed as selectable options...

Next, in the AppHost project, register and consume the SurrealDB integration using the AddSurrealServer extension method to add the SurrealDB container to the application builder. Chain a call to the returned resource builder to AddNamespace and then AddDatabase, to add a SurrealDB database resource:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var db = builder.AddSurrealServer("surreal")
.AddNamespace("ns")
.AddDatabase("db");
builder.AddProject<Projects.ExampleProject>()
.WithReference(db)
.WaitFor(db);
// After adding all resources, run the app...

In each of the consuming client projects, install the Aspire SurrealDB client integration:

.NET CLI — Add CommunityToolkit.Aspire.SurrealDb package
dotnet add package CommunityToolkit.Aspire.SurrealDb

In the Program.cs file of your client-consuming project, call the AddSurrealClient extension method to register a SurrealDbClient for use via the dependency injection container. The method takes a connection name parameter.

builder.AddSurrealClient(connectionName: "db");

You can then retrieve the SurrealDbClient instance using dependency injection. For example, to retrieve the client from a service:

public class ExampleService(SurrealDbClient client)
{
// Use client...
}

For full reference details, see SurrealDB hosting integration and SurrealDB client integration.