Let's say I have two modules: PostsModule and CommentsModule and I want to create a post along with its comments in a single route (I know this is a weird scenario but I'm taking that example just for the sake of clarification). Let's also assume I have createPost method in my post service and inside of that service I create a post and call createComment method of comment service. Both of these services are called inside the same route.
Since we are performing two different write operations in a single route, I have to wrap everything within a transaction. I know that one solution is to create query runner in that route and use the entity manager from it to insert into multiple tables like:
const queryRunner = dataSource.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
await queryRunner.manager.insert(Post, {/**/})
await queryRunner.manager.insert(Comment, {/**/})
However with this solution I'm not using comments service. I'm doing everything inside the post service which would break the single responsibility principle. I want to call comments service methods to create a comments but still wrap all those operations in a transaction. Does anyone know how to use transactions across different modules? Thanks in advance