You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
How to access current http context inside component method?
As an example i would like to access current session user inside "typeorm" EventSubscriber class to automatically set user who inserted/update/deleted record.
Hi @darioxtx,
Can you say something more about what you wanna achieve? In most cases, you only have to pass the request object to the component method.
Each request have user object inside and i want to access that data in component and use current user object to filter data from database based on logged in user ex.:
Hi @darioxtx,
To achieve some kind of 'request context' you can try to use this library https://www.npmjs.com/package/request-context
But referring to your issue, I'm afraid that it might be not such easy. The instances of the typeorm event subscribers are created in the core of this ORM. As far as I know, to set up the subscribers, you have to include classes (or directories where they are) inside the options object. It means that typeorm and nest would use different instances of this class. If there's a possibility to pass instances instead of types to typeorm, it'd much easier to make it work together.
Hi @darioxtx,
you can also try using Zone.js as middleware and see how it goes.
@kamilmysliwiec, maybe Zone.js can be integrated in the framework? The zone will provide this request context if each request handler is run in a new zone. Then that can be injected in the components as RequestContext.
@cdiaz yes my solution is to use Zone.js. it seems promising and is under angular team control.
But i faced issue using async/await with target ES2017. with target ES2016 works good.
Some weeks ago i tried zone.js in node to get the current user (and some other information). But zone.js often caused crashes when the app becomes complex.
Therefore i upgraded to node 8.4 and used cls-hooked.
The latest version of cls-hooked uses async_hooks API when run in Node >= 8.2.1
The implementation works in a very similar way.
There is also a type definition file for cls-hooked
@JustGreg, this is excellent, thanks for sharing, I wasn't aware that this API was added in Node.js.
I've also used Zone.js experimentally in Node.js project and even though I didn't noticed any issues back then I still felt uncomfortable. Zone.js in order to preserve the context does monkey patching of all async APIs that are in the browser and in Node. That means something could very easily break in the next upgrade. I would definitely go with this API instead of Zone.js even though it's in experimental phase.
@kamilmysliwiec, this is excellent opportunity for adding new features that depend on the 'thread-local' storage like declarative transaction management. While it's in experimental phase Nest can leave the decision to the user whether he will enable this feature or not. What are your thoughts?
Hi everyone,
A lot of great stuff, that's awesome 🎉
@saskodh since this API is still in experimental phase (stability: 1) I'm not gonna integrate it with nest in any way now. What I'm trying to do is to make sure that learning curve is not too big, so I see this feature as a 3rd-party module rather than the 'core thing'.
@adrien2p Can you provide a small example please?
I want to use Typeorm's Listener's to update createdBy when inserting a new entity and get the user from the request (req.user)
You just have to use the inject decorator to inject the request, for that you need to make your provider request scoped. from the injected request you only need to do this.request.user to access the current user on the request. But don’t forget to attach the user through a guard. I am out of the office and on the phone, hard to write an example ^^ for the request scoped i invite you to have a look on this link https://docs.nestjs.com/fundamentals/injection-scopes
mambax, iliuyt, vineet-suri and joelcoronahyogevlahyani, maxfriedmann and kamilmysliwiec
@mambax you have to set your logger scope to Scope.Request in @Injectable() decorator. Otherwise, we won't be able to actually inject REQUEST because the instance will be created once (during the application bootstrap)
I know this issue is closed, but I got here by Google Search and also the last comment is pretty recent (16 days ago).
@ants88 I do not have a suggestion, but what I am experiencing right now is: if service in the Subscriber (using the NestJS way, not the TypeORM way) dependency tree (so not only services directly used in the Subscriber's constructor, but also anywhere deeper) is marked with @Injectable({ scope: Scope.REQUEST }), the Subscriber's constructor is not called. There is no error or warning reported.
@kamilmysliwiec if I understand what you mention in your comment it almost looks like a bug to me. Should I open a new issue and add a test case for this?
Since I need this badly too, I will try the other options suggested in this thread and update you with my findings.
Activity
kamilmysliwiec commentedon Oct 1, 2017
Hi @darioxtx,
Can you say something more about what you wanna achieve? In most cases, you only have to pass the
request
object to the component method.darioxtx commentedon Oct 2, 2017
Hi @kamilmysliwiec,
Each request have user object inside and i want to access that data in component and use current user object to filter data from database based on logged in user ex.:
An other place where I want to get user is
EventSubscriber
here i want to know witch user created or updated or deleted entity:Thanks
kamilmysliwiec commentedon Oct 2, 2017
Hi @darioxtx,
To achieve some kind of 'request context' you can try to use this library https://www.npmjs.com/package/request-context
But referring to your issue, I'm afraid that it might be not such easy. The instances of the
typeorm
event subscribers are created in the core of this ORM. As far as I know, to set up the subscribers, you have to include classes (or directories where they are) inside the options object. It means that typeorm and nest would use different instances of this class. If there's a possibility to pass instances instead of types to typeorm, it'd much easier to make it work together.darioxtx commentedon Oct 2, 2017
Thank you for your answer. I will share my experience solving this issue.
saskodh commentedon Oct 2, 2017
Hi @darioxtx,
you can also try using Zone.js as middleware and see how it goes.
@kamilmysliwiec, maybe Zone.js can be integrated in the framework? The zone will provide this request context if each request handler is run in a new zone. Then that can be injected in the components as RequestContext.
BTW, request-context is implemented on top of the Node.js Domain API which is deprecated for quite some time.
darioxtx commentedon Oct 2, 2017
@saskodh thanks for sharing info. I will try Zone.js.
cdiaz commentedon Oct 4, 2017
@darioxtx did you find any solution?
darioxtx commentedon Oct 4, 2017
@cdiaz yes my solution is to use Zone.js. it seems promising and is under angular team control.
But i faced issue using async/await with target ES2017. with target ES2016 works good.
I can share code later if anyone interested.
cdiaz commentedon Oct 4, 2017
Great, I'm interested to see your code to know how to implement CurrentUser context.
thanks
darioxtx commentedon Oct 5, 2017
Hi,
Here is my Zone.js integration with nest.
first I import
zone.js
toserver.ts
like thisThen I created
NestMiddleware
RequestContext
looks like thisRequestContextMiddleware
is used in mainApplicationModude
Current
RequestContext
can be accessed everywhere in your codeI think that's it, also i like exception trace that provide
Zone.js
. It's really easy to find where your code crashed.@saskodh thanks for right directions.
jselesan commentedon Oct 5, 2017
Great work @darioxtx !
JustGreg commentedon Oct 7, 2017
Some weeks ago i tried zone.js in node to get the current user (and some other information). But zone.js often caused crashes when the app becomes complex.
Therefore i upgraded to node 8.4 and used cls-hooked.
The latest version of
cls-hooked
uses async_hooks API when run in Node >= 8.2.1The implementation works in a very similar way.
There is also a type definition file for
cls-hooked
saskodh commentedon Oct 7, 2017
@JustGreg, this is excellent, thanks for sharing, I wasn't aware that this API was added in Node.js.
I've also used Zone.js experimentally in Node.js project and even though I didn't noticed any issues back then I still felt uncomfortable. Zone.js in order to preserve the context does monkey patching of all async APIs that are in the browser and in Node. That means something could very easily break in the next upgrade. I would definitely go with this API instead of Zone.js even though it's in experimental phase.
@kamilmysliwiec, this is excellent opportunity for adding new features that depend on the 'thread-local' storage like declarative transaction management. While it's in experimental phase Nest can leave the decision to the user whether he will enable this feature or not. What are your thoughts?
kamilmysliwiec commentedon Oct 31, 2017
Hi everyone,
A lot of great stuff, that's awesome 🎉
@saskodh since this API is still in experimental phase (stability: 1) I'm not gonna integrate it with nest in any way now. What I'm trying to do is to make sure that learning curve is not too big, so I see this feature as a 3rd-party module rather than the 'core thing'.
21 remaining items
adrien2p commentedon Apr 18, 2019
You should be able to achieve that by injecting the request as @Inject(REQUEST)
yogevlahyani commentedon Apr 18, 2019
@adrien2p Can you provide a small example please?
I want to use Typeorm's Listener's to update
createdBy
when inserting a new entity and get the user from the request (req.user)adrien2p commentedon Apr 18, 2019
You just have to use the inject decorator to inject the request, for that you need to make your provider request scoped. from the injected request you only need to do this.request.user to access the current user on the request. But don’t forget to attach the user through a guard. I am out of the office and on the phone, hard to write an example ^^ for the request scoped i invite you to have a look on this link https://docs.nestjs.com/fundamentals/injection-scopes
mambax commentedon Jul 17, 2019
I am still stuck on this topic, can you help regarding this issue?
I "sold" NestJS to our company to use it but this stops us from boosting it like "This is the way we do!"
https://stackoverflow.com/questions/57070997/request-scoped-logger
Instead of using it for an ORM I need the current request everytime someone calls "logger.log"...
kamilmysliwiec commentedon Jul 17, 2019
@mambax you have to set your logger scope to
Scope.Request
in@Injectable()
decorator. Otherwise, we won't be able to actually injectREQUEST
because the instance will be created once (during the application bootstrap)ants88 commentedon Jul 30, 2019
Hello! If I add
@Injectable({ scope: Scope.REQUEST })
decorator to my EntitySubscriberInterface, the beforeInsert event is not fired.Instead with this structure the event is fired, but I need request
Any suggestion? Thank you
davidpodhola commentedon Aug 15, 2019
I know this issue is closed, but I got here by Google Search and also the last comment is pretty recent (16 days ago).
@ants88 I do not have a suggestion, but what I am experiencing right now is: if service in the
Subscriber
(using the NestJS way, not the TypeORM way) dependency tree (so not only services directly used in theSubscriber
's constructor, but also anywhere deeper) is marked with@Injectable({ scope: Scope.REQUEST })
, theSubscriber
's constructor is not called. There is no error or warning reported.@kamilmysliwiec if I understand what you mention in your comment it almost looks like a bug to me. Should I open a new issue and add a test case for this?
Since I need this badly too, I will try the other options suggested in this thread and update you with my findings.
megazoll commentedon Sep 4, 2019
@kamilmysliwiec what do you think about introducing
RequestHolder
service, which will return actualRequest
object? In this case we can useRequest
inSINGLETON
-scoped services.Symfony framework works in such way: https://symfony.com/blog/new-in-symfony-2-4-the-request-stack
iliuyt commentedon Oct 8, 2019
so........How to access current http context inside "typeorm" EventSubscriber class??????????
davidpodhola commentedon Nov 8, 2019
Just FYI right now I am using https://github.com/jeff-lewis/cls-hooked to pass the context I need (like
@CurrentUser() user
orEntityManager
) like inasync find(user: AppUser) : Promise<Array<T>> { return run( user, getManager(), async () => await getManager().getRepository(this.getCtor()).find() ); }
.Would be interested to know how this can be done better.
devendrainfotech commentedon Dec 8, 2019
Is this feature released in nest 6 release. If yes can we have a example how to do get request context in typeorm event subscribe class
joelcoronah commentedon Feb 10, 2020
Is it already integrated?
kamilmysliwiec commentedon Feb 10, 2020
https://docs.nestjs.com/fundamentals/injection-scopes