Aliheym / typeorm-transactional

A Transactional Method Decorator for TypeORM that uses Async Local Storage or cls-hooked to handle and propagate transactions between different repositories and service methods.
MIT License
201 stars 27 forks source link

two connectione #11

Closed thobui14ig closed 1 year ago

thobui14ig commented 1 year ago

how to use 2 service transaction, hellpe me, i am from VietNam await this.resourcesRepository.save(data); await this.userService.saveUser();

Aliheym commented 1 year ago

Hello @thobui14ig , can you give more information about what you want to do if possible?

Do you want to run your code in one transaction?

await this.resourcesRepository.save(data);
await this.userService.saveUser();
thobui14ig commented 1 year ago

yes, i want one transaction, that is two difference service, if this.userService.saveUser() error i want rollback this.resourcesRepository.save(data), help me

thobui14ig commented 1 year ago

Hello @thobui14ig , can you give more information about what you want to do if possible?

Do you want to run your code in one transaction?

await this.resourcesRepository.save(data);
await this.userService.saveUser();

help me

Aliheym commented 1 year ago

Hi, @thobui14ig.

  1. You need to connect this library (https://github.com/Aliheym/typeorm-transactional#initialization) and example for Nest.js (https://github.com/Aliheym/typeorm-transactional#usage)
  2. You need to use Transactional decorator in the method that contains a code that you want to "wrap" in the one transactions. For example it can be a controller method:
@Patch('/users/:id`)
@Transactional()
async updateUser(data) {
    await this.resourcesRepository.save(data);
    await this.userService.saveUser();
}

or it can be any other method:

class UsersService {
@Transactional()
async updateUser(data) {
    await this.resourcesRepository.save(data);
    await this.userService.saveUser();
}

You can ask questions if you have troubles with something.

Aliheym commented 1 year ago

Feel free to reopen if you have issues.