T
tRPC

❓-help

Is there any benefit to putting the db connection in the context versus having it as an global var?

Kkbemaster1/22/2024
Can I do this?
export const challengesRouter = router({
getChallengeById: privateProcedure
.input(z.string().uuid())
.query(async ({ input }) => {
const challenge = await db.query.challengeTable.findFirst({
where: (columns, { eq }) => eq(columns.sectionId, input),
columns: {
id: true,
sectionId: true,
},
});
return challenge;
}),
});
export const challengesRouter = router({
getChallengeById: privateProcedure
.input(z.string().uuid())
.query(async ({ input }) => {
const challenge = await db.query.challengeTable.findFirst({
where: (columns, { eq }) => eq(columns.sectionId, input),
columns: {
id: true,
sectionId: true,
},
});
return challenge;
}),
});
or in this way i have a benefit?
export const challengesRouter = router({
getChallengeById: privateProcedure
.input(z.string().uuid())
.query(async ({ input, ctx }) => {
const challenge = await ctx.db.query.challengeTable.findFirst({
where: (columns, { eq }) => eq(columns.sectionId, input),
columns: {
id: true,
sectionId: true,
},
});
return challenge;
}),
});
export const challengesRouter = router({
getChallengeById: privateProcedure
.input(z.string().uuid())
.query(async ({ input, ctx }) => {
const challenge = await ctx.db.query.challengeTable.findFirst({
where: (columns, { eq }) => eq(columns.sectionId, input),
columns: {
id: true,
sectionId: true,
},
});
return challenge;
}),
});
BBillyBob1/23/2024
I've wondered the same thing. If you application will grow much it might be fine to not include it in context and extract business logic out to a service. for example challenges.service.ts where all the DB and business logic is done in there.
export const challengesRouter = router({
getChallengeById: privateProcedure
.input(z.string().uuid())
.query(async ({ input }) =>
await challengesService.getById(input);
),
});
export const challengesRouter = router({
getChallengeById: privateProcedure
.input(z.string().uuid())
.query(async ({ input }) =>
await challengesService.getById(input);
),
});
Solution
BBillyBob1/23/2024
What's the benefit of using the context instead of a direct import ...
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.
Kkbemaster1/23/2024
a minute ago I found this thread but it is not clear for me
BBillyBob1/23/2024
the message i took from the thread was that it doesn't really matter, BUT it could make things easier for testing

Looking for more? Join the community!

Recommended Posts
Controller is already closed crashIm using trpc with nextjs 14 app router and started to see that my app crashes due to such error. DiThis page needs to bail out of prerendering at this point because it used revalidate: 0I've been testing out partial prerendering with next using tRPC but having some issues. Everytime How to infer query types on client?```ts export const tenantRouter = router({ getTenants: procedure.query(() => { return { hello:NextJS Output TypingsQuick question probably stupid, I learned about this library yesterday and completely migrated from 'react-server-dom-webpack/client' errorHey, i am trying to get started with TRPC but getting this error ```shell ../../node_modules/.pnpm/React Query client errors on 401I'm throwing a `TRPCError({ code: "UNAUTHORIZED" })` in a middleware, and handling error states retuExtremely slow queriesI'm having elapsed time of 2 to 5 seconds and I just switched from mongo to postgres. Is it the DB lTRPC works but getting unpredictable Typescript warnings for a barebones trpc monorepo projectHi, TRPC is working as expected but there are many warnings that feels very "off" for simple proceduKirimase's tRPC usage CRUD routeHey, using Nico's Kirimase tRPC package. I am trying to call the queries and mutations, in the db/scClient `tsc` checking server codeI have a trpc server and vite/react client set up in a pnpm monorepo. The VSCode type checking is beUnable to set up a client side subscription to a trpc route that supports websocketsI wrote the code on the client side here to query the subscription: https://github.com/sumanthneerumwhat's this union type doing(Zod)?I have 2 types of user, guest and registered(schemas). I have a union type with both and the guest dtrpc's useQuery with undefined input adds extra {"values":["undefined"]} to queryMy React app has several calls to query endpoints that have no input data but that do need useQuery Can you ensureData without the useUtils hook?The Tanstack Router docs recommend calling `ensureQueryData` in a route’s loader function if using T