odavid / typeorm-transactional-cls-hooked

A Transactional Method Decorator for typeorm that uses cls-hooked to handle and propagate transactions between different repositories and service methods. Inpired by Spring Trasnactional Annotation and Sequelize CLS
MIT License
524 stars 86 forks source link

feat: allow use of scoped containers #21

Open ashleyw opened 5 years ago

ashleyw commented 5 years ago

Hi,

By default, TypeORM only supports a global container[0], which is referenced whenever you call getManager().

This PR contains my potential solution but requires your services have a public container property exposed so the @Transactional decorator can access it, and an active ConnectionManager instance in the container.

Is there a better way of doing this? Is this feature something you'd be interested in merging? Please let me know your thoughts -- I'm sure there must be a cleaner way of achieving this!

Thanks!

[0] https://github.com/typeorm/typeorm/blob/master/src/container.ts

odavid commented 5 years ago

Hi @ashleyw - Thank you!

I am sorry I could not respond earlier, and I am sorry in advance if it will take me some time to do so in the future.

The part that we need a container property within the owner service feels a bit "hacky" to me.

I haven't give it a lot of thinking, but maybe instead of using a "convention" and adding the container property to the owner service, there could be a Factory injected to the cls-hooked context during the initialization passing a function to the initializeTransactionalContext function.

Not sure if the above can solve the problem, but it would feel much better if the "contract" will not rely on member name.

Can you explain a bit the use case of scoped containers for creating connections?

Cheers