How to Generalize Transaction Scope in NestJS Across TypeORM and MongoDB
A common NestJS pattern wraps entire HTTP request handlers in a single database transaction, which works well when only database operations are involved. Problems arise when a use case must call an external service — such as an AI model or payment gateway — before writing to the database, leaving the transaction open for minutes and holding pooled connections and row locks. The article proposes extracting the transaction executor's contract into a small domain-level interface, decoupling transaction control from any specific ORM. This abstraction is validated with two separate implementations — one for PostgreSQL via TypeORM and one for MongoDB — running identical use cases and tests against both. The exercise also highlights which engine-level differences no abstraction can fully hide, offering a practical boundary for how far such a generalization can reliably reach.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in