Nestjs 使用TypeForm选择有限数量的关系(一个多个)
我想知道使用TypeForm选择固定数量关系的最佳方法,例如,如果我有UsersEntity和PublicationsEntity,用户有100多个出版物,我只想选择其中10个,实现这一点的最佳方法是什么 几年前有人问过类似的问题,但我一直没能做到: 没有任何限制,我的代码如下所示:Nestjs 使用TypeForm选择有限数量的关系(一个多个),nestjs,typeorm,Nestjs,Typeorm,我想知道使用TypeForm选择固定数量关系的最佳方法,例如,如果我有UsersEntity和PublicationsEntity,用户有100多个出版物,我只想选择其中10个,实现这一点的最佳方法是什么 几年前有人问过类似的问题,但我一直没能做到: 没有任何限制,我的代码如下所示: return await getRepository(UsersEntity) .createQueryBuilder('user') .where('user.userName =
return await getRepository(UsersEntity)
.createQueryBuilder('user')
.where('user.userName = :userName', { userName })
.leftJoinAndSelect('user.publications', 'publications')
.leftJoinAndSelect('publications.category', 'category')
.leftJoinAndSelect('publications.comments', 'comments')
.leftJoinAndSelect('comments.children', 'children')
.leftJoinAndSelect('publications.likes', 'likes')
.getOne();
您可以通过两个查询按照他们在本主题中提到的方式进行操作,如下所示:
const user = await getRepository(UsersEntity).createQueryBuilder('articles')
.where("user.userName = :userName", { userName })
.getOne()
user.publications = await getRepository(PublicationsEntity)
.createQueryBuilder("publication")
.where("publication.colmunuserid=:userId", { userId: user.id }) // replace colmunuserid with the correct name of the column
.leftJoinAndSelect('publication.category', 'category')
.leftJoinAndSelect('publication.comments', 'comments')
.leftJoinAndSelect('comments.children', 'children')
.leftJoinAndSelect('publication.likes', 'likes')
.limit(10)
.getMany()
return user;
您可以通过两个查询按照他们在本主题中提到的方式进行操作,如下所示:
const user = await getRepository(UsersEntity).createQueryBuilder('articles')
.where("user.userName = :userName", { userName })
.getOne()
user.publications = await getRepository(PublicationsEntity)
.createQueryBuilder("publication")
.where("publication.colmunuserid=:userId", { userId: user.id }) // replace colmunuserid with the correct name of the column
.leftJoinAndSelect('publication.category', 'category')
.leftJoinAndSelect('publication.comments', 'comments')
.leftJoinAndSelect('comments.children', 'children')
.leftJoinAndSelect('publication.likes', 'likes')
.limit(10)
.getMany()
return user;
这个查询和子查询的性能有点不同?我想是的,顺便说一句,我正在研究一个解决方案,在一个查询中处理极限关系,我会更新我的答案,并在得到新的解决方案时通知您,直到现在这个解决方案工作得很好,好运气这和子查询的性能有点不同?我想是的,顺便说一句,我正在研究一个解决方案,在一个查询中处理极限关系,我会更新我的答案,并在我得到新的解决方案时通知你,直到现在这个解决方案工作得很好,祝你好运