Prisma client extensions in T3 app
T3 app uses this variable called
globalThis
and globalForPrisma
. I don't understand the usage of it, but it nullifies the extended types. Can I just remove these and use new PrismaClient()
? If not, what else can I do?
- The file in question: src/server/db.ts
- The page for client extensions: https://www.prisma.io/docs/concepts/components/prisma-client/client-extensions/resultPrisma
Prisma Client extensions: result component (Preview)
Extend the functionality of Prisma Client, result component
4 Replies
https://www.prisma.io/docs/guides/other/troubleshooting-orm/help-articles/nextjs-prisma-client-dev-practices#solution
This is the reason as to why.
Though not sure if you can maybe make an object with your options. Then tack on the
export const prisma
line perhaps? Like
Maybe ?Prisma
Best practice for instantiating PrismaClient with Next.js
Best practice for instantiating PrismaClient with Next.js
Thanks a lot. Your solution is great but the problem remains because of:
No matter what it's force casting the generated types
I am currently forcing my own client types I wrote manually everywhere I use them but that's just not scalable and safe
We solved this like so:
And then use this
PrismaClient
type definition in the globals instead of the prisma-exported one
This works!
At least it seems to be at the moment
I need to test it and inform you
This is genius
Thank you
Why are we creating a function and returning instead of just creating an object?
What's the difference?
This solution also sets the type of
args
of methods like findFirst
and findMany
to {}
for some reason
for the arguments of the methods you don't get autocompletion
Is this a problem for you as well?
I found the optimal solution to this: