TRPC Context

Hi guys! I was wondering when the trpc context is created? is it created at every request? I was wondering because I'm adding a query to my database inside my trpc context just so I get additional user info that is needed in my routers or do we have a better way to do this instead? Thank you!
5 Replies
cje
cje2y ago
It gets created on every request If you don’t need this information in all routes then maybe get it in a middleware instead
josh-dev627
josh-dev6272y ago
Hi @cje Thanks for the response. Can I get more information about putting it in the middleware instead? do you mean adding a route check and only add those information if the condition passes? Thank you!
cje
cje2y ago
Middlewares | tRPC
You are able to add middleware(s) to a procedure with the t.procedure.use() method. The middleware(s) will wrap the invocation of the procedure and must pass through its return value.
cje
cje2y ago
Trpc middleware can modify context
josh-dev627
josh-dev6272y ago
ahh middlewares in trpc, I though in nextjs. Thanks for the help man!
Want results from more Discord servers?
Add your server