How to use Prisma on middleware
Answered
Californian posted this in #help-forum
CalifornianOP
I see Next.js middleware only supports Edge runtime. This causes problems if I use Prisma as a provider in NextAuth. Prisma recommends two solutions:
- Prisma Accelerate: https://pris.ly/d/accelerate
- Driver Adapters: https://pris.ly/d/driver-adapters
At a quick glance, the first solution seems paid to scale and the second solution only supports PostgreSQL and Turso databases (mine is currently MongoDB).
How are you guys solving this problem ? I would like a free and scalable solution if possible.
Thanks
- Prisma Accelerate: https://pris.ly/d/accelerate
- Driver Adapters: https://pris.ly/d/driver-adapters
At a quick glance, the first solution seems paid to scale and the second solution only supports PostgreSQL and Turso databases (mine is currently MongoDB).
How are you guys solving this problem ? I would like a free and scalable solution if possible.
Thanks
Answered by Californian
Folks, I come with good news
I got a Vercel PostgreSQL database working via Prisma on Next middleware (Edge runtime) using the Neon database driver and the Prisma's Neon driver adapter. Docs here (I think these docs came very recently): https://www.prisma.io/docs/orm/prisma-client/deployment/edge/deploy-to-vercel#vercel-postgres
No need for Prisma accelerate for this.
Cheers 🥂
I got a Vercel PostgreSQL database working via Prisma on Next middleware (Edge runtime) using the Neon database driver and the Prisma's Neon driver adapter. Docs here (I think these docs came very recently): https://www.prisma.io/docs/orm/prisma-client/deployment/edge/deploy-to-vercel#vercel-postgres
No need for Prisma accelerate for this.
Cheers 🥂
20 Replies
Common House-Martin
what i did was make an api endpoint and i query that endpoint from the middleware, make sure you're clearing cache on it correctly. I also find this pretty dumb that it works like that but it's the only way really
CalifornianOP
In my case the calls to prisma are being made internally by NextAuth, so there is no way to wrap them in route handlers.
I think I will have to migrate to PostgreSQL to be able to use Prisma's database driver adapter (which supports Edge) ... It's so annoying !
I think I will have to migrate to PostgreSQL to be able to use Prisma's database driver adapter (which supports Edge) ... It's so annoying !
Eulophid wasp
I'm finding limitations with Prisma and edge too.
Have you managed to use Prisma client in Edge by any change? I get 405 whenever I try and use them.
Have you managed to use Prisma client in Edge by any change? I get 405 whenever I try and use them.
CalifornianOP
I am currently migrating all my database from MongoDB to PostgreSQL to be able to use Prisma's drive adapter, which promises to support Edge. Let's see...
Eulophid wasp
I now have prisma working from an edge function on Vercel... I can tell you what i did if it might help 🙂
CalifornianOP
Oh, tell me please 🙏
I spent some hours to migrate my whole database from MongoDB to PostgreSQL (I had to write a bunch of conversion scripts) and now I realize Prisma doesn't honor its promise of supporting PostgreSQL on Edge runtime via their driver adapter. I followed all the instructions from this page (https://www.prisma.io/docs/orm/overview/databases/postgresql#using-the-node-postgres-driver) to set up the driver adapter for my PostgreSQL database, but when I launch the app:

I spent some hours to migrate my whole database from MongoDB to PostgreSQL (I had to write a bunch of conversion scripts) and now I realize Prisma doesn't honor its promise of supporting PostgreSQL on Edge runtime via their driver adapter. I followed all the instructions from this page (https://www.prisma.io/docs/orm/overview/databases/postgresql#using-the-node-postgres-driver) to set up the driver adapter for my PostgreSQL database, but when I launch the app:
⨯ Error: The edge runtime does not support Node.js 'crypto' module.
Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime
at <unknown> (webpack-internal:///(middleware)/./node_modules/next/dist/esm/server/web/globals.js:33)
at Object.get (webpack-internal:///(middleware)/./node_modules/next/dist/esm/server/web/globals.js:33:19)
at eval (webpack-internal:///(middleware)/./node_modules/pg/lib/crypto/utils-webcrypto.js:22:32)
at (middleware)/./node_modules/pg/lib/crypto/utils-webcrypto.js (file:///home/worker/code/.next/server/middleware.js:1270:1)
at __webpack_require__ (file:///home/worker/code/.next/server/edge-runtime-webpack.js:37:33)
at fn (file:///home/worker/application/.next/server/edge-runtime-webpack.js:384:21)
at eval (webpack-internal:///(middleware)/./node_modules/pg/lib/crypto/utils.js:8:20)
at (middleware)/./node_modules/pg/lib/crypto/utils.js (file:///home/worker/code/.next/server/middleware.js:1281:1)
at __webpack_require__ (file:///home/worker/code/.next/server/edge-runtime-webpack.js:37:33)
at fn (file:///home/worker/code/.next/server/edge-runtime-webpack.js:384:21)
at eval (webpack-internal:///(middleware)/./node_modules/pg/lib/crypto/sasl.js:3:16) {
middleware: true
Eulophid wasp
OK... I'll try and remember 🫠 ....
Firstly - here's the guides I used...
- https://www.prisma.io/blog/database-access-on-the-edge-8F0t1s1BqOJE
- https://www.prisma.io/docs/accelerate/getting-started
You can create a project at https://console.prisma.io - you then use your sql connection string to create an accelerate connection string.
Firstly - here's the guides I used...
- https://www.prisma.io/blog/database-access-on-the-edge-8F0t1s1BqOJE
- https://www.prisma.io/docs/accelerate/getting-started
You can create a project at https://console.prisma.io - you then use your sql connection string to create an accelerate connection string.
Packages:
"@prisma/extension-accelerate"
"@prisma/client": "^5.12.1"
"prisma": "^5.12.1"
"@prisma/extension-accelerate"
"@prisma/client": "^5.12.1"
"prisma": "^5.12.1"
import { PrismaClient } from '@prisma/client/edge';
let prisma = new PrismaClient();Use this (edge) client on your edge function.
You also need to change your build command in package.json to
"build": "prisma generate --no-engine && next build",(note the --no-engine)
CalifornianOP
Ah, you took their
I will probably move away from Prisma if I can't make the free solution work
accelerate stuff. I had saw it but don't want to pay for it. I was trying to make the driver adapter solution work, since it's free. But thanks anyway for your time
I will probably move away from Prisma if I can't make the free solution work

Eulophid wasp
I did... there's a free accelerate plan though...
CalifornianOP
Hmm, I don't know. Between adding a dependency that bottlenecks my db queries just to get Edge runtime and moving away from Prisma to some free solution that supports Edge, I prefer the later.
But it's weird that their free driver adapter solution doesn't work on Edge, as they promise in the docs. I will probably put some question on github on the related packages to understand the problem better before moving away from Prisma, since it will be very annoying to move away after some months using it...
But it's weird that their free driver adapter solution doesn't work on Edge, as they promise in the docs. I will probably put some question on github on the related packages to understand the problem better before moving away from Prisma, since it will be very annoying to move away after some months using it...
Common House-Martin
might be worth checking out https://open-next.js.org/get_started#other-frameworks
Eulophid wasp
@Californian Let me know ow it goes! I'd rather not be on Accelerate if I can help but needed to get it running...
CalifornianOP
Yeah, sure I will put questions on
prisma and node-postgres githubs. Let's see how it goes... ThanksCalifornianOP
Folks, I come with good news
I got a Vercel PostgreSQL database working via Prisma on Next middleware (Edge runtime) using the Neon database driver and the Prisma's Neon driver adapter. Docs here (I think these docs came very recently): https://www.prisma.io/docs/orm/prisma-client/deployment/edge/deploy-to-vercel#vercel-postgres
No need for Prisma accelerate for this.
Cheers 🥂
I got a Vercel PostgreSQL database working via Prisma on Next middleware (Edge runtime) using the Neon database driver and the Prisma's Neon driver adapter. Docs here (I think these docs came very recently): https://www.prisma.io/docs/orm/prisma-client/deployment/edge/deploy-to-vercel#vercel-postgres
No need for Prisma accelerate for this.
Cheers 🥂
Answer
Eulophid wasp
Thanks for posting back @Californian 👌🏼