如何防止 Prisma 在多对多关系查询中显示冗余/不必要的嵌套属性?

How to prevent Prisma from showing redundant/ unnecessarly nested properties in Many-To-Many-Relationship query?

我尝试检索用户和他所属的组织。包含组织的数组包含冗余/不必要的嵌套属性。

我有以下设置。

    model Organization {   
    id            Int   @id @default(autoincrement())   
    name          String   
    ownerId       Int   
    owner         User   
    
    model User {
      id                 Int                     @id @default(autoincrement())
      email              String                  @unique
      password           String
      organizations      MembersInOrganization[]
      ownedOrganizations Organization[]
    }
    
    model MembersInOrganization {
      organization   Organization @relation(fields: [organizationId], references: [id])
      organizationId Int
      user           User         @relation(fields: [userId], references: [id])
      userId         Int
    
      @@id([organizationId, userId])
    }            

我想检索用户和他所属的组织。 以下查询的结果类似于:

    let user = await prisma.user.findUnique({
                where: {
                    id:  verified.sub
                },
                 include: {
                    organizations: {
                        select: {
                            organization: true
                        }
                    }
                } 
            })        
    {
      "id": 1,
      "email": "someting@gmail.com",
      "password": "HASHED_PASSWORD",
      "organizations": [
        {
          "organization": {
            "id": 1,
            "name": "lol",
            "ownerId": 1
          }
        },
        {
          "organization": {
            "id": 2,
            "name": "Cool",
            "ownerId": 1
          }
        },
        {
          "organization": {
            "id": 3,
            "name": "Very cool",
            "ownerId": 1
          }
        },
      ]
    }

现在我的问题是如何防止数组中出现多余的 属性 名称? 所以结果看起来像这样:

    {
      "id": 1,
      "email": "someting@gmail.com",
      "password": "HASHED_PASSWORD",
      "organizations": [
        {
            "id": 1,
            "name": "lol",
            "ownerId": 1
          
        },
        {
            "id": 2,
            "name": "Cool",
            "ownerId": 1
          
        },
        {
       
            "id": 3,
            "name": "Very cool",
            "ownerId": 1
          
        },
      ]
    }

根据您的架构文件,本机无法删除 organizations 关键字。它需要通过遍历 organizations 数组来进一步处理。

您可以使用以下查询获得响应:

  let user = await prisma.user.findUnique({
    where: {
      id: 2,
    },
    include: {
      organizations: {
        select: {
          organization: {
            select: {
              name: true,
              ownerId: true,
              id: true,
            },
          },
        },
      },
    },
  });

  //@ts-ignore
  user.organizations = user?.organizations.map((org) => {
    //@ts-ignore
    return (org = { ...org.organization });
  });

  console.log(JSON.stringify(user, null, 2));

这是示例响应

{
  "id": 2,
  "email": "t@g.com",
  "password": "123456",
  "organizations": [
    {
      "name": "test",
      "ownerId": 2,
      "id": 1
    }
  ]
}