What's the benefit of using the context instead of a direct import for the database connection?
I've wondered why I should use the tRPC context instead of just importing my database singleton from a module. Is there any benefit to it?
Because of TS performance issues, I'm creating multiple functions for bigger procedures. And passing the db connection around may be an unnecessary overhead.
5 Replies
i think people are doing it so they don’t have to import it everytime which is trivial to me.
curious others people thoughts
Personally I import it where I use it
I used to have it in the context so I think it caught on
I don't think singletons belong there unless they're tied to some specific authed proc or similar
Thanks for the insights.
Yeah, I find importing it way simpler, since VS Code just does it for me. But passing the context to each function is really annoying 😅
I wonder why the docs take the database connection as an example, while e.g. a user's profile is potentially different for each request.
If you want to unit test your work, it makes injecting mocks much easier
It’s very much a best practice in other ecosystems
Obviously in JS land we can mock modules, so a bit of personal preference exists
Yeah, sometimes we are a bit spoiled here in JS land. (Or almost going crazy because there's so many ways to do any given thing)