Open kurtextrem opened 1 year ago
@kurtextrem fastify-request-context is based on AsyncLocalStorage, so you can think about it in terms of ThreadLocal in java - you have a guarantee that certain value is scoped to one request, even if you are accessing it directly from the store, and not through request itself. request decorators don't use anything fancy under-the-hood, they are reliant on you accessing something on the request instance itself. If you have reference to your request instance, you don't need fastify-request-context, as AsyncLocalStorage definitely brings performance overhead.
@kibertoad Thank you! So, with fastify-request-context you could potentially avoid passing the request instance to every function - makes sense. I see the use now.
If you have decorated your request with a function, that function's this
is mapped to the request.
💬 Question here
I have hard time understanding the difference between decorators that reset on every req vs. the fastify-request-context plugin. If my handler looks like:
How would it happen that
req.user
gets overriden? Am I missing something? E.g. the code above should reflect the example in https://github.com/thorough-developer/fastify-http-context. The readme in fastify-http-context and fastify-request-context mention that this prevents overriding things when multiple requests hit the same API endpoint - but is this correct? So if e.g. "/user/*" is waiting for the DB to load data for user X, and then some accesses user Y, it could happen that we get back "Hello, X" for the Y route?