NodeJS 我想在typeorm中得到多重关系,请告诉我一些方法

gk7wooem  于 2022-12-18  发布在  Node.js
关注(0)|答案(1)|浏览(97)
const announcement = await this.announsRequestsRepo.findOne({
        where: { id },
        relations: {
          owner: true,
          category: true
        }
      })

我想得到范畴的关系等等

l7mqbcuq

l7mqbcuq1#

有两种方法:query builderfind方法

查找

const announcement = await this.announsRequestsRepo.findOne({
        relations: {
          owner: {
            friends: true
          },
          category: {
            subcategories: true
          }
        },
        where: { id }
})

查询生成器

return await this.announsRequestsRepo
      .createQueryBuilder('announcement')
      .leftJoinAndSelect('announcement.owner', 'owner')
      .leftJoinAndSelect('announcement.category', 'category')

和使用查询生成器的深度嵌套:

return await this.announsRequestsRepo
          .createQueryBuilder('announcement')
          .innerJoinAndSelect('announcement.owner', 'owner')
          .leftJoinAndSelect('owner.friends', 'friends')


引用:查找方法:enter link description here查询构建器:在此输入链接说明

相关问题