Assuming we have 2 services, A and B. Service A has a function doing the following:
typeorm-transactional-cls-hooked uses CLS (Continuation Local Storage) to handle and propagate transactions between different repositories and service methods.
@Injectable()
export class PostService {
constructor(
private readonly authorRepository: AuthorRepository,
private readonly postRepository: PostRepository,
) {}
@Transactional() // will open a transaction if one doesn't already exist
async createPost(authorUsername: string, message: string): Promise {
const author = await this.authorRepository.create({ username: authorUsername });
return this.postRepository.save({ message, author_id: author.id });
}
}