Back to all reviewers

Use transactions for consistency

elie222/inbox-zero
Based on 9 comments
TypeScript

Always wrap multiple related database operations in a transaction to ensure data consistency and prevent partial updates. This applies to operations across different tables or when coordinating database updates with external services (e.g., Redis cache).

Database TypeScript

Reviewer Prompt

Always wrap multiple related database operations in a transaction to ensure data consistency and prevent partial updates. This applies to operations across different tables or when coordinating database updates with external services (e.g., Redis cache).

Example:

- const dbPromise = prisma.label.upsert({ ... })
- const redisPromise = saveUserLabel({ ... })
- await Promise.all([dbPromise, redisPromise])

+ await prisma.$transaction(async (tx) => {
+   const dbResult = await tx.label.upsert({ ... });
+   await saveUserLabel({ ... });
+ });

Key benefits:

  • Ensures all operations succeed or fail together
  • Prevents inconsistent state between related data
  • Maintains referential integrity
  • Simplifies error handling and rollbacks

Apply this pattern when:

  • Updating multiple related records
  • Coordinating DB changes with cache updates
  • Performing data migrations or merges
  • Managing cross-service data consistency
9
Comments Analyzed
TypeScript
Primary Language
Database
Category

Source Discussions