Bird
0
0

You want to create a custom method findActiveUsers() in your UserRepository that returns only users with isActive = true. Which approach correctly applies the Repository pattern in NestJS?

hard📝 Application Q15 of 15
NestJS - Database with TypeORM
You want to create a custom method findActiveUsers() in your UserRepository that returns only users with isActive = true. Which approach correctly applies the Repository pattern in NestJS?
AAdd <code>findActiveUsers()</code> method directly in the UserService without modifying repository
BExtend the Repository class and add <code>findActiveUsers()</code> method, then inject this custom repository
CUse a global function outside repository and service to filter active users
DModify the User entity to include a static method <code>findActiveUsers()</code>
Step-by-Step Solution
Solution:
  1. Step 1: Understand extending repository for custom methods

    To add custom queries, extend the Repository class and add methods like findActiveUsers().
  2. Step 2: Confirm proper injection of custom repository

    Inject the custom repository in services to keep data access logic encapsulated.
  3. Final Answer:

    Extend the Repository class and add findActiveUsers() method, then inject this custom repository -> Option B
  4. Quick Check:

    Custom repo methods = extend Repository [OK]
Quick Trick: Add custom queries by extending Repository class [OK]
Common Mistakes:
  • Putting query logic in service instead of repository
  • Using global functions outside repository pattern
  • Adding static methods to entity instead of repository

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes