コンテンツにスキップ
ドキュメントAspire を試す
ドキュメント試す

Set up SurrealDB in the AppHost

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

⭐ Community Toolkit SurrealDB logo

This article is the reference for the Aspire SurrealDB Hosting integration. It enumerates the AppHost APIs that you use to model a SurrealDB server, namespace, and database as resources in your AppHost project.

If you’re new to the SurrealDB integration, start with the Get started with SurrealDB integrations guide. For how consuming apps read the connection information this page exposes, see Connect to SurrealDB.

To start building an Aspire app that uses SurrealDB, install the 📦 CommunityToolkit.Aspire.Hosting.SurrealDb NuGet package:

Terminal
aspire add CommunityToolkit.Aspire.Hosting.SurrealDb

Learn more about aspire add in the command reference.

This updates your aspire.config.json with the SurrealDB hosting integration package:

aspire.config.json
{
"packages": {
"CommunityToolkit.Aspire.Hosting.SurrealDb": "*"
}
}

Once you’ve installed the hosting integration in your AppHost project, you can add a SurrealDB server, namespace, and database resource as shown in the following example:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
const
const surreal: SurrealDbServerResource
surreal
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addSurrealServer(name: string, options?: {
userName?: string | ParameterResource;
password?: string | ParameterResource;
port?: number;
path?: string;
}): SurrealDbServerResource (+1 overload)

Adds a SurrealDB resource to the application model. A container is used for local development. The default image is and the tag is .

addSurrealServer
("surreal");
const
const ns: SurrealDbNamespaceResource
ns
= await
const surreal: SurrealDbServerResource
surreal
.
SurrealDbServerResource.addNamespace(name: string, options?: {
namespaceName?: string;
}): SurrealDbNamespaceResource (+1 overload)

Adds a SurrealDB namespace resource to the application model

addNamespace
("ns");
const
const db: SurrealDbDatabaseResource
db
= await
const ns: SurrealDbNamespaceResource
ns
.
SurrealDbNamespaceResource.addDatabase(name: string, options?: {
databaseName?: string;
}): SurrealDbDatabaseResource (+1 overload)

Adds a SurrealDB database resource to the application model

addDatabase
("db");
const
const exampleProject: ProjectResource
exampleProject
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addProject(name: string, projectPath: string, options?: {
launchProfileOrOptions?: ProjectResourceOptions;
}): ProjectResource (+1 overload)

Adds a .NET project resource

addProject
("apiservice", "../ExampleProject/ExampleProject.csproj");
await
const exampleProject: ProjectResource
exampleProject
.
ProjectResource.withReference(source: EndpointReference | string | uri, options?: {
connectionName?: string;
optional?: boolean;
name?: string;
} | undefined): ProjectResource (+1 overload)

Adds a reference to another resource

withReference
(
const db: SurrealDbDatabaseResource
db
);
await
const exampleProject: ProjectResource
exampleProject
.
ProjectResource.waitFor(dependency: IResource | IResourceWithConnectionString, waitBehavior?: WaitBehavior): ProjectResource

Waits for another resource to be ready

waitFor
(
const db: SurrealDbDatabaseResource
db
);
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.build(): DistributedApplication

Builds the distributed application

build
().
DistributedApplication.run(cancellationToken?: cancellationToken): void

Runs the distributed application

run
();
  1. When Aspire adds a container image to the AppHost, as shown in the preceding example with the docker.io/surrealdb/surrealdb image, it creates a new SurrealDB instance on your local machine.

  2. The SurrealDB resource includes default credentials with a username of root and a random password generated using the CreateDefaultPasswordParameter method. When the AppHost runs, the password is stored in the AppHost’s secret store under the key surreal-password.

  3. Chaining .AddNamespace("ns") declares a SurrealDB namespace resource, and chaining .AddDatabase("db") declares a database inside that namespace.

  4. The AppHost reference call configures a connection in the consuming project named after the referenced database resource, such as db in the preceding example.

To add Surrealist to the SurrealDB server resource, call the WithSurrealist method:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
let
let surreal: SurrealDbServerResource
surreal
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addSurrealServer(name: string, options?: {
userName?: string | ParameterResource;
password?: string | ParameterResource;
port?: number;
path?: string;
}): SurrealDbServerResource (+1 overload)

Adds a SurrealDB resource to the application model. A container is used for local development. The default image is and the tag is .

addSurrealServer
("surreal");
let surreal: SurrealDbServerResource
surreal
= await
let surreal: SurrealDbServerResource
surreal
.
SurrealDbServerResource.withSurrealist(options?: {
containerName?: string;
}): SurrealDbServerResource (+1 overload)
withSurrealist
();
const
const ns: SurrealDbNamespaceResource
ns
= await
let surreal: SurrealDbServerResource
surreal
.
SurrealDbServerResource.addNamespace(name: string, options?: {
namespaceName?: string;
}): SurrealDbNamespaceResource (+1 overload)

Adds a SurrealDB namespace resource to the application model

addNamespace
("ns");
const
const db: SurrealDbDatabaseResource
db
= await
const ns: SurrealDbNamespaceResource
ns
.
SurrealDbNamespaceResource.addDatabase(name: string, options?: {
databaseName?: string;
}): SurrealDbDatabaseResource (+1 overload)

Adds a SurrealDB database resource to the application model

addDatabase
("db");
const
const exampleProject: ProjectResource
exampleProject
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addProject(name: string, projectPath: string, options?: {
launchProfileOrOptions?: ProjectResourceOptions;
}): ProjectResource (+1 overload)

Adds a .NET project resource

addProject
("apiservice", "../ExampleProject/ExampleProject.csproj");
await
const exampleProject: ProjectResource
exampleProject
.
ProjectResource.withReference(source: EndpointReference | string | uri, options?: {
connectionName?: string;
optional?: boolean;
name?: string;
} | undefined): ProjectResource (+1 overload)

Adds a reference to another resource

withReference
(
const db: SurrealDbDatabaseResource
db
);
await
const exampleProject: ProjectResource
exampleProject
.
ProjectResource.waitFor(dependency: IResource | IResourceWithConnectionString, waitBehavior?: WaitBehavior): ProjectResource

Waits for another resource to be ready

waitFor
(
const db: SurrealDbDatabaseResource
db
);
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.build(): DistributedApplication

Builds the distributed application

build
().
DistributedApplication.run(cancellationToken?: cancellationToken): void

Runs the distributed application

run
();

Surrealist is a web-based user interface for interacting with your SurrealDB database visually. It lets you connect to any SurrealDB instance, execute queries, explore your tables, and design your schemas.

Add SurrealDB resource with data bind mount

Section titled “Add SurrealDB resource with data bind mount”

To add a data bind mount to the SurrealDB resource, call the WithDataBindMount method:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
let
let surreal: SurrealDbServerResource
surreal
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addSurrealServer(name: string, options?: {
userName?: string | ParameterResource;
password?: string | ParameterResource;
port?: number;
path?: string;
}): SurrealDbServerResource (+1 overload)

Adds a SurrealDB resource to the application model. A container is used for local development. The default image is and the tag is .

addSurrealServer
("surreal");
let surreal: SurrealDbServerResource
surreal
= await
let surreal: SurrealDbServerResource
surreal
.
SurrealDbServerResource.withDataBindMount(source: string): SurrealDbServerResource

Adds a bind mount for the data folder to a SurrealDB resource.

withDataBindMount
("./data/surreal/data");
const
const ns: SurrealDbNamespaceResource
ns
= await
let surreal: SurrealDbServerResource
surreal
.
SurrealDbServerResource.addNamespace(name: string, options?: {
namespaceName?: string;
}): SurrealDbNamespaceResource (+1 overload)

Adds a SurrealDB namespace resource to the application model

addNamespace
("ns");
const
const db: SurrealDbDatabaseResource
db
= await
const ns: SurrealDbNamespaceResource
ns
.
SurrealDbNamespaceResource.addDatabase(name: string, options?: {
databaseName?: string;
}): SurrealDbDatabaseResource (+1 overload)

Adds a SurrealDB database resource to the application model

addDatabase
("db");
const
const exampleProject: ProjectResource
exampleProject
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addProject(name: string, projectPath: string, options?: {
launchProfileOrOptions?: ProjectResourceOptions;
}): ProjectResource (+1 overload)

Adds a .NET project resource

addProject
("apiservice", "../ExampleProject/ExampleProject.csproj");
await
const exampleProject: ProjectResource
exampleProject
.
ProjectResource.withReference(source: EndpointReference | string | uri, options?: {
connectionName?: string;
optional?: boolean;
name?: string;
} | undefined): ProjectResource (+1 overload)

Adds a reference to another resource

withReference
(
const db: SurrealDbDatabaseResource
db
);
await
const exampleProject: ProjectResource
exampleProject
.
ProjectResource.waitFor(dependency: IResource | IResourceWithConnectionString, waitBehavior?: WaitBehavior): ProjectResource

Waits for another resource to be ready

waitFor
(
const db: SurrealDbDatabaseResource
db
);
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.build(): DistributedApplication

Builds the distributed application

build
().
DistributedApplication.run(cancellationToken?: cancellationToken): void

Runs the distributed application

run
();

Data bind mounts rely on the host machine’s filesystem to persist SurrealDB data across container restarts. The mount path ./data/surreal/data is resolved relative to the AppHost project directory. For more information on data bind mounts, see Docker docs: Bind mounts.

When you want to explicitly provide the password used by the container image, you can provide it as a parameter:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
const
const password: ParameterResource
password
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addParameter(name: string, options?: {
value?: string;
publishValueAsDefault?: boolean;
secret?: boolean;
}): ParameterResource (+1 overload)

Adds a parameter resource

addParameter
("password", {
secret?: boolean | undefined
secret
: true });
const
const surreal: SurrealDbServerResource
surreal
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addSurrealServer(name: string, options?: {
userName?: string | ParameterResource;
password?: string | ParameterResource;
port?: number;
path?: string;
}): SurrealDbServerResource (+1 overload)

Adds a SurrealDB resource to the application model. A container is used for local development. The default image is and the tag is .

addSurrealServer
("surreal", {
password?: string | ParameterResource | undefined
password
});
const
const ns: SurrealDbNamespaceResource
ns
= await
const surreal: SurrealDbServerResource
surreal
.
SurrealDbServerResource.addNamespace(name: string, options?: {
namespaceName?: string;
}): SurrealDbNamespaceResource (+1 overload)

Adds a SurrealDB namespace resource to the application model

addNamespace
("ns");
const
const db: SurrealDbDatabaseResource
db
= await
const ns: SurrealDbNamespaceResource
ns
.
SurrealDbNamespaceResource.addDatabase(name: string, options?: {
databaseName?: string;
}): SurrealDbDatabaseResource (+1 overload)

Adds a SurrealDB database resource to the application model

addDatabase
("db");
const
const exampleProject: ProjectResource
exampleProject
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addProject(name: string, projectPath: string, options?: {
launchProfileOrOptions?: ProjectResourceOptions;
}): ProjectResource (+1 overload)

Adds a .NET project resource

addProject
("apiservice", "../ExampleProject/ExampleProject.csproj");
await
const exampleProject: ProjectResource
exampleProject
.
ProjectResource.withReference(source: EndpointReference | string | uri, options?: {
connectionName?: string;
optional?: boolean;
name?: string;
} | undefined): ProjectResource (+1 overload)

Adds a reference to another resource

withReference
(
const db: SurrealDbDatabaseResource
db
);
await
const exampleProject: ProjectResource
exampleProject
.
ProjectResource.waitFor(dependency: IResource | IResourceWithConnectionString, waitBehavior?: WaitBehavior): ProjectResource

Waits for another resource to be ready

waitFor
(
const db: SurrealDbDatabaseResource
db
);
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.build(): DistributedApplication

Builds the distributed application

build
().
DistributedApplication.run(cancellationToken?: cancellationToken): void

Runs the distributed application

run
();

For more information on providing parameters, see External parameters.

To configure the log level for the SurrealDB container, call the WithLogLevel method:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
let
let surreal: SurrealDbServerResource
surreal
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addSurrealServer(name: string, options?: {
userName?: string | ParameterResource;
password?: string | ParameterResource;
port?: number;
path?: string;
}): SurrealDbServerResource (+1 overload)

Adds a SurrealDB resource to the application model. A container is used for local development. The default image is and the tag is .

addSurrealServer
("surreal");
let surreal: SurrealDbServerResource
surreal
= await
let surreal: SurrealDbServerResource
surreal
.
SurrealDbServerResource.withLogLevel(logLevel: string): SurrealDbServerResource
withLogLevel
("Debug");
const
const ns: SurrealDbNamespaceResource
ns
= await
let surreal: SurrealDbServerResource
surreal
.
SurrealDbServerResource.addNamespace(name: string, options?: {
namespaceName?: string;
}): SurrealDbNamespaceResource (+1 overload)

Adds a SurrealDB namespace resource to the application model

addNamespace
("ns");
const
const db: SurrealDbDatabaseResource
db
= await
const ns: SurrealDbNamespaceResource
ns
.
SurrealDbNamespaceResource.addDatabase(name: string, options?: {
databaseName?: string;
}): SurrealDbDatabaseResource (+1 overload)

Adds a SurrealDB database resource to the application model

addDatabase
("db");
const
const exampleProject: ProjectResource
exampleProject
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addProject(name: string, projectPath: string, options?: {
launchProfileOrOptions?: ProjectResourceOptions;
}): ProjectResource (+1 overload)

Adds a .NET project resource

addProject
("apiservice", "../ExampleProject/ExampleProject.csproj");
await
const exampleProject: ProjectResource
exampleProject
.
ProjectResource.withReference(source: EndpointReference | string | uri, options?: {
connectionName?: string;
optional?: boolean;
name?: string;
} | undefined): ProjectResource (+1 overload)

Adds a reference to another resource

withReference
(
const db: SurrealDbDatabaseResource
db
);
await
const exampleProject: ProjectResource
exampleProject
.
ProjectResource.waitFor(dependency: IResource | IResourceWithConnectionString, waitBehavior?: WaitBehavior): ProjectResource

Waits for another resource to be ready

waitFor
(
const db: SurrealDbDatabaseResource
db
);
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.build(): DistributedApplication

Builds the distributed application

build
().
DistributedApplication.run(cancellationToken?: cancellationToken): void

Runs the distributed application

run
();

The WithLogLevel method enables verbose logging in the SurrealDB container, which is useful during development and debugging. The non-C# TypeScript AppHost accepts the log level as a string (for example, "Trace", "Debug", "Information", "Warning", "Error", "Critical", or "None").

The SurrealDB hosting integration automatically adds a health check for the SurrealDB namespace and database resources. The health check uses both the /health endpoint and a simple raw query to verify that SurrealDB is running and that a connection can be established. The health check is wired into the /health HTTP endpoint, where all registered health checks must pass before the app is considered ready to accept traffic.