Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use getRepository instead of createEntityManager #156

Open
wants to merge 1 commit into
base: next
Choose a base branch
from

Conversation

tillkolter
Copy link

@tillkolter tillkolter commented Jan 2, 2024

Hi,
first of all, thank you for porting the original repository to support later TypeORM versions.

My PR is a rather short one and I frankly don't know if my intended solution is too niche for the bigger picture on how general this library needs to be. But maybe I hope I can win you over. I have tapped into TypeORM only for about a week, so my judgement call might not well educated.

I am currently improving the test suite for a NodeJS project, trying to make the integration-testing developer experience as close to batteries included ecosystem like Python Django or Ruby on Rails, which was the reason why I came across your predecessor's library to support a factory_girl like pattern.

Besides adding a factory pattern to create test entities, I had also integrated this library to support isolating test cases inside their own transactions which can be rolled back after execution. The problem is, that now the typeorm-factory entities are created in different transaction contexts, because each Factory.create(...) creates a new EntityManager instance.

The typeorm-transactional works by patching the DataSource.manager and EntityManager.prototype.getRepository (in contrast to createEntityManager). After some consideration where to fix my issue, I came to the conclusion that I'd rather fix this issue on the factory side, because the factories operate on a higher level of the stack and there are probably good reasons not to patch createEntityManager or even the underlying EntityManagerFactory to keep special use cases intact, where the manager creation is needed to for custom SQL queries.

@inossidabile
Copy link
Contributor

@tillkolter I think the better approach would be to do this.dataSource.manager which is more compatible to the way it works currently. Using repos is an unnecessary wrapper. We could do a protected method getEntityManager() on the level of Factory and default to current implementation but allowing us to override it and keep general transaction.

Breaking out of current transaction still makes sense for some cases and changing it by default is not backward compatible. Let me quickly draft another PR with alternative implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants