Bird
0
0

In a NestJS service using TypeORM, how do you correctly initiate a transaction with the DataSource instance?

easy📝 Syntax Q3 of 15
NestJS - Database with TypeORM
In a NestJS service using TypeORM, how do you correctly initiate a transaction with the DataSource instance?
Aawait dataSource.transaction(async (manager) => { /* transactional code */ });
Bawait dataSource.startTransaction();
Cawait dataSource.begin();
Dawait dataSource.createTransaction();
Step-by-Step Solution
Solution:
  1. Step 1: Use DataSource.transaction()

    The TypeORM DataSource provides a transaction method that accepts a callback with a transactional EntityManager.
  2. Step 2: Pass an async callback

    The callback receives a manager to perform operations atomically.
  3. Final Answer:

    await dataSource.transaction(async (manager) => { /* transactional code */ }); -> Option A
  4. Quick Check:

    Check method name and usage [OK]
Quick Trick: Use dataSource.transaction() with async callback [OK]
Common Mistakes:
  • Using non-existent methods like startTransaction()
  • Calling transaction without async callback
  • Trying to manually manage transactions without DataSource

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes