postgresql 我可以在连接表中使用supplyselect()获得属性而不是记录数组吗?

cqoc49vn  于 2023-11-18  发布在  PostgreSQL
关注(0)|答案(1)|浏览(127)

看看schema visualizer:


的数据
这是我的请求:

const { data } = await supabase.from('teams').select(`
  *,
  members:profiles (
    *,
    roles (
      name
    ),
    members(
      isTeamLeader
    )
  )
`)

字符串
我得到的响应是一个具有以下结构的对象数组:

{
    "id": "team_id",
    "created_at": "timestamp",
    "name": "team_name",
    "members": [
        {
            "first_name": "profile_first_name",
            "last_name": "profile_last_name",
            "user_id": "user_id",
            "created_at": "timestamp",
            "id": "profile_id",
            "avatar_id": null,
            "bio": "profile_bio",
            "roles": [
                {
                    "name": "role_name"
                }
            ],
            "members": [
                {
                    "isTeamLeader": true
                }
            ]
        }
    ]
}


我想得到其他响应结构。特别是:

{
  "id": "team_id",
  "created_at": "timestamp",
  "name": "team_name",
  "members": [
      {
          "first_name": "profile_first_name",
          "last_name": "profile_last_name",
          "user_id": "user_id",
          "created_at": "timestamp",
          "id": "profile_id",
          "avatar_id": null,
          "bio": "profile_bio",
          "role_name": "role_name",
          "isTeamLeader": true
      }
  ]
}


我该如何改变我的请求以实现这一目标?

oyxsuwqo

oyxsuwqo1#

supabase-js使用PostgREST。根据PostgREST文档,您可以使用省略号...spread operator扩展嵌入的资源:

const { data } = await supabase.from('teams').select(`
  *,
  members:profiles (
    *,
    ...roles (
      name
    )&roles.limit=1,
    ...members(
      isTeamLeader
    )&members.limit=1
  )
`)

字符串

相关问题