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

Add AsyncLocalStorage driver #39

Closed Aareksio closed 11 months ago

Aareksio commented 1 year ago

So I decided to spent the day implementing AsyncLocalStorage driver... Resolves https://github.com/Aliheym/typeorm-transactional/issues/22

The PR is best read commit after commit, as I tried to make incremental changes:

Need to figure out a way to test both drivers on the same suite. It seems infeasible to copy-and-paste cases, or worse prepare some specific cases for drivers. Best to run everything twice to ensure 100% combability. The lib sadly stores some information in memory (eg. dataSources map) and there is currently no way to clean it up between runs, so .each fails. Therefore I temporarily added TEST_STORAGE_DRIVER environment variable that can be defined before tests run:

TEST_STORAGE_DRIVER=ASYNC_LOCAL_STORAGE npm run test

As I use newer version of npm, the package-lock.json got updated. There is one new dependency, semver. It could probably be removed if we decided on custom implementation, but I worried about the compatibility with older runtimes which may use unexpected version format.

Considerations:

Aliheym commented 1 year ago

Hi, it looks cool. Let me check it on these weekends.

ghost commented 1 year ago

Happy to test and start using it, once merged!

vsamofal commented 11 months ago

@Aliheym @Aareksio love this feature,

is this lib abandoned, and it's better to host it on our own to get this feature?

Aliheym commented 11 months ago

Hi guys, sorry for such delay. It's been a busy period at work. I'll get it done today or tomorrow morning.

Aliheym commented 11 months ago

All tests have successfully passed. However, as rightly mentioned above, the current code does not allow for convenient testing of this feature without running it with different TEST_STORAGE_DRIVER env.

We need to modify the internal data structure, but this would be beyond the scope of the current PR. I believe we can roll out this feature and then I can focus on the necessary internal modifications.

Additionally, I also think we need to use AUTO as the default value

Aareksio commented 11 months ago

For tests, alternatively, you could run CI with different environments using matrix feature.

As for AUTO, I am not sure what is the best rollout strategy here - to release minor keeping cls hooked the default and change to AUTO with next major. Or release major right away.

vsamofal commented 11 months ago

Let me know if I can help with it

Aliheym commented 11 months ago

For tests, alternatively, you could run CI with different environments using matrix feature.

I will check, thanks.

As for AUTO, I am not sure what is the best rollout strategy here - to release minor keeping cls hooked the default and change to AUTO with next major. Or release major right away.

Hard question 🙃. Actually, I'm not thinking about a major version yet. In my opinion it would be better to release the minor version first with cls hooked the default as it is now. Next major, as you said, we can change to AUTO.

Aliheym commented 11 months ago

Thank you for your patience guys. I've merged this PR.