reymon359R
tRPC2y ago
1 reply
reymon359

Why do we pass the database client in the context

Hey there 👋

In the examples and in T3app we pass the db client in the trpc context
export async function createContextInner(opts?: CreateInnerContextOptions) {
  return {
    prisma,
    session: opts.session,
  };
}


then, to make requests to the db we use it from the context such as ctx.prisma.whatever.create()

I would like to understand why do we do so.
Why no just import the client and use it such as import { prisma } from "~/server/db"; and then in the router use prisma.whatever.create() without the ctx.

Why is it used from the context?
What are the benefits?
does it really matter?
Was this page helpful?