Unable to setup prisma accelerate with cloudflare workers

index.ts

import { PrismaClient } from '@prisma/client/edge';
import { withAccelerate } from '@prisma/extension-accelerate';
export default {
async fetch(request, env, ctx): Promise<Response> {
const prisma = new PrismaClient().$extends(withAccelerate());
console.log(env);
const user = await prisma.user.findMany({
where: {
email: {
contains: 'alice@prisma.io',
},
},
cacheStrategy: { swr: 60, ttl: 60 },
});
console.log(user);
return new Response('Hello World!');
},
} satisfies ExportedHandler<Env>;

import { PrismaClient } from '@prisma/client/edge';
import { withAccelerate } from '@prisma/extension-accelerate';
export default {
async fetch(request, env, ctx): Promise<Response> {
const prisma = new PrismaClient().$extends(withAccelerate());
console.log(env);
const user = await prisma.user.findMany({
where: {
email: {
contains: 'alice@prisma.io',
},
},
cacheStrategy: { swr: 60, ttl: 60 },
});
console.log(user);
return new Response('Hello World!');
},
} satisfies ExportedHandler<Env>;
.env DATABASE_URL="prisma accelerate api key" DIRECT_URL="database uri string" wrangler.toml
name = "app2"
main = "src/index.ts"
compatibility_date = "2024-07-01"
compatibility_flags = ["nodejs_compat"]
[vars]
DATABASE_URL = "prisma accelerate api key"
name = "app2"
main = "src/index.ts"
compatibility_date = "2024-07-01"
compatibility_flags = ["nodejs_compat"]
[vars]
DATABASE_URL = "prisma accelerate api key"
schema.prisma
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
directUrl = env("DIRECT_URL")
}

model User {
id Int @id @default(autoincrement())
email String @unique
name String?
}
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
directUrl = env("DIRECT_URL")
}

model User {
id Int @id @default(autoincrement())
email String @unique
name String?
}
I am getting following error when I hit the server
Invalid `prisma.user.findMany()` invocation: error: Environment variable not found: DATABASE_URL. In Cloudflare module Workers, environment variables are available only in the Worker's `env` parameter of `fetch`. To solve this, provide the connection string directly: https://pris.ly/d/cloudflare-datasource-url
Invalid `prisma.user.findMany()` invocation: error: Environment variable not found: DATABASE_URL. In Cloudflare module Workers, environment variables are available only in the Worker's `env` parameter of `fetch`. To solve this, provide the connection string directly: https://pris.ly/d/cloudflare-datasource-url
What is going wrong here?
3 Replies
Cyb3r-Jak3
Cyb3r-Jak3•2mo ago
Have you tried following the link that says it has the solution? https://pris.ly/d/cloudflare-datasource-url
Abhaykd
Abhaykd•2mo ago
It worked 🙂 My bad, should have tried it at least. I was following a couple of months old video, thought It should work here as well if it's working there, that's why I was not looking to change the code. Anyways, Thanks.
richburdon
richburdon•2mo ago
hi @Cyb3r-Jok3 @Abhaykd did this work for you. I have been using Prisma in my worker project, but when I tried to access the DB using prisma from another pages worker i started to get this error. in the worker we're setting up the client like this: new PrismaClient({ adapter: new PrismaD1(c.env.DB) }); assuming that Pages project require the datasource url to be set -- what value would that be?
Want results from more Discord servers?
Add your server