0
0
NestJSframework~20 mins

Repository pattern in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Repository Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this NestJS repository method call?
Consider a NestJS repository method that fetches all users with the role 'admin'. What will be the output if the database contains 3 users with roles: 'admin', 'user', 'admin'?
NestJS
async findAdmins() {
  return this.userRepository.find({ where: { role: 'admin' } });
}
AAn array with 2 user objects having role 'admin'
BAn array with all 3 user objects regardless of role
CAn empty array
DA single user object with role 'admin'
Attempts:
2 left
💡 Hint
Remember that the find method returns all matching records as an array.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a custom repository method in NestJS?
You want to add a method 'findByEmail' to your UserRepository to find a user by email. Which code snippet correctly defines this method?
A
async findByEmail(email: string) {
  return this.findOneBy({ email });
}
B
async findByEmail(email: string) {
  return this.userRepository.findOneByEmail(email);
}
C
async findByEmail(email: string) {
  return this.userRepository.findOne({ email });
}
D
async findByEmail(email: string) {
  return this.findOne({ where: { email } });
}
Attempts:
2 left
💡 Hint
Check the correct TypeORM repository method for finding one record by condition.
🔧 Debug
advanced
2:00remaining
Why does this NestJS repository method throw a runtime error?
Given the method below, why does calling 'findUserById(5)' throw an error?
NestJS
async findUserById(id: number) {
  return this.userRepository.findOne({ where: { id } });
}
AThe method findOne is deprecated and cannot be used
BThe id parameter must be a string, not a number
CfindOne expects an object with conditions, not a direct id argument
DThe repository is not injected properly
Attempts:
2 left
💡 Hint
Check the TypeORM repository method signatures for findOne.
state_output
advanced
2:00remaining
What is the state of the database after this repository save operation?
If you call this method to save a user object with id 10 and name 'Alice', what happens if a user with id 10 already exists?
NestJS
async saveUser(user: User) {
  return this.userRepository.save(user);
}

// Called with { id: 10, name: 'Alice' }
AThe existing user remains unchanged
BA new user with id 10 is created, causing a duplicate key error
CThe save method throws a runtime error
DThe existing user with id 10 is updated with name 'Alice'
Attempts:
2 left
💡 Hint
The save method updates if primary key exists, otherwise inserts.
🧠 Conceptual
expert
2:00remaining
Which statement best describes the role of the repository pattern in NestJS?
Choose the most accurate description of why the repository pattern is used in NestJS applications.
AIt automatically generates REST API endpoints for database entities
BIt abstracts data access logic, allowing business logic to remain independent of database details
CIt replaces the need for service classes in NestJS
DIt enforces strict typing on all database queries
Attempts:
2 left
💡 Hint
Think about separation of concerns between data access and business logic.