Aller au contenu

Get started with the SurrealDB integrations

Ce contenu n’est pas encore disponible dans votre langue.

⭐ 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 — Ajouter le package CommunityToolkit.Aspire.Hosting.SurrealDb
aspire add communitytoolkit-surrealdb

La CLI Aspire est interactive ; choisissez le résultat approprié lorsque demandé :

Aspire CLI — Exemple de sortie
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.