Comments (9)
@willianscfa i wound up writing a custom express wrapper. Mostly detailed here: typeorm/typeorm#6823
from typeorm-transactional-cls-hooked.
@micalevisk that is mostly correct, I think... The rollback functionality is coupled to the Repository Manager (which, can include all repos if you use the monkey-patch of the base TypeORM classes). Therefore, if you do not use a patched repository, then the @transactional is not applied.
I'm not sure about the specific semantics of into
in this scenario. You might consider whether you can get away with a pattern like: await getRepository(User)...
and chain from there, to reduce this ambiguity in the absence of additional clarification from @odavid
from typeorm-transactional-cls-hooked.
+1
I would also like to happen if its enough to patch the repositories and having one transactional decorator to have any methods using the repository after the decorator to use the same trx.
from typeorm-transactional-cls-hooked.
Hi @alsonkemp!
If I understand you correctly, you will need to follow the steps in the README to set up a custom repository, and then use @Transactional()
in all methods that call database operations.
Note that you need to use the custom repository for database operations, since calls to Entity.save() method will run inside an independent transaction (and might break your application).
The default propagation level for transaction is REQUIRED
, which means that that method's will use the existing transaction if called from a method with an active transaction, or create a new one.
from typeorm-transactional-cls-hooked.
@alsonkemp, I'm checking with @odavid if a class decorator makes sense in this lib. However, #41 contains a custom class decorator implementation that would work.
from typeorm-transactional-cls-hooked.
I'm trying to integrate this because it seems like a really elegant approach to organizing transactions, and am running into some issues of clarity from the README like @alsonkemp. @cassinaooo , you said:
use
@Transactional()
in all methods that call database operations."
If you patch TypeORM Repository with BaseRepository, what scenarios are not covered? Can I do getManager().createQueryBuilder(MyEntity) and expect the @Transactional
to cover it? What about createQueryRunner, so long as I am only touching Entities that use BaseRepository? Are there any caveats with relations?
Is there any way to grab the transactional context that is established by the decorator to use in functions that do not / cannot employ the decorator? Eg, can I safely do getRepository(MyEntity).manager, pass that manager to another function, and expect the transactional context to hold for that manager?
from typeorm-transactional-cls-hooked.
@mscottnelson See typeorm/typeorm#6823 for how I wound up handling this. I can provide more details if you like.
from typeorm-transactional-cls-hooked.
Can I do getManager().createQueryBuilder(MyEntity) and expect the @transactional to cover it?
Managers / QueryBuilders obtained using the patched repository run inside the transaction in my experience.
I usually do something like this.customRepositoryInstance.createQueryBuilder(...)
and it works just fine.
from typeorm-transactional-cls-hooked.
So this following is expected to don't run inside transactions, right?
await getConnection()
.createQueryBuilder()
.insert()
.into(User)
.values([
{ firstName: "Timber", lastName: "Saw" },
{ firstName: "Phantom", lastName: "Lancer" }
])
.execute();
I've been using this lib with https://github.com/w3tecch/typeorm-seeding and when using its connection
value to create the QueryBuilder
this query don't rolls back
from typeorm-transactional-cls-hooked.
Related Issues (20)
- Transactional not rollback excute Promise.all Propagation.REQUIRED HOT 1
- TypeError: typeorm_transactional_cls_hooked_1.runOnTransactionCommit is not a function HOT 1
- Support the latest version of typeorm (v0.3.x) HOT 16
- Transaction(s) stuck after connection limit reached
- How do i get Transaction to work ? Getting ConnectionNotFound error HOT 5
- can't add new command when connection is in closed state
- Transaction not rolling back as expected
- Transactional doesn't work well with Promise.all HOT 1
- use @Transactional() with @Res of nestjs transaction won't close HOT 1
- QueryRunnerAlreadyReleasedError after runOnTransactionRollback HOT 4
- Feature request: make NAMESPACE_NAME exported
- How to test that method uses Transactional decorator? HOT 1
- How to commit the transaction? HOT 4
- EntityManager is Undefined
- Testing Error initializeTransactionalContext() HOT 3
- Transactions not working with multiple DBs (connections) HOT 6
- manager property is undefined when using Propagation 'SUPPORTS' HOT 4
- transaction w/o class method decorator HOT 2
- Transactions does not work with multi tenant application HOT 15
- Transaction doesn't work HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typeorm-transactional-cls-hooked.