-
Notifications
You must be signed in to change notification settings - Fork 28
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
Method to expose transactional entity manager #8
Comments
Hi @foliveirafilho. Thanks for this issue, I investigated your use case. The problem is how // file - typeorm-ex.module.ts <---- this file from gist
// ...
providers.push({
inject: [getDataSourceToken()],
provide: repository,
useFactory: (dataSource: DataSource): typeof repository => {
const baseRepository = dataSource.getRepository<any>(entity);
// <------ Check line below
return new repository(baseRepository.target, baseRepository.manager, baseRepository.queryRunner);
//
},
});
// ... Here we take the We set custom repository's entity manager as I think we can resolve your problem easier. We just need to change this way how // file - typeorm-ex.module.ts <---- this file from gist
// ...
providers.push({
inject: [getDataSourceToken()],
provide: repository,
useFactory: (dataSource: DataSource): typeof repository => {
const baseRepository = dataSource.getRepository<any>(entity);
const customRepository = new repository(
baseRepository.target,
baseRepository.manager,
);
// we look at baseRepository's `manager` property
// to always get actual one
Object.defineProperty(customRepository, 'manager', {
get() {
return baseRepository.manager;
},
});
return customRepository;
},
});
// ... If you change only this part of code, you don't need other "hacks" with entity manager. I hope, it will help you. |
Hey @Aliheym, it worked! Thank you for this workaround! The problem I see is: if TypeORM creates a new entity manager for each transaction, don't you think using the same entity manager instance in every repository transaction could be a problem? In the way I see it, it could decrease concurrency in the system. What do you think? |
Don't worry. I don't see any problem with it. The solution from the gist and my solution are the same when you don't use this library. We only created a getter for repository's property the "manager". That's it. No difference at all. Your prev solution almost always uses the same entity manager instance, except for transactions, when you need to call But when you use this library you can't use the solution from gist, because this library "patch" a
In the TypeORM transactions look like:
Because of this style of transactions, it's hard to work with transactions in the TypeORM, especially if you want to split your logic and don't want to create new repositories for each transaction. This library or our solution doesn't change this behavior. We just use some Node.js possibilities to make transactions more convenient to use, so you can use the same repositories and other entities of TypeORM in the different transactions and different part of your code. Under the hood, this library makes some magic to support it. |
Great explanation! I'll provide your workaround in the gist so your lib get more popular in the community. Again, thank you for your help. This is such a great lib! |
Hi @Aliheym!
I have a project that was recently upgraded to TypeORM v0.3.7 from v.0.2.x and I used a pretty cool workaround to maintain the custom repos with the same structure as before, as shown here.
Along with that, we also have to use transactions and we encoutered your lib that seems to ease the transactional job.
The thing is, for the transactions to work properly with this solution, I must call
entityManager.withRepository(myRepo)
as show here.So I was able to retrieve the entity manager used in the current transaction with the following code:
But I think this could be provided by the lib. What do you think? Does it make sense with the current scope of this lib?
The text was updated successfully, but these errors were encountered: