Performance: Query builder
Query builders affect server response time and data fetching speed, impacting how fast the page can load dynamic content.
const users = await this.userRepository.createQueryBuilder('user') .leftJoinAndSelect('user.posts', 'post') .getMany();
const users = await this.userRepository.find(); for (const user of users) { user.posts = await this.postRepository.find({ where: { userId: user.id } }); }
| Pattern | Database Queries | Server Processing | Network Payload | Verdict |
|---|---|---|---|---|
| N+1 Query Pattern | Multiple queries (1 + N) | High due to repeated queries | Larger due to multiple responses | [X] Bad |
| Optimized Query Builder | Single combined query | Low due to single query | Smaller and efficient | [OK] Good |
QueryBuilder in NestJS with TypeORM?User in NestJS with TypeORM?createQueryBuilder.createQueryBuilder correctly.users?
const users = await this.userRepository
.createQueryBuilder('user')
.where('user.age > :age', { age: 18 })
.getMany();age > 18, so only users older than 18 are selected.getMany() returns an array of matching users, not a single user or error.const user = await this.userRepository
.createQueryBuilder('user')
.where('user.name = :name', { username: 'Alice' })
.getOne();:name but the object key is username, causing mismatch.:name is undefined, causing query error or no results.where and orWhere.where with age and orWhere with role, both with parameters.Pass the test and this pattern moves unknown