'How to transfer a filter to a query (TypeORM)
I want to get rid of the filter and put it in the request body. I tried using where, how can I do it?
Questionnaire entity like:
@OneToMany(() => LikeEntity, (like) => like.questionnaire)
@JoinTable()
likes: LikeEntity[];
@OneToMany(() => DislikeEntity, (dislike) => dislike.questionnaire)
@JoinTable()
dislikes: DislikeEntity[];
Code:
const questionnaires = await this.questionnairesRepository.find({
relations: ['likes', 'dislikes'],
where: [
{
userId: Not(questionnaire.userId),
likes: {
id: 12,
},
},
],
order: {
id: 'ASC',
},
});
if (!questionnaires)
throw new HttpException('Questionnaires not found', HttpStatus.NOT_FOUND);
return questionnaires
.filter((questionnaire) => {
return (
!questionnaire.likes.find((like) => like.ownerUserId == userId) &&
!questionnaire.dislikes.find((like) => like.ownerUserId == userId)
);
})
.slice(0, 10);
Solution 1:[1]
try using createQueryBuilder
, you can left join or inner join table easier
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Hai Alison |