0
0
NestJSframework~10 mins

Transactions 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 start a transaction using TypeORM's QueryRunner.

NestJS
const queryRunner = dataSource.createQueryRunner();
await queryRunner.connect();
await queryRunner.[1]();
Drag options to blanks, or click blank then click option'
AstartTransaction
BcommitTransaction
CrollbackTransaction
Drelease
Attempts:
3 left
💡 Hint
Common Mistakes
Using commitTransaction instead of startTransaction
Calling release before starting the transaction
2fill in blank
medium

Complete the code to commit the transaction after successful operations.

NestJS
try {
  // some database operations
  await queryRunner.[1]();
} catch (error) {
  await queryRunner.rollbackTransaction();
}
Drag options to blanks, or click blank then click option'
AstartTransaction
BcommitTransaction
Crelease
Dconnect
Attempts:
3 left
💡 Hint
Common Mistakes
Calling startTransaction again instead of commitTransaction
Forgetting to commit and only rolling back
3fill in blank
hard

Fix the error in the transaction cleanup by completing the code to release the QueryRunner.

NestJS
finally {
  await queryRunner.[1]();
}
Drag options to blanks, or click blank then click option'
Arelease
BrollbackTransaction
CcommitTransaction
DstartTransaction
Attempts:
3 left
💡 Hint
Common Mistakes
Calling commitTransaction or rollbackTransaction in finally block
Not releasing the QueryRunner causing connection leaks
4fill in blank
hard

Fill both blanks to create a transaction that rolls back on error and releases the QueryRunner.

NestJS
try {
  await queryRunner.startTransaction();
  // operations
  await queryRunner.commitTransaction();
} catch (error) {
  await queryRunner.[1]();
} finally {
  await queryRunner.[2]();
}
Drag options to blanks, or click blank then click option'
ArollbackTransaction
Brelease
CstartTransaction
DcommitTransaction
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping rollback and release methods
Forgetting to release the QueryRunner
5fill in blank
hard

Fill all three blanks to create an object that maps entity names to their repository instances inside a transaction.

NestJS
const repositories = {
  [1]: queryRunner.manager.getRepository([2]),
  [3]: queryRunner.manager.getRepository(ProductEntity),
};
Drag options to blanks, or click blank then click option'
A"userEntity"
BUserEntity
C"productEntity"
DProductEntity
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted keys or quoted class names
Mixing keys and values incorrectly