typeorm-transactional-cls-hooked
typeorm-transactional-cls-hooked copied to clipboard
feat: allow use of scoped containers
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
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