0
0
NestJSframework~10 mins

Repository pattern in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the Repository class from TypeORM.

NestJS
import { [1] } from 'typeorm';
Drag options to blanks, or click blank then click option'
AModule
BRepository
CController
DService
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Service or Controller instead of Repository.
Forgetting to import from 'typeorm'.
2fill in blank
medium

Complete the code to inject the repository for the User entity using @InjectRepository.

NestJS
constructor(@InjectRepository([1]) private userRepository: Repository<User>) {}
Drag options to blanks, or click blank then click option'
AUser
BUserService
CUserModule
DUserController
Attempts:
3 left
💡 Hint
Common Mistakes
Passing service or module names instead of the entity class.
Omitting the entity class inside @InjectRepository.
3fill in blank
hard

Fix the error in the method to find a user by id using the repository.

NestJS
async findUserById(id: number): Promise<User | null> {
  return await this.userRepository.[1]({ id });
}
Drag options to blanks, or click blank then click option'
AfindOneById
BfindById
CfindOne
DgetOne
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like findOneById or findById.
Using getOne which is not a repository method.
4fill in blank
hard

Fill both blanks to create a new user entity and save it using the repository.

NestJS
const newUser = this.userRepository.[1](userData);
await this.userRepository.[2](newUser);
Drag options to blanks, or click blank then click option'
Acreate
Bsave
Cinsert
Dupdate
Attempts:
3 left
💡 Hint
Common Mistakes
Using insert instead of save after create.
Using update instead of save for new entities.
5fill in blank
hard

Fill all three blanks to update a user's email by id using the repository.

NestJS
await this.userRepository.[1]({ id: userId }, { [2]: [3] });
Drag options to blanks, or click blank then click option'
Aupdate
Bemail
CnewEmail
Dsave
Attempts:
3 left
💡 Hint
Common Mistakes
Using save instead of update for partial updates.
Mixing up field names or values in the update object.