Prisma + Zod.. which types to use as SOT? Is there a better way?
I’m building t3 app with prisma. Since I want to use trpc and react hook form, i feel like i also need zod. But now I have a dilemma of needing to pick a source of truth for my types. I’m assuming I should use prisma since its ultimately the db. I’ve looked at some libraries to generate zod schemas from prisma, but it just doesn’t feel like the cleanest way to go about this. How have you all handled this in the past?
3 Replies
I'd make them all their own sources of truth.
Prisma - SOT for backend types returned to the frontend
tRPC + Zod - SOT for backend types received from the frontend
RHF + Zod - SOT for frontend types sent to the backend
And then if any of them don't match up you'll get type errors
You can also intermingle them with stuff like custom prisma generators but I haven't used them
hmm but it just seems so annoying to have to keep them all in sync and write essentially the same things twice
yeah that's where generators come in, idk what they're like