【问题标题】:Prisma - When use include return only subobject fieldsPrisma - 使用时仅返回子对象字段
【发布时间】:2021-09-19 17:14:07
【问题描述】:

我编写了这个函数来过滤表building 结束可选我可以传递一个Prisma.BuildingInclude 对象来返回一个或多个子对象。

async describeEntity(filter: Filter, include?: Prisma.BuildingInclude): Promise<CCResponse> {
  try {
    const entity = await this.prisma.building.findMany({
      where: this.queryCondition(filter),
      include,
    });

    return new CCResponse('OK', entity);
  }
  catch (err) {
    console.log(err);
    return new CCResponse('INTERNAL_ERROR', this.content['GeneralError']);
  }
}

问题是,如果我将include 参数传递到棱镜响应中,我还有building 的字段。存在include 参数时,如何仅返回子对象字段?

【问题讨论】:

    标签: typescript prisma


    【解决方案1】:

    您可以将include 条件替换为select 条件来解决此问题。

    例如,要查找许多具有完全相同 where 条件的 building 记录,但只返回所需的关系子对象字段,查询可能如下所示

     const entity = await this.prisma.building.findMany({
          where: this.queryCondition(filter),
          select: {
                relationSubObjectOne: true,  //change relationSubObjectOne to appropriate relation name..
                relationSubObjectTwo: true,  //change relationSubObjectTwo to appropriate relation name..
                // ... other fields/subobjects you might be interested in.
            },
        });
    
    

    您可以在 Prisma 文档中Select fields 文章的包含关系和选择关系字段小节中了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-08
      • 2018-11-02
      • 2020-03-10
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多