Gå til indhold

Data API builder integration

Dette indhold er ikke tilgængeligt i dit sprog endnu.

⭐ Community Toolkit Data API builder logo

Data API Builder (DAB) is an open-source tool that provides a REST and GraphQL API on top of your database. The Aspire Data API Builder hosting integration enables you to run Data API Builder alongside your .NET projects in the Aspire app host.

To get started with the Aspire Data API Builder hosting integration, install the CommunityToolkit.Aspire.Hosting.Azure.DataApiBuilder NuGet package in the app host project.

Aspire CLI — Tilføj CommunityToolkit.Aspire.Hosting.Azure.DataApiBuilder-pakke
aspire add communitytoolkit-azure-dataapibuilder

Aspire CLI er interaktiv; vælg det passende søgeresultat når du bliver spurgt:

Aspire CLI — Eksempel output
Select an integration to add:
> communitytoolkit-azure-dataapibuilder (CommunityToolkit.Aspire.Hosting.Azure.DataApiBuilder)
> Other results listed as selectable options...

To add Data API Builder to your app host, use the AddDataAPIBuilder extension method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var sqldb = builder.AddSqlServer("sql")
.AddDatabase("sqldb");
var dab = builder.AddDataAPIBuilder("dab")
.WithReference(sqldb);
builder.AddProject<Projects.ExampleProject>()
.WithReference(dab);
// After adding all resources, run the app...

The Data API Builder integration automatically discovers and uses dab-config.json configuration files in your project.

Data API Builder supports multiple configuration files for different databases. To specify multiple configuration files:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var sql = builder.AddSqlServer("sql")
.AddDatabase("sqldb");
var postgres = builder.AddPostgres("postgres")
.AddDatabase("postgresdb");
var dab = builder.AddDataAPIBuilder("dab", ["dab-config.SqlServer.json", "dab-config.PostgreSQL.json"])
.WithReference(sql)
.WithReference(postgres);
// After adding all resources, run the app...

To use a custom Data API Builder container image, use the container image methods:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var dab = builder.AddDataAPIBuilder("dab")
.WithImageRegistry("myregistry.azurecr.io")
.WithImage("custom-dab")
.WithImageTag("1.0.0");
// After adding all resources, run the app...

Data API Builder requires references to the databases it exposes. Use WithReference to connect Data API Builder to your databases:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var sql = builder.AddSqlServer("sql")
.AddDatabase("sqldb");
var cosmos = builder.AddAzureCosmosDB("cosmos")
.AddDatabase("cosmosdb");
var dab = builder.AddDataAPIBuilder("dab")
.WithReference(sql)
.WithReference(cosmos);
// After adding all resources, run the app...

Data API Builder uses dab-config.json files to define the API. Here’s an example configuration:

{
"$schema": "https://github.com/Azure/data-api-builder/releases/download/v0.10.23/dab.draft.schema.json",
"data-source": {
"database-type": "mssql",
"connection-string": "@env('DATABASE_CONNECTION_STRING')"
},
"runtime": {
"rest": {
"enabled": true,
"path": "/api"
},
"graphql": {
"enabled": true,
"path": "/graphql"
}
},
"entities": {
"Product": {
"source": "dbo.Products",
"permissions": [
{
"role": "anonymous",
"actions": ["read"]
}
]
}
}
}

The Aspire integration automatically injects the database connection strings from the referenced resources.

Data API Builder exposes two endpoints:

  • REST API: Available at /api by default
  • GraphQL API: Available at /graphql by default

Both endpoints are configured in the dab-config.json file.

Spørg & svarSamarbejdFællesskabDiskutérSe