kbemaster
kbemaster7mo ago

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

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;
}),
});
Solution:
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.
Jump to solution
4 Replies
BillyBob
BillyBob7mo ago
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
BillyBob
BillyBob7mo ago
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.
kbemaster
kbemaster7mo ago
a minute ago I found this thread but it is not clear for me
BillyBob
BillyBob7mo ago
the message i took from the thread was that it doesn't really matter, BUT it could make things easier for testing