I recently switched from Prisma to TypeORM. From what I understand, the best practice with TypeORM in NestJS is to keep repositories scoped to their own feature modules. For example, the ProductsRepository should only be used within the ProductsService and registered in the ProductsModule.
The challenge I’m facing is when I need to perform operations that span multiple entities. For example, in a CartsService, I might need to modify products along with other database operations. With Prisma, this was straightforward: I use prisma.$transaction to work directly with the entities in a single transactional context (tho this has his cons I know ).
With TypeORM, however, this becomes tricky. I don’t want to inject the ProductsRepository directly into the CartsService, since that breaks the separation of concerns and goes against Nest/TypeORM best practices. I thought about passing a QueryRunner into the ProductsService as an optional argument, but that would require code changes and feels inelegant, breaking the centralized, clean NestJS design.
So, what’s the best way to handle cross-entity transactional operations in TypeORM while still keeping services and repositories properly encapsulated?