A Query Builder in NestJS is a tool that helps you create database queries step-by-step using code instead of writing raw SQL. It makes queries easier to build, read, and maintain.
Click to reveal answer
beginner
How do you start a Query Builder for a User entity in NestJS using TypeORM?
You use the repository for the User entity and call createQueryBuilder(). For example: const query = this.userRepository.createQueryBuilder('user');
Click to reveal answer
beginner
What method do you use to add a WHERE condition in a NestJS Query Builder?
You use the .where() method. For example: query.where('user.age > :age', { age: 18 }) adds a condition to filter users older than 18.
Click to reveal answer
intermediate
How can you join related tables using Query Builder in NestJS?
You use .leftJoinAndSelect() or .innerJoinAndSelect() methods to join related tables and select their data. For example: query.leftJoinAndSelect('user.profile', 'profile') joins the profile table.
Click to reveal answer
beginner
Why is using a Query Builder better than raw SQL in NestJS?
Query Builder helps avoid SQL syntax errors, improves security by preventing SQL injection, and makes queries easier to read and maintain with code instead of raw strings.
Click to reveal answer
Which method starts a Query Builder for an entity in NestJS?
AcreateQueryBuilder()
Bfind()
Cexecute()
Dconnect()
✗ Incorrect
createQueryBuilder() is the method used to start building a query for an entity.
How do you add a WHERE clause in a NestJS Query Builder?
A.orderBy()
B.join()
C.select()
D.where()
✗ Incorrect
The .where() method adds conditions to filter results.
Which method joins related tables and selects their data?
A.leftJoinAndSelect()
B.groupBy()
C.limit()
D.insert()
✗ Incorrect
.leftJoinAndSelect() joins tables and includes their data in the result.
What is a key benefit of using Query Builder over raw SQL?
ARequires no database connection
BPrevents SQL injection
CRuns faster than SQL
DAutomatically creates tables
✗ Incorrect
Query Builder helps prevent SQL injection by safely handling parameters.
In NestJS, which object do you usually call createQueryBuilder() on?
AService
BController
CRepository
DModule
✗ Incorrect
You call createQueryBuilder() on the repository of the entity.
Explain how to build a query with a WHERE condition and a JOIN using NestJS Query Builder.
Think about chaining methods to build the query step-by-step.
You got /4 concepts.
Describe the advantages of using Query Builder in NestJS compared to writing raw SQL queries.
Focus on safety, readability, and flexibility.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using a QueryBuilder in NestJS with TypeORM?
easy
A. To build database queries safely and readably without raw SQL
B. To write raw SQL queries directly in the code
C. To create frontend UI components
D. To manage user authentication
Solution
Step 1: Understand QueryBuilder role
QueryBuilder helps create database queries in a safe and readable way, avoiding raw SQL.
Step 2: Identify incorrect options
Options B, C, and D do not describe QueryBuilder's purpose correctly.
Final Answer:
To build database queries safely and readably without raw SQL -> Option A
D. this.userRepository.createQueryBuilder('user')
.where('user.age > :age OR user.role = :role', { age: 30 })
.getMany();
Solution
Step 1: Understand the OR condition
We want users older than 30 OR with role 'admin', so we use where and orWhere.
Step 2: Check parameter usage
this.userRepository.createQueryBuilder('user')
.where('user.age > :age', { age: 30 })
.orWhere('user.role = :role', { role: 'admin' })
.getMany(); correctly uses where with age and orWhere with role, both with parameters.
Step 3: Identify errors in other options
this.userRepository.createQueryBuilder('user')
.where('user.age > :age AND user.role = :role', { age: 30, role: 'admin' })
.getMany(); uses AND instead of OR. this.userRepository.createQueryBuilder('user')
.where('user.age > 30 OR user.role = admin')
.getMany(); lacks parameter binding. this.userRepository.createQueryBuilder('user')
.where('user.age > :age OR user.role = :role', { age: 30 })
.getMany(); misses role parameter.