Challenge - 5 Problems
Repository Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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' } }); }
Attempts:
2 left
💡 Hint
Remember that the find method returns all matching records as an array.
✗ Incorrect
The find method with a where clause returns all records matching the condition. Since 2 users have role 'admin', the output is an array of those 2 users.
📝 Syntax
intermediate2: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?
Attempts:
2 left
💡 Hint
Check the correct TypeORM repository method for finding one record by condition.
✗ Incorrect
The method findOneBy is the correct TypeORM repository method to find a single record by a condition object.
🔧 Debug
advanced2: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 } }); }
Attempts:
2 left
💡 Hint
Check the TypeORM repository method signatures for findOne.
✗ Incorrect
In TypeORM v0.3+, findOne requires an object with conditions like { where: { id } }, passing id directly causes a runtime error.
❓ state_output
advanced2: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' }
Attempts:
2 left
💡 Hint
The save method updates if primary key exists, otherwise inserts.
✗ Incorrect
TypeORM's save method updates the record if the primary key exists, so the user with id 10 is updated with the new name.
🧠 Conceptual
expert2: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.
Attempts:
2 left
💡 Hint
Think about separation of concerns between data access and business logic.
✗ Incorrect
The repository pattern abstracts how data is fetched or saved, so the rest of the app does not depend on database specifics.