[英]Query limited relations using typeorm(OneToMany) relations?
I use OneToMany
relations for adding messages
under conversations
.我使用
OneToMany
关系在conversations
下添加messages
。 Here, I want to query conversation with last messages only.在这里,我只想查询与最后一条消息的对话。
Here is an example-这是一个例子-
const conversation = await this.conversationRepository.find({
where: [
{ user: { id: reqUser.id }, hide_from_user: false },
{ participant: { id: reqUser.id }, hide_from_participant: false }
],
order: {
updated_at: "DESC"
},
relations: {
user: true,
participant: true,
message: true
}
});
It returns all messages
under particular conversation
-它返回特定
conversation
下的所有messages
-
But I need only last message
.但我只需要最后
message
。 Then how I have to query it?那我怎么查询呢? Please help me, here?
请帮帮我,这里?
TypeOrm doesn't support that directly, to do that you have to make another query TypeOrm 不直接支持,为此你必须进行另一个查询
import { getRepository } from "typeorm";
const conversation = await this.conversationRepository.find({
where: [
{ user: { id: reqUser.id }, hide_from_user: false },
{ participant: { id: reqUser.id }, hide_from_participant: false }
],
order: {
updated_at: "DESC"
},
relations: {
user: true,
participant: true,
}
});
conversation.messages = await getRepository(Message)
.createQueryBuilder('message')
.where('message.conversation = :id' , {conversation.id});
.limit(1)
.orderBy('message.id', 'Desc')
.getMany()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.