飞布产品手册
官网B站Github
V2.0
V2.0
  • 序言
  • 更新日志
    • 更新日志V2.0
    • V2.0更新说明
    • 更新日志V1.0
  • 产品简介
    • 什么是飞布?
    • 飞布的价值
    • 飞布的优势
    • 应用场景
    • 数据安全
    • 产品案例
  • 快速入门
    • 初识飞布
    • 快速上手
      • 图文版
    • 词汇概览
    • 工作原理
  • 基础-可视化开发
    • 概览
      • CLI
      • 控制台
        • 主功能区
    • 数据源
      • 数据库
        • 数据库连接
          • 高级设置
        • 数据建模
        • 数据预览
      • Prisma 数据源
      • REST 数据源
      • GraphQL 数据源
      • 消息队列
    • API构建
      • 可视化构建
        • API规范
      • 批量新建
      • HTTP请求流程指令
      • 使用API
      • 实时查询
      • 实时推送
      • 关联查询
      • 数据缓存
      • 常见用例
    • 身份验证
      • 授权码模式
        • 身份验证(废弃)
      • 隐式模式
      • 数据权限控制
    • 身份授权
      • RBAC
        • 授权与访问控制(废弃)
      • 接口权限控制
      • 开放API
    • 文件存储
      • S3配置及使用
      • 文件管理面板
      • 高级配置:profile
  • 进阶-钩子机制
    • 钩子概览
    • 启动钩子
      • Node钩子
      • Golang钩子
      • Java钩子
      • Python钩子
    • OPERATION钩子
    • 身份验证钩子
    • graphql钩子
    • 函数钩子
      • function
      • proxy
    • 文件上传钩子
    • 内部调用
  • 使用-部署上线
    • 部署运维
      • 手动部署
        • 流水线部署(废弃)
      • Docker部署
      • 飞布云
      • sealos部署
    • 接口安全
      • CSRF token 保护
      • 跨域访问
    • 客户端SDK
      • Js SDK
      • 微信小程序SDK
      • Flutter SDK
      • uniapp SDK
    • 性能测试
  • 迁移到V2
  • 环境准备
    • 文件存储 S3
    • 身份认证 OIDC
    • NodeJs环境
  • 实战案例
    • Amis Admin
      • 管理后台-refine(废弃)
    • 实时TODO LIST
    • 语音版ChatGPT
    • AI魔法师实战
    • 阿里低代码引擎
    • appsmith集成
  • Roadmap
  • 常见问题
  • 核心概念
    • GraphQL
    • 超图
    • 请求时序图
    • 服务端Operation
  • 二次开发
    • 钩子规范
      • 钩子规范bak
    • 模板规范
    • 自定义模板
    • 其它参考
由 GitBook 提供支持
在本页
  • 组装查询
  • QUERY
  • MUTATION
  • 事务
  • 分页
  • 模糊搜索
  • 关联查询
  • 关联更新
  • 可为空字段
  • null查询
  • null更新
  • 跨源关联
  • 原生sql
  • queryRaw
  • executeRaw
  • 变量默认值
  • 默认值
  • 必填项

这有帮助吗?

在GitHub上编辑
  1. 基础-可视化开发
  2. API构建

常见用例

组装查询

QUERY

在一个接口中,同时拿到多个数据源的数据。

query MyQuery{
  # mysql 数据源
  mysql_findFirstPost {
    createdAt
    id
  }
  # pgsql 数据源
  pgsql_findFirstTodo {
    completed
    content
    createdAt
  }
  # rest 数据源
  system_getAllRoles {
    code
    remark
  }
}

MUTATION

在一个接口中,同时变更多个数据源的数据。

mutation MyQuery {
  # mysql创建数据
  mysql_createOneTodo(data: {title: "test"}) {
    id
  }
  # mysql删除数据
  mysql_deleteOneTodo(where: {id: 10}) {
    id
  }
  # pgsql删除数据
  pgsql_deleteOneTodo(where: {id: 10}) {
    id
  }
}

事务

用@transaction指令修饰mutation OPERATION,保证原子性

mutation MyQuery @transaction {
  rb_createOneT(data: { name: "22211122"}) {
    id
    name
  }
  rb_createOneRole(data: {code: "a111111", name: "1111"}) {
    code
    name
  }
} 

分页

query GetTodoList(
  $take: Int = 10, $skip: Int = 0, # 分页 skip=(page-1)*take
  $orderBy: [todo_TodoOrderByWithRelationInput], # 排序
  $query: todo_TodoWhereInput) { # 查询条件,支持模糊搜索
  # 获取列表
  data: todo_findManyTodo(skip: $skip take: $take orderBy: $orderBy where: {AND: $query}) {
    id
    title
    completed
    createdAt
  }
  # 获取记录数
  total: todo_aggregateTodo(where: {AND: $query}) @transform(get: "_count.id") {
    _count {
      id
    }
  }
}

模糊搜索

模糊搜索,对应sql like 匹配,例如:根据$title模糊搜索待做事项列表

query MyQuery($title: String, $skip: Int!) {
  todo_findFirstTodo(skip: $skip, take: 10, where: {title: {contains: $title}}) {
    createdAt
    completed
    id
    title
  }
}gr

关联查询

同一数据源的关联查询有两种情况:

  • 有外键:建立表之间的外键关联

  • 无外键: Prisma 数据源

其用法相同,如下:

query MyQuery {
  rb_findFirstUser {
    name
    uid
    Role {
      code
      name
    }
  }
}

关联更新

在更新某个对象数据的同时,关联更新其关联对象的数据,仅适用于1:1关联。

# 更新用户$name的同时,更新其profile的$bio字段。
mutation MyQuery( $id: Int!, $name: String, $bio: String) {
  mysql_updateOneUser(
    data: {name: {set: $name}, Profile: {update: {bio: {set: $bio}}}}
    where: {id: $id}
  ) {
    id
  }
}

可为空字段

对于可为空字段,支持用null来查询或更新。

model T {
  id   Int     @id @unique
  name String
  des  String? // 注意,这里的?
}

null查询

将null作为查询条件,例如:

1,获取所有des=null的数据

# 方法1
query MyQuery {
  rb_findManyT(where: {des: null}) {
    des
    id
  }
} 

# 方法2,不推荐使用
query MyQuery {
  rb_findManyT(where: {des: {equals: null}}) {
    des
    id
  }
}

2,获取所有des is not null的数据

query MyQuery {
  rb_findManyT(where: {des: {not:null}}) {
    des
    id
  }
}

null更新

设置某字段的值为null,例如:设置id为10的记录,des=null

mutation MyQuery($des: String = null) {
  rb_updateOneT(data: {des: {set: $des}}, where: {id: 10}) {
    id
    des
  }
}

跨源关联

原生sql

数据库内省的“函数”基于prisma构建,有些场景无法支持,如数据统计类需求。因此,支持raw sql,以实现任意复杂度的需求。

queryRaw

执行查询SQL,返回值是对象数组

# 用法1:界面上语法会报错,但实际上支持
mutation MyQuery($id:Int=1) {
  todo_queryRaw(query: "SELECT *,rowid \"NAVICAT_ROWID\" FROM \"main\".\"Todo\"  WHERE id=$1", parameters: [$id])
} 
# 用法2
mutation MyQuery($parameters: todo_Json = [1]) {
  todo_queryRaw(
    query: "SELECT *,rowid \"NAVICAT_ROWID\" FROM \"main\".\"Todo\"  WHERE id=$1"
    parameters: $parameters
  )
}

executeRaw

执行变更SQL,返回值是包含count字段的对象

# 用法1:界面上语法会报错,但实际上支持
mutation MyQuery($title: String = "beijing",$id:Int=1) {
  todo_executeRaw(
    query: "UPDATE \"main\".\"Todo\" SET \"title\" = $1 WHERE id=$2"
    parameters: [$title,$id]
  )
}
# 用法2
mutation MyQuery($parameters: todo_Json = ["beijing", 1]) {
  todo_executeRaw(
    query: "UPDATE \"main\".\"Todo\" SET \"title\" = $1 WHERE id=$2"
    parameters: $parameters
  )
}

变量默认值

默认值

设置$skip的默认值为0,请求接口时可忽略。

query MyQuery($skip: Int = 0 ) {
  rb_findManyUser(skip: $skip, take: 10) {
    uid
    name
  }
}

必填项

设置$skip为必填项,请求接口时必填。

query MyQuery($skip: Int! ) {
  rb_findManyUser(skip: $skip, take: 10) {
    uid
    name
  }
}
上一页数据缓存下一页身份验证

最后更新于1年前

这有帮助吗?

分页查询,支持排序和查询条件(例如模糊搜索)。具体用法参考:

通过 @export 和 _join 可以进行跨数据源的关联查询。详情见 。

跨源关联
#query-operation