NodeJS typeorm得到树结构与在哪里和关系

wko9yo5t  于 2023-08-04  发布在  Node.js
关注(0)|答案(1)|浏览(189)

我有一个实体myobject,它是一个树实体,还包含与commentstagsuser的关系。如何查询此实体并获取关系?

@Entity('MyObject')
@Tree('closure-table')
export class MyObject {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column()
  name: string;

  @TreeChildren()
  childObjects: MyObject[];

  @TreeParent()
  parentObject: MyObject;

  @ManyToOne(() => User, (user) => user.objects)
  user: User;

  @OneToMany(() => Comment, (comment) => comment.object)
  comments: Comment[];

  @ManyToMany(() => Tags, (tags) => tags.objects)
  tags: Tags[];
}

字符串
我的第一个非树查询是这样的:

this.objectRepository
      .createQueryBuilder('object')
      .leftJoin('object.tags', 'tags')
      .leftJoinAndSelect('object.childrenObjects', 'childrenObjects')
      .addSelect(['tags.id'])
      .leftJoinAndSelect('object.comments', 'comments')
      .where('object.user.id = :id', { id: userId })
      .getMany();


这得到了我想要的一切,但没有深层的树结构。我知道我可以用getManager().getTreeRepository(MyObject).findTrees().then()得到树结构,但是缺少了关系和位置。
那么,如何查询这些数据呢?

wn9m85ua

wn9m85ua1#

至少关系部分可以通过使用
查找树({关系:['标签']})
您将获得嵌套的树结构,其中包含相关对象的每个节点都有一个tags属性。
对于where部分,我还没有找到解决方案,所以我只是递归地遍历树来得到我想要的。

相关问题