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.
trash310d ago
i think people are doing it so they don’t have to import it everytime which is trivial to me. curious others people thoughts
Alex / KATT 🐱310d ago
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
michaelschufi309d ago
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.
Nick309d ago
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
michaelschufi309d ago
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)
More Posts
Using both tRPC React client AND tRPC React Query clientHey all! We are successfully using both the React client and the React Query client. We do this becaTRPCClientErrorlike no name propertyQuick question: Why doesn't TRPCClientErrorLike have the name property? It is called ErrorLike, so IapplyWSSHandler yells at context error in expressI used the example code from docs. My applyWSSHandler code is: ``` const handler = applyWSSHandler({How to use the errorFormatter when using an adapter?The example here uses `context.create()`, which isn't something you do when using an adapter. I donOne file per function call on the server sideI'd like to structure my server with one file per function call -- is this an antipattern? If not, dOptimistic update on infinite queryNode 18.12.1 with pnpm trpc v 10.28.1 I have the following query to render a list of posts ```tsx Creating trpc context (express server) for testing without req and res objects.I have seen a few examples of tests with next auth or t3 stack that is using next-auth under the hooDoes trpc (t3 stack) disable ssr from Nextjs?Or is the ssr just not applied to the apiWhat is error formatingI understand error handeling but what is error formating , found it in trcp docs have'nt seen that twhere does opts.path and opts.type came from ? i guess by default it only stores ctx ,next and input```ts const loggerMiddleware = middleware(async (opts) => { const start = Date.now();   const rePassing objects to the next() function of the middle ware```ts // ------------------------------------------------- // @filename: context.ts // -------------opts object passed to mutation , i can not understand where it came from```ts export const authorizedProcedure = publicProcedure .input(z.object({ townName: z.string() })How do I log thrown errors from the query/ mutation on the server side globally?I was surprised to find that the server servicing tRPC requests do not log any thrown errors from a trpc with app dir nextjsHello im wondering how to use trpc with nextjs app router now?. I have t3 app but for now its a src/if I export 'appRouter' instead of 'router' I get errorhey everyone, I have this exact setup https://icflorescu.github.io/trpc-sveltekit/getting-started I React Native/Expo: Uncaught Error: No QueryClient set, use QueryClientProvider to set oneFound this issue but it just says to use patch-package, I was wondering if there was any changes sinAll values in DecoratedProcedureRecord are of type anyI've stumbled across an issue and I'm not sure how to start diagnosing it... Basically in my IDE tDetecting batch queries/mutations on the server-sideHello all, Looking to leverage batching as a means to streamline some of our cross-service requests,VS Code becomes unresponsiveHello everyone, I've started to experience huge performance issues with my project that's based onMiddleware-ish for clientHi! I had a quick question about client-side usage: I have users with long-running sessions, and i