Skip to main content

Effective Transaction Management with typeorm-transactional


Overview

The @Transactional decorator, derived from the typeorm-transactional library, simplifies handling transactions in TypeORM. It utilizes ALS (Async Local Storage) or cls-hooked to manage and propagate transactions across different repositories and service methods.

Key Features

  • Data Sources: Provides the ability to handle multiple data sources, enhancing flexibility in database management by providing the connectionName property.
  • Transaction Propagation: Supports options like MANDATORY, NESTED, NEVER, NOT_SUPPORTED, REQUIRED, REQUIRES_NEW, and SUPPORTS, defining how transactions are handled under different scenarios.
  • Isolation Levels: Supports multiple isolation levels like READ_COMMITTED and SERIALIZABLE for transaction control.
  • Hooks: Offers hooks for transaction commit, rollback, and completion to execute custom callbacks.
  • Unit Test Mocking: Facilitates mocking of transactions in unit tests to bypass actual transactional code.

Example with Audit Functionality

  • needs to be done

An example of using the @Transactional decorator could involve a user sign-up process, where the signup action is audited. The decorator ensures that all operations within the scope of the function are executed within a transaction. This setup enhances data integrity, especially in complex operations involving multiple steps or interactions with different data entities.