Prisma in TRPC context

hey, i'm using trpc and i have my prismaclient instance in trpc's context. i sometimes like to put business logic into functions seperate from my procedures. is it fine to just use a prisma instance which isn't in my trpc context or is it a bad idea?
5 Replies
cupofcrypto
cupofcrypto•13mo ago
You can organize your logic as much as you want but I think that we still need to use the same instance of Prisma. For example, I am trying right now to have a Next.js endpoint API to run a specific task in Prisma but I am still in debug mode because my approach seems it does not work. Have a look here for some ideas: https://youtu.be/G2ZzmgShHgQ
Christopher Ehrlich
YouTube
Advanced tRPC - Callers, functions, and gSSP
🚨 createSSGHelpers has been renamed to createServerSideHelpers 🚨 Repo for this video: https://github.com/c-ehrlich/you-dont-need-callers If you want to use schema in the frontend, they cannot be imported from the same file as things that run specifically in the backend. One solution would be to put them into a procedureName.schema.ts or simi...
monobenzone
monobenzone•13mo ago
You can organize your logic as much as you want but I think that we still need to use the same instance of Prisma.
of course, i was just wondering if there was any reason to put the client's instance in trpc's context and if not doing so could bite me in the ass
cupofcrypto
cupofcrypto•13mo ago
I am still learning how all the pieces of the stack work together, but from my point of view you add the instance in the tRPC's context if you plan to use internally within your components. Making a procedure will allow you to pull/push data right from the procedure and the React Query hooks. Otherwise, if it is a one-shot action I believe that you can work without tRPC for that specific task. For example, my in my use-case I have created a Next.js API endpoint because I want to pull data from an external API and fill the database with the specific information that I get from it. In this case, I have not called tRPC and I've imported the PrismaClient right away.
// Pseudo-code
const prisma = new PrismaClient()

const response = await fetch(...)
const data = await response.json()

const massageData = data.map( /* Do your magic */ )

prisma.table.createMany({data: massageData})
// Pseudo-code
const prisma = new PrismaClient()

const response = await fetch(...)
const data = await response.json()

const massageData = data.map( /* Do your magic */ )

prisma.table.createMany({data: massageData})
I have a tRPC procedure to standard CRUD for the table but in this case it seemed to me that bring it was just an overkill
monobenzone
monobenzone•13mo ago
I am still learning how all the pieces of the stack work together, but from my point of view you add the instance in the tRPC's context if you plan to use internally within your components. Making a procedure will allow you to pull/push data right from the procedure and the React Query hooks.
i don't mean the trpc client instance that you have in react, i meant the context which you pass to trpc's request handler on the server
cupofcrypto
cupofcrypto•13mo ago
mmm probably I am not able to help you out here, let's wait what more experienced people will answer you 😉