Entities

数据库实体的配置设置。

Health

Property Description
entities.entity-name.health.enabled 为实体启用运行状况检查终结点
entities.entity-name.health.first 运行状况检查查询中返回的行数
entities.entity-name.health.threshold-ms 运行状况检查查询的最大持续时间(以毫秒为单位)

Source

Property Description
entities.entity-name.source.type 对象类型:table、或 viewstored-procedure
entities.entity-name.source.object 数据库对象的名称
entities.entity-name.source.parameters 存储过程或函数的参数
entities.entity-name.source.key-fields 视图的主键字段列表
entities.entity-name.mappings 将 API 字段名称映射到数据库列

REST

Property Description
entities.entity-name.rest.enabled 为此实体启用 REST
entities.entity-name.rest.path REST 终结点的自定义路由
entities.entity-name.rest.methods 允许的 REST 方法:get、、postputpatchdelete

GraphQL

Property Description
entities.entity-name.graphql.type 使用 singular 键入名称或对象 plural
entities.entity-name.graphql.operation 作类型: querymutation
entities.entity-name.graphql.enabled 为此实体启用 GraphQL

Permissions

Property Description
entities.entity-name.permissions[].role 角色名称字符串
entities.entity-name.permissions[].actions 一个或多个:create、、readupdatedeleteexecute

Relationships

Property Description
entities.entity-name.relationships.relationship-name.cardinality onemany
entities.entity-name.relationships.relationship-name.target.entity 目标实体的名称
entities.entity-name.relationships.relationship-name.source.fields 关系中使用的此实体的字段
entities.entity-name.relationships.relationship-name.target.fields 来自目标实体的字段
entities.entity-name.relationships.relationship-name.linking.object 用于多对多关系的联接对象
entities.entity-name.relationships.relationship-name.linking.source.fields 联接中使用的源实体的字段
entities.entity-name.relationships.relationship-name.linking.target.fields 联接中使用的目标实体的字段

Cache

Property Description
entities.entity-name.cache.enabled 为实体启用响应缓存
entities.entity-name.cache.ttl-seconds 缓存生存时间(以秒为单位)

Format overview

{
  "entities": {
    "{entity-name}": {
      "rest": {
        "enabled": <boolean> // default: true
        "path": <string> // default: "{entity-name}"
        "methods": ["GET", "POST"] // default: ["GET", "POST"]
      },
      "graphql": {
        "enabled": <boolean> // default: true
        "type": {
          "singular": <string>,
          "plural": <string>
        },
        "operation": "query" | "mutation" // default: "query"
      },
      "source": {
        "object": <string>,
        "type": "view" | "stored-procedure" | "table",
        "key-fields": [<string>], // primary keys for the view
        "parameters": { // only for stored-procedure
          "<parameter-name>": <string | number | boolean>,
          "<parameter-name>": <string | number | boolean>
        }
      },
      "mappings": {
        "<database-field-name>": <string>
      },
      "relationships": {
        "<relationship-name>": {
          "cardinality": "one" | "many",
          "target.entity": <string>,
          "source.fields": [<string>],
          "target.fields": [<string>],
          "linking.object": <string>,
          "linking.source.fields": [<string>],
          "linking.target.fields": [<string>]
        }
      },
      "permissions": [
        {
          "role": "anonymous" | "authenticated" | <custom-role>,
          "actions": ["create", "read", "update", "delete", "execute", "*"],
          "fields": {
            "include": [<string>],
            "exclude": [<string>]
          },
          "policy": {
            "database": <string>
          }
        }
      ]
    }
  }
}

源(实体名称实体)

Parent Property 类型 Required Default
entities.{entity-name} source 对象 ✔️ Yes None

实体的数据库源详细信息。

Nested properties

Parent Property 类型 Required Default
entities.{entity-name}.source object 字符串 ✔️ Yes None
entities.{entity-name}.source type 枚举 (table, , viewstored-procedure ✔️ Yes None
entities.{entity-name}.source key-fields string array ✔️ Yes* None
entities.{entity-name}.source parameters 对象 ✔️ Yes** None
  • key-fields 仅当 typeview. 该值表示主键。

** parameters 仅当 typestored-procedure. 可以省略可选参数。

Tip

如果对象属于 dbo 架构,则指定架构是可选的。 此外,如果需要,可以使用对象名称周围的方括号(例如, dbo.Users 与) [dbo].[Users]

支持的参数数据类型:

  1. string— 包括 ANSI 和 Unicode;也用于 JSON/Date/DateTime 类型
  2. number— 包括整型和小数
  3. booleantruefalse

Format

{
  "entities": {
    "{entity-name}": {
      "source": {
        "object": <string>,
        "type": <"view" | "stored-procedure" | "table">,
        "key-fields": [ <string> ], // primary keys of the view
        "parameters": { // only for stored-procedure
          "<parameter-name-1>": <string | number | boolean>,
          "<parameter-name-2>": <string | number | boolean>
        }
      }
    }
  }
}

权限(实体名称实体)

Parent Property 类型 Required Default
entities.permissions role 字符串 ✔️ Yes None

一个字符串,指定权限应用到的角色的名称。

Format

{
  "entities": {
    "{entity-name}": {
      "permissions": [
        {
          "role": <"anonymous" | "authenticated" | "custom-role">
        }
      ]
    }
  }
}

Example

此示例定义仅read对实体具有权限User的角色custom-role

{
  "entities": {
    "User": {
      "permissions": [
        {
          "role": "custom-role",
          "actions": ["read"]
        }
      ]
    }
  }
}

Usage examples

GET https://localhost:5001/api/User
Authorization: Bearer <your_access_token>
X-MS-API-ROLE: custom-role

作(字符串数组权限实体名称实体)

Parent Property 类型 Required Default
entities.permissions actions oneOf [string, array] ✔️ Yes None

一个字符串数组,详细说明关联角色允许的作。

Action SQL Operation
* All actions
create 插入一个或多个* 行
read 选择一行或多行
update 修改一个或多个* 行
delete 删除一个或多个* 行
execute 运行存储过程

* GraphQL 目前仅支持多个作。

Note

对于存储过程,通配符(*)操作仅扩展到 execute 操作。 对于表和视图,它扩展到 createreadupdatedelete

Format

{
  "entities": {
    "{entity-name}": {
      "permissions": [
        {
          "actions": [ <string> ]
        }
      ]
    }
  }
}

Example

{
  "entities": {
    "{entity-name}": {
      "permissions": [
        {
          "actions": [ "*" ] // equivalent to create, read, update, delete
        }
      ]
    }
  }
}

备用格式(仅字符串,当 type=stored-procedure

{
  "entities": {
    "{entity-name}": {
      "permissions": [
        {
          "actions": <string>
        }
      ]
    }
  }
}

Example

{
  "entities": {
    "{entity-name}": {
      "permissions": [
        {
          "actions": "*" // equivalent to execute
        }
      ]
    }
  }
}

作(对象数组权限实体名称实体)

Parent Property 类型 Required Default
entities.{entity-name}.permissions actions string array ✔️ Yes None

一个对象数组,详细说明了关联角色允许的作。

Note

对于存储过程,通配符(*)操作仅扩展到 execute。 对于表/视图,它扩展到 createreadupdatedelete

Nested properties

Parent Property 类型 Required Default
entities.{entity-name}.permissions.actions[] action 字符串 ✔️ Yes None
entities.{entity-name}.permissions.actions[] fields 对象 ❌ 否 None
entities.{entity-name}.permissions.actions[] policy 对象 ❌ 否 None
entities.{entity-name}.permissions.actions[].policy database 字符串 ✔️ Yes None

Format

{
  "entities": {
    "{entity-name}": {
      "permissions": [
        {
          "role": <string>,
          "actions": [
            {
              "action": <string>,
              "fields": <array of strings>,
              "policy": <object>
            }
          ]
        }
      ]
    }
  }

Example

这会授予readauditorUser实体的权限,并具有字段和策略限制。

{
  "entities": {
    "User": {
      "permissions": [
        {
          "role": "auditor",
          "actions": [
            {
              "action": "read",
              "fields": {
                "include": ["*"],
                "exclude": ["last_login"]
              },
              "policy": {
                "database": "@item.IsAdmin eq false"
              }
            }
          ]
        }
      ]
    }
  }
}

Policy notes

  • 策略支持 OData 运算符,例如 eq
  • 策略支持使用 andor.
  • 仅支持作:createreadupdatedelete。 (不 execute
  • 策略筛选结果,但不阻止数据库中的查询执行。
  • 字段必须使用字段别名(如果已映射)。

类型(GraphQL 实体名称实体)

Parent Property 类型 Required Default
entities.{entity-name}.graphql type 对象 ❌ 否 {entity-name}

设置 GraphQL 架构中实体的命名约定。

Format

{
  "entities": {
    "{entity-name}": {
      "graphql": {
        "type": {
          "singular": "<string>",
          "plural": "<string>"
        }
      }
    }
  }
}

Nested properties

Parent Property Required 类型 Default
entities.{entity-name}.graphql.type singular ❌ 否 字符串 None
entities.{entity-name}.graphql.type plural ❌ 否 字符串 N/A (默认值为单一值)

Example

Configuration

{
  "entities": {
    "User": {
      "graphql": {
        "type": {
          "singular": "User",
          "plural": "Users"
        }
      }
    }
  }
}

GraphQL query

{
  Users {
    items {
      id
      name
      age
      isAdmin
    }
  }
}

GraphQL response

{
  "data": {
    "Users": {
      "items": [
        {
          "id": 1,
          "name": "Alice",
          "age": 30,
          "isAdmin": true
        },
        {
          "id": 2,
          "name": "Bob",
          "age": 25,
          "isAdmin": false
        }
        // ...
      ]
    }
  }
}

作(GraphQL 实体名称实体)

Parent Property 类型 Required Default
entities.{entity-name}.graphql operation enum string ❌ 否 mutation

指定作是否stored-procedure显示在作的下方QueryMutation

Note

{entity-name}.type 设置为 stored-procedure时,将自动创建新的 GraphQL 类型 executeXXX。 此属性 operation 控制此类型放置在 GraphQL 架构中的位置。 没有功能影响,只是架构卫生。

Format

{
  "entities": {
    "{entity-name}": {
      "graphql": {
        "operation": "query" | "mutation"
      }
    }
  }
}

Example: operation

何时 operation 设置为 query

type Query {
  executeGetUserDetails(userId: Int!): GetUserDetailsResponse
}

何时 operation 设置为 mutation

type Mutation {
  executeGetUserDetails(userId: Int!): GetUserDetailsResponse
}

已启用 (GraphQL 实体名称实体)

Parent Property 类型 Required Default
entities.{entity-name}.graphql enabled boolean ❌ 否 True

允许开发人员选择性地将实体包含在 GraphQL 架构中。

Format

{
  "entities": {
    "{entity-name}": {
      "graphql": {
        "enabled": <true> (default) | <false>
      }
    }
  }
}

REST (实体名称实体)

Parent Property 类型 Required Default
entities.{entity-name}.rest enabled boolean ❌ 否 True
entities.rest path 字符串 ❌ 否 /{entity-name}
entities.{entity-name}.rest methods string array ❌ 不* POST

* 该 methods 属性仅适用于 stored-procedure 终结点。

Format

{
  "entities": {
    "{entity-name}": {
      "rest": {
        "enabled": <true> (default) | <false>,
        "path": <string; default: "{entity-name}">
      }
    }
  }
}

映射(实体名称实体)

Parent Property 类型 Required Default
entities.{entity-name} mappings 对象 ❌ 否 None

为数据库对象字段启用自定义别名或公开的名称。

Important

对于启用了 GraphQL 的实体,配置的公开名称必须满足 GraphQL 名称要求

Format

{
  "entities": {
    "{entity-name}": {
      "mappings": {
        "<field-1-name>": "<field-1-alias>",
        "<field-2-name>": "<field-2-alias>",
        "<field-3-name>": "<field-3-alias>"
      }
    }
  }
}

Examples

Database Table

CREATE TABLE Books
(
  id INT,
  sku_title VARCHAR(50),
  sku_status VARCHAR(50),
)

Configuration

{
  "entities": {
    "Books": {
      ...
      "mappings": {
        "sku_title": "title",
        "sku_status": "status"
      }
    }
  }
}

缓存(实体名称实体)

Parent Property 类型 Required Default
entities.{entity-name} cache 对象 ❌ 否 None

启用和配置实体的缓存。

Nested properties

Parent Property 类型 Required Default
entities.{entity-name}.cache enabled boolean ❌ 否 False
entities.{entity-name}.cache ttl-seconds 整数 ❌ 否 -

Format

{
  "entities": {
    "{entity-name}": {
      "cache": {
        "enabled": <true> (default) | <false>,
        "ttl-seconds": <integer; default: 5>
      }
    }
  }
}

Note

如果未指定, ttl-seconds 则继承设置的 runtime.cache全局值。

Example

{
  "entities": {
    "Author": {
      "cache": {
        "enabled": true,
        "ttl-seconds": 30
      }
    }
  }
}

关系(实体名称实体)

Parent Property 类型 Required Default
entities.{entity-name} relationships 对象 ❌ 否 None

配置 GraphQL 实体与其他公开实体的关系。 有关详细信息,请参阅 数据 API 生成器关系细分

Note

relationship-name每个关系的属性在该实体的所有关系中必须是唯一的。

Nested properties

这些属性在不同的组合中使用,具体取决于关系基数。

Parent Property 类型 Required Default
entities.{entity-name}.relationships cardinality 字符串 ✔️ Yes None
entities.{entity-name}.relationships target.entity 字符串 ✔️ Yes None
entities.{entity-name}.relationships target.fields string array ❌ 否 None
entities.{entity-name}.relationships source.fields string array ❌ 否 None
entities.{entity-name}.relationships linking.object 字符串 ❌ 否 None
entities.{entity-name}.relationships linking.source.fields string array ❌ 否 None
entities.{entity-name}.relationships linking.target.fields string array ❌ 否 None

Format

{
  "entities": {
    "{entity-name}": {
      "relationships": {
        "<relationship-name>": {
          "cardinality": "one" | "many",
          "target.entity": "<string>",
          "source.fields": ["<string>"],
          "target.fields": ["<string>"],
          "linking.object": "<string>",
          "linking.source.fields": ["<string>"],
          "linking.target.fields": ["<string>"]
        }
      }
    }
  }
}
Relationship Cardinality Example
one-to-many many 一个类别实体可以与多个待办事项实体关联
many-to-one one 许多待办事项实体可能与一个类别实体相关
many-to-many many 一个待办事项实体可以与多个用户实体关联,一个用户实体可以与多个待办实体关联

示例:一对一基数

每个 Profile 都与一个 User完全相关,每个都 User 只有一个相关 Profile

{
  "entities": {
    "User": {
      "relationships": {
        "user_profile": {
          "cardinality": "one",
          "target.entity": "Profile",
          "source.fields": [ "id" ],
          "target.fields": [ "user_id" ]
        }
      }
    },
    "Profile": {
      ...
    }
  }
}

GraphQL schema

type User
{
  id: Int!
  ...
  profile: Profile
}

Command-line

dab update User \
  --relationship profile \
  --target.entity Profile \
  --cardinality one \
  --relationship.fields "id:user_id"

示例:一对多基数

A Category 可以有一个或多个相关 Book 实体,而每个实体 Book 可以有一个相关 Category实体。

{
  "entities": {
    "Book": {
      ...
    },
    "Category": {
      "relationships": {
        "category_books": {
          "cardinality": "many",
          "target.entity": "Book",
          "source.fields": [ "id" ],
          "target.fields": [ "category_id" ]
        }
      }
    }
  }
}

GraphQL schema

type Category
{
  id: Int!
  ...
  books: [BookConnection]!
}

Command line

dab update Category \
  --relationship category_books \
  --target.entity Book \
  --cardinality many \
  --relationship.fields "id:category_id"

示例:多对一基数

许多 Book 实体可以有一个相关 Category项,而一 Category 个实体可以有一个或多个相关 Book 条目。

{
  "entities": {
    "Book": {
      "relationships": {
        "books_category": {
          "cardinality": "one",
          "target.entity": "Category",
          "source.fields": [ "category_id" ],
          "target.fields": [ "id" ]
        }
      },
      "Category": {
        ...
      }
    }
  }
}

GraphQL schema

type Book
{
  id: Int!
  ...
  category: Category
}

Command line

dab update Book \
  --relationship books_category \
  --target.entity "Category" \
  --cardinality one \
  --relationship.fields "category_id:id"

示例:多对多基数

许多 Book 实体可以具有许多相关 Author 实体,而许多 Author 实体可以具有许多相关 Book 条目。

Note

This relationship is possible with a third table, dbo.books_authors, which we refer to as the linking object.

{
  "entities": {
    "Book": {
      "relationships": {
        ...,
        "books_authors": {
          "cardinality": "many",
          "target.entity": "Author",
          "source.fields": [ "id" ],
          "target.fields": [ "id" ],
          "linking.object": "dbo.books_authors",
          "linking.source.fields": [ "book_id" ],
          "linking.target.fields": [ "author_id" ]
        }
      },
      "Category": {
        ...
      },
      "Author": {
        ...
      }
    }
  }
}

GraphQL schema

type Book
{
  id: Int!
  ...
  authors: [AuthorConnection]!
}

type Author
{
  id: Int!
  ...
  books: [BookConnection]!
}

Command line

dab update Book \
  --relationship books_authors \
  --target.entity "Author" \
  --cardinality many \
  --relationship.fields "id:id" \
  --linking.object "dbo.books_authors" \
  --linking.source.fields "book_id" \
  --linking.target.fields "author_id"

运行状况(实体名称实体)

Parent Property 类型 Required Default
entities.{entity-name} health 对象 ❌ 否 None

启用和配置实体的运行状况检查。

Nested properties

Parent Property 类型 Required Default
entities.{entity-name}.health enabled boolean ❌ 否 True
entities.{entity-name}.health first 整数 ❌ 否 1
entities.{entity-name}.health threshold-ms 整数 ❌ 否 5

Format

{
  "entities": {
    "{entity-name}": {
      "health": {
        "enabled": true,
        "first": 5,
        "threshold-ms": 2000
      }
    }
  }
}

Example

{
  "entities": {
    "Book": {
      "health": {
        "enabled": true,
        "first": 3,
        "threshold-ms": 500
      }
    }
  }
}