该 data-source 部分定义数据库访问详细信息。 它还定义数据库选项。
数据源设置
| Property | Description |
|---|---|
| data-source | 包含数据库连接设置的对象 |
| data-source.database-type | 后端中使用的数据库:mssql、、postgresqlmysql、、 cosmosdb_nosqlcosmosdb_postgresql |
| data-source.connection-string | 所选数据库类型的连接字符串 |
| data-source.options | 特定于数据库的属性(例如 SQL Server、Cosmos DB 等的选项) |
| data-source.options.database | 用于 NoSQL 数据库的 Azure Cosmos DB 的名称(在以下情况下 database-type = cosmosdb_nosql是必需的) |
| data-source.options.container | 用于 NoSQL 容器的 Azure Cosmos DB 的名称(在以下情况下 database-type = cosmosdb_nosql是必需的) |
| data-source.options.schema | GraphQL 架构文件的路径(在以下情况下 database-type = cosmosdb_nosql是必需的) |
| data-source.options.set-session-context | 启用将 JSON Web 令牌 (JWT) 声明作为会话上下文发送(仅限 SQL Server) |
| data-source.health | 为数据源配置运行状况检查的对象 |
| data-source.health.enabled | 启用运行状况检查终结点 |
| data-source.health.name | 运行状况报告中使用的标识符 |
| data-source.health.threshold-ms | 运行状况检查查询的最大持续时间(以毫秒为单位) |
Format overview
{
"data-source": {
"database-type": <string>,
"connection-string": <string>,
"options": {
// mssql only
"set-session-context": <true> (default) | <false>,
// cosmosdb_nosql only
"database": <string>,
"container": <string>,
"schema": <string>
},
"health": {
"enabled": <true> (default) | <false>,
"name": <string>,
"threshold-ms": <integer; default: 1000>
}
},
"data-source-files": ["<string>"]
}
Data source
| Parent | Property | 类型 | Required | Default |
|---|---|---|---|---|
$root |
database-source |
对象 | ✔️ Yes | - |
Nested properties
| Parent | Property | 类型 | Required | Default |
|---|---|---|---|---|
data-source |
database-type |
枚举 | ✔️ Yes | None |
data-source |
connection-string |
字符串 | ✔️ Yes | None |
data-source |
options |
对象 | ❌ 否 | None |
Property values
database-type |
Description | Min Version |
|---|---|---|
mssql |
Fabric 中的 SQL | - |
mssql |
Azure SQL 数据库 | - |
mssql |
Azure SQL MI | - |
mssql |
SQL Server | 2016 |
dwsql |
Azure Synapse Analytics | - |
dwsql |
Fabric Warehouse | - |
dwsql |
Fabric SQL Analytics 终结点 | - |
postgresql |
PostgreSQL | ver. 11 |
mysql |
MySQL | ver. 8 |
cosmosdb_nosql |
Azure Cosmos DB for NoSQL | - |
cosmosdb_postgresql |
Azure Cosmos DB for PostgreSQL | - |
Format
{
"data-source": {
"database-type": <string>,
"connection-string": <string>,
"options": {
"<key-name>": <string>
}
}
}
示例:Azure SQL 和 SQL Server
"data-source": {
"database-type": "mssql",
"connection-string": "Server=tcp:myserver.database.windows.net,1433;Initial Catalog=MyDatabase;User ID=MyUser;Password=MyPassword;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;",
"options": {
"set-session-context": true
}
}
Note
We use SqlClient for Azure SQL and SQL Server, which supports these connection strings variants.
消费 SESSION_CONTEXT
对于 Azure SQL 和 SQL Server,数据 API 生成器可以包含 SQL 中的 SESSION_CONTEXT声明信息。
CREATE PROC GetUser @userId INT AS
BEGIN
-- Use claims
IF SESSION_CONTEXT(N'user_role') = 'admin'
BEGIN
RAISERROR('Unauthorized access', 16, 1);
END
SELECT Id, Name, Age, IsAdmin
FROM Users
WHERE Id = @userId;
END;
示例:Azure Cosmos DB
"data-source": {
"database-type": "cosmosdb_nosql",
"connection-string": "@env('SQL_CONNECTION_STRING')",
"options": {
"database": "Your_CosmosDB_Database_Name",
"container": "Your_CosmosDB_Container_Name",
"schema": "Path_to_Your_GraphQL_Schema_File"
}
}
Note
指定的database“选项”(和containerschema)特定于 Azure Cosmos DB。
Environment variables
使用环境变量将纯文本机密保留在配置文件中。
Tip
数据 API 生成器支持 @env() function.env](../reference-functions.md#env) and [ 的文件。
"data-source": {
"database-type": "mssql",
"connection-string": "@env('SQL_CONNECTION_STRING')"
}
Connection resiliency
数据 API 生成器使用指数退避在暂时性错误后重试数据库请求。
| Attempts | First | Second | Third | Fourth | Fifth |
|---|---|---|---|---|---|
| Seconds | 2s | 4s | 8s | 16s | 32s |
托管服务标识 (MSI)
库中定义的Azure.Identity托管服务标识(MSI)受DefaultAzureCredential支持。 详细了解 Azure SQL 的 Microsoft Entra 中的托管标识。
User-Assigned 托管标识(UAMI)
For User Assigned Managed Identity, append the Authentication and User Id properties to your connection string while substituting in your User Assigned Managed Identity's client id: Authentication=Active Directory Managed Identity; User Id=<UMI_CLIENT_ID>;.
System-Assigned 托管标识(SAMI)
For System Assigned Managed Identity, append the Authentication property and exclude the UserId and Password arguments from your connection string: Authentication=Active Directory Managed Identity;.
运行状况(数据源)
| Parent | Property | 类型 | Required | Default |
|---|---|---|---|---|
data-source |
health |
对象 | No | – |
数据 API 生成器支持多个配置文件,每个配置文件都有自己的数据源。 此配置块允许每个数据源都有自己的运行状况配置。
Nested properties
| Parent | Property | 类型 | Required | Default |
|---|---|---|---|---|
data-source.health |
enabled |
boolean | No | true |
data-source.health |
name |
字符串 | No | database-type |
data-source.health |
threshold-ms |
整数 | No | 1000 |
Check name
由于多个配置文件可以指向同一类型的数据源,因此无法在运行状况报告中区分这些数据源。 用于 name 分配仅在运行状况报告中使用的唯一可识别标签。
Check behavior
针对给定数据源执行最简单的查询(特定于数据库类型),以验证是否可以打开连接。 使用该 threshold-ms 属性配置该查询完成的最大可接受持续时间(以毫秒为单位)。
Format
{
"data-source": {
"health": {
"enabled": <true> (default) | <false>,
"name": <string>,
"threshold-ms": <integer; default: 1000>
}
}
}