Need setup Prisma with cloudflare pages
We already have docs on setting up Prisma with Cloudflare workers: https://www.prisma.io/docs/guides/deployment/deployment-guides/deploying-to-cloudflare-workers#9-set-the-data-proxy-connection-string-in-your-environment But Pages is a bit different. So the rest of this document is probably okay but we need another paragraph here addressing how to get Data Proxy to work with CF Pages.
Notes: I'm trying to get this to work with Remix at the moment and can't do it. The specific problem I have is how to pass the Data Proxy URL to new PrismaClient() and how to ensure prisma generate --data-proxy runs in production. I've got the Remix side of things done. I can grab the env variable, but I cannot setup prisma, I get:
Attempted to access binding using global in modules.
You must use the 2nd env parameter passed to exported handlers/Durable Object constructors, or context.env with Pages Functions.
I write all this not because I'm looking for help here, I know this isn't the right place, but to demonstrate the problems I'm seeing setting up DataProxy with CF Pages and hoping that the docs can be amended to help us know how to do this properly.
Can you provide some links how one can use Prisma in Cloudflare Pages, and use Remix to do that? I think this is generally not a concept anyone here is familiar with - I am pretty sure it never came up yet.
Maybe we should leave the Remix part out because it complicates things a bit. Assuming we can get the db access url from the framework somehow, how do we use it to create a new PrismaClient?
With Remix, env variables are passed into the function on a context object, so I've been trying this:
const prisma = new PrismaClient({
datasources: {
db: {
url: context.DATABASE_URL as string,
},
},
});
But I'm getting an error. Note that context.DATABASE_URL is fine, it's the correct db access url
What error? If that is the valid connection string, PrismaClient would use it on instantiation.
Yeah that's what's confusing me, I'm getting an error:
Attempted to access binding using global in modules.
You must use the 2nd env parameter passed to exported handlers/Durable Object constructors, or context.env with Pages Functions.
Is there something else I need to setup, like could prisma generate throw this error?
This is not an error message from Prisma.
That aside:
... or context.env with Pages Functions.
Could this mean that it wants you to use context.env.DATABASE_URL?
Right sorry, I got that error only when trying to create a new Prisma client so I assumed it was a Prisma error.. I asked about this on the Remix discord and this is how we need to handle env with Remix: https://github.com/sergiodxa/sergiodxa.com/blob/b47fc690556426ae103d93da763c9979d9e50f53/server/index.ts#L27
I guess maybe this is a Remix problem rather than a Prisma problem after all.
Still worth documenting of course - I bet you are not the only Remix user that wants to use Prisma. And of course we want that to be the case much more often :D
CloudFlare Pages now can run well full-stack Nextjs with Next-Auth v5 as Pages Functions. Please upgrade serverless DB adapters to work with Prisma such as: Neon, TiDBcloud like Kysely.