Comments (9)
I'll try and submit a PR in the next few days. Thanks for your inputs @mscottnelson! I'll will follow your suggestion of @Transactional
always overriding the class decorator. We can use @Transactional({propagation: NOT_SUPORTED})
as a substitute for @NotTransactional()
.
from typeorm-transactional-cls-hooked.
Hi @herbertpimentel, PR would be happily accepted...
from typeorm-transactional-cls-hooked.
I extended the Transactional
decorator for entire classes using this custom decorator:
export interface TransactionalOptions {
propagation: Propagation
isolationLevel: IsolationLevel
}
const TransactionalService = (options: TransactionalOptions): ClassDecorator => {
return (target: Function) => {
for (const key of Object.getOwnPropertyNames(target.prototype)) {
const methodDescriptor = Object.getOwnPropertyDescriptor(target.prototype, key)
// assumes that only async methods need a transaction
const isAsyncMethod =
methodDescriptor &&
methodDescriptor.value instanceof Object.getPrototypeOf(async function() {}).constructor
if (!methodDescriptor || !isAsyncMethod) {
continue
}
Transactional(options)(target, key, methodDescriptor)
Object.defineProperty(target.prototype, key, methodDescriptor)
}
}
}
It only adds transactions to async
methods, since that is what made sense in my application.
@odavid Should this be a feature of the lib itself? How could I go about implementing it?
from typeorm-transactional-cls-hooked.
Hey @cassinaooo, Can you please open a PR for that decorator?
For this code to be added, I believe we need:
- Tests
- README section
- Since this decorator is an implicit one, and treats all the methods the same, I think we should also introduce an
IgnoreTransactionalServiceMethod
(Name is too long 😄 ) decorator that we can mark a method as not being decorated implicitly
Let me know what you think...
Cheers!
from typeorm-transactional-cls-hooked.
@odavid Is there a way for the @Transactional
decorator to be aware of this new class decorator, or vice-versa? I'm not familiar with meta programming on typescript, maybe Reflect Metadata?
Then we just need to decide on the override semantics instead of implementing a new decorator.
I don't really have anything against @IgnoreTransactionalServiceMethod
or similar, just generally inclined to keep APIs as minimal as possible.
from typeorm-transactional-cls-hooked.
@cassinaooo - I believe reflect metadata should help listing methods that are decorated with Ignore
I believe you can start without the ignore feature.
It was just a suggestion, since setting Transactional
on all async methods seems to me a bit "brutal", but a user can always remove the class decorator and put the Transactional
explicitly.
Hope it helps...
from typeorm-transactional-cls-hooked.
@odavid I see and agree with your point.
What should be expected of @Transactional
when we have @TransactionalService
active, specially in nested calls? This will eventually happen in large enough applications when services have methods that are called in multiple contexts.
I'm trying to understand how Transactional
should behave in regards to propagation in the presence of TransactionalService
, and if that helps avoid using @Ignore
explicitly.
Suppose we have a class annotated with @TransactionalService({propagation: REQUIRED})
and decide to use @Transactional({propagation: NOT_SUPORTED})
in a given method. Would the ignore/override semantics be implicit, but clear? What are combinations of TransactionalService
/ Transactional
that could be problematic?
from typeorm-transactional-cls-hooked.
Just weighing in here since I would like to see this. It seems to me that @transactional would always override @TransactionalService for defined options, and use the options declared in @TransactionalService as defaults. At least, that would be my personal expectation.
As an additional alternative to a decorator like @NotTransactional(), perhaps @TransactionalService could also take an array of method names to exclude, eg:
@TransactionalService({ excludeMethods: ["myMethod"] })
from typeorm-transactional-cls-hooked.
Agree with @mscottnelson
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.