在数据 API 生成器中使用存储过程

存储过程可以作为 DAB 中的 REST 或 GraphQL 终结点公开。 这对于涉及不由简单表或视图处理的自定义逻辑、筛选、验证或计算结果的方案非常有用。

配置

公开存储过程:

  • source.type 设置为 "stored-procedure"
  • source.object 设置为完全限定的过程名称
  • 必要时定义可选parameters及其默认值
  • 设置 rest.methods (例如, "GET""POST") 或 rest: false
  • graphql.operation设置为"query""mutation",或省略以默认为"mutation"
  • 使用 "execute" 操作授予权限

CLI 示例

dab add GetCowrittenBooksByAuthor \
  --source dbo.stp_get_all_cowritten_books_by_author \
  --source.type "stored-procedure" \
  --source.params "searchType:default-value" \
  --permissions "anonymous:execute" \
  --rest.methods "get" \
  --graphql.operation "query"

配置示例

"GetCowrittenBooksByAuthor": {
  "source": {
    "type": "stored-procedure",
    "object": "dbo.stp_get_all_cowritten_books_by_author",
    "parameters": {
      "searchType": "default-value"
    }
  },
  "rest": {
    "methods": [ "GET" ]
  },
  "graphql": {
    "operation": "query"
  },
  "permissions": [
    {
      "role": "anonymous",
      "actions": [ "execute" ]
    }
  ]
}

REST 支持

  • 仅支持 GETPOST
  • 省略POST时,默认为methods
  • 使用GET通过查询字符串发送参数
  • 通过 JSON 正文发送参数 POST
  • 如果设置了"rest": false,将禁用存储过程的 REST 功能

示例请求

GET /api/GetCowrittenBooksByAuthor?author=asimov

POST /api/GetCowrittenBooksByAuthor

{
  "author": "asimov"
}

GraphQL 支持

  • 要求 graphql.operation"query""mutation"
  • 字段自动添加前缀 execute,例如 executeGetCowrittenBooksByAuthor
  • 参数将作为 GraphQL 参数进行传递

GraphQL 示例

query {
  executeGetCowrittenBooksByAuthor(author: "asimov") {
    id
    title
  }
}

局限性

  • 仅返回第一个结果集
  • 不支持分页、筛选和排序
  • 不支持关系
  • 需要来自 sys.dm_exec_describe_first_result_set 的元数据
  • 无法通过键返回单个项目
  • 无参数级授权