Bird
0
0

In NestJS with TypeORM, which code snippet correctly ensures that updates to two entities are executed atomically within a transaction?

hard📝 Application Q8 of 15
NestJS - Database with TypeORM
In NestJS with TypeORM, which code snippet correctly ensures that updates to two entities are executed atomically within a transaction?
Aawait manager.save(entityA); await manager.save(entityB);
Bawait dataSource.transaction(async manager => { await manager.save(entityA); await manager.save(entityB); });
Cawait dataSource.transaction(async () => { await entityA.save(); await entityB.save(); });
Dawait dataSource.transaction(async manager => { manager.save(entityA); manager.save(entityB); });
Step-by-Step Solution
Solution:
  1. Step 1: Use DataSource.transaction()

    Wrap all related operations inside a single transaction callback.
  2. Step 2: Use the transactional EntityManager

    Perform all saves using the provided manager to ensure atomicity.
  3. Step 3: Await all asynchronous operations

    Await each save to ensure proper sequencing and error handling.
  4. Final Answer:

    await dataSource.transaction(async manager => { await manager.save(entityA); await manager.save(entityB); }); -> Option B
  5. Quick Check:

    Ensure all operations use the transactional manager and are awaited [OK]
Quick Trick: Await all saves inside transaction callback using manager [OK]
Common Mistakes:
  • Not awaiting save operations inside transaction
  • Using entity methods instead of transactional manager
  • Calling save outside transaction callback

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes