nestjs icon indicating copy to clipboard operation
nestjs copied to clipboard

feat: add Transactional Decorator

Open ale-vncs opened this issue 1 year ago • 1 comments

Is your feature request related to a problem? Please describe. Hey, this is amazing ORM to NestJS and I am migrating from TypeOrm, but I am passing by a problem.

In TypeOrm have a lib typeorm-transactional that add a @Transactional, like a Spring Framework, to create transaction in database with decorator. And this do more easy work with transaction, e.g:

@Transactional()
public async saveUser(data: CreateUserDto) {
  const newUserEntity = new UserEntity(data)
  await this.repository.save(newUserEntity)

  await this.addRoles(newUserEntity)

  return new UserResponseDto(newUserEntity)
}

private async addRoles(user: UserEntity) {
  await this.roleService.addDefaultRole(user)
}

If any error occur inside saveUser method or RoleService, a rollback is executed, otherwise a commit is executed when saveUser finalize.

And don't need inject EntityManager and call em.begin() or em.transactional(). In addition, don't need checked if exist a transactional open because @Transactional() manage this. See Propagation and Isolation

Describe the solution you'd like The same solution of Spring @Transactional that typeorm-transactional uses

Describe alternatives you've considered Using AsyncLocalStorage of node to manage a context of mikro-orm and open a transaction like typeorm-transactional

Additional context ...

ale-vncs avatar Apr 28 '24 00:04 ale-vncs

Here is one implementation that came in as a workaround for a different problem with the ORM itself:

https://github.com/mikro-orm/mikro-orm/discussions/5585#discussioncomment-9492403

I'd say this could be a worthy addition to the core package, it's not really tied to the nest integration.

B4nan avatar May 20 '24 08:05 B4nan