iTranslated by AI
The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
😸
Conditional Method Chaining in TypeORM QueryBuilder
What I want to achieve
When building a QueryBuilder using method chaining, is there a way to add where clauses conditionally while keeping the code readable?
Current approach
searchQuery = repository
.createQueryBuilder("member")
.innerJoinAndSelect("member.age", "age");
if (status) {
searchQuery = searchQuery.andWhere("status = :status", { status });
}
Conclusion
.andWhere(status ? `status = :status` : '1=1', { status })
Discussion