Theo's Typesafe CultTTC
Theo's Typesafe Cult4y ago
15 replies
yantakus

Proxy prisma calls to FE with tRPC

Hi there. First of all, I'd like to thank everyone involved in the T3 project. It is really awesome.

I have a question. Is there a way to just proxy prisma calls to FE with tRPC? So that we don't have to create procedures, duplicate inputs... Just something like:
export const appRouter = router({
  prisma,
  custom: customProcedures
});


And then on the FE:
const post = trpc.prisma.post.findUnique(variables)

if all you need is something simple.

And
const smthSpecific = trpc.custom.post.doSmthSpecific(variables)

when you need to do something with, let's say, permissions check or some other custom logic that prisma is not aware of.

That would be so cool, because, ATM, most of my procedures is boilerplate, where I create them just to make existing prisma endpoints available at FE with exactly the same API, for example:

export const postRouter = router({
  findUnique: publicProcedure
    .input(z.object({ id: z.number() }))
    .query(({ input, ctx }) => {
      return ctx.prisma.post.findUnique({
        where: { id: input.id },
      })
    }),
  findMany: publicProcedure.query(({ ctx }) => {
    return ctx.prisma.post.findMany()
  }),
})
Was this page helpful?