P
Prisma•4mo ago
Faith

Error validating datasource `db`: the URL must start with the protocol `prisma://`

Hello everyone! Im currently getting this problem that i descriped in the title. where when i try to run Prisma on cloudflare workers i tried the soultion from here "https://www.prisma.io/docs/orm/reference/prisma-client-reference#programmatically-override-a-datasource-url" but cant seem to get it to work .dev.vars DATABASE_URL="DATABASE_URL=postgresql://postgres:@localhost:5432/postgres?schema=test" this is my prisma client
import { PrismaClient } from "@prisma/client/edge";

export const db = new PrismaClient({
datasources: {
db: {
url: 'file:../src/dev.vars',
},
},
})
import { PrismaClient } from "@prisma/client/edge";

export const db = new PrismaClient({
datasources: {
db: {
url: 'file:../src/dev.vars',
},
},
})
my schema.prisma
generator client {
provider = "prisma-client-js"
engineType = "binary"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
engineType = "binary"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
and when i try to add "prisma://" in the front it says "api key needed"
Prisma Client API | Prisma Documentation
API reference documentation for Prisma Client.
6 Replies
jonfanz
jonfanz•4mo ago
hmm I don't think that will work. DATABASE_URL="DATABASE_URL=postgresql://postgres:@localhost:5432/postgres?schema=test" isn't a valid string, it should just be something like DATABASE_URL=postgresql://postgres:@localhost:5432/postgres?schema=test but even then, there is no PostgreSQL database hosted at localhost on cloudflare workers. This documentation (specific to Cloudflare) might help.
Deploy to Cloudflare Workers & Pages | Prisma Documentation
Learn the things you need to know in order to deploy an app that uses Prisma Client for talking to a database to a Cloudflare Worker or to Cloudflare Pages.
Faith
Faith•4mo ago
Hey i done this and still dont work i still get "the URL must start with the protocol prisma://" i did prisma generate also im trying to set it up locally first or is that not going to work at all? i have now tried this way aswell
fetch: async (request: Request, env: Environment, ctx: ExecutionContext) => {
var prisma = await db(env.DATABASE_URL)

env = { ...env, prisma }

await app.fetch(request, env, ctx);
},
fetch: async (request: Request, env: Environment, ctx: ExecutionContext) => {
var prisma = await db(env.DATABASE_URL)

env = { ...env, prisma }

await app.fetch(request, env, ctx);
},
and then i tried to call it in my router like this
async (data: any, prisma: PrismaClient) => {
const result = await prisma.search.create({
data: {
results: data.results,
createdAt: data.createdAt,
currentCycleIsDone: data.currentCycleIsDone,
noMoreResults: data.noMoreResults,
availableCycles: data.availableCycles,
searchInput: data.searchInput
}
})

console.log('Search created:', result)

return result
}
async (data: any, prisma: PrismaClient) => {
const result = await prisma.search.create({
data: {
results: data.results,
createdAt: data.createdAt,
currentCycleIsDone: data.currentCycleIsDone,
noMoreResults: data.noMoreResults,
availableCycles: data.availableCycles,
searchInput: data.searchInput
}
})

console.log('Search created:', result)

return result
}
im using cloudflare workers + hono btw also updated the db that i export like this
export const db = (databaseUrl: string) => new PrismaClient({
datasources: {
db: {
url: databaseUrl,
},
},
})
export const db = (databaseUrl: string) => new PrismaClient({
datasources: {
db: {
url: databaseUrl,
},
},
})
jonfanz
jonfanz•4mo ago
This is happening locally? What command are you running to start your app? What version of @prisma/client are you using?
Faith
Faith•4mo ago
@Jon Harrell To start it wrangler dev src/index.ts and "@prisma/client": "5.12.1", i think im just going to use cloudflare d1
jonfanz
jonfanz•4mo ago
That will work a lot better. I'm not sure if there's a way to test a local DB against wrangler. Some will work: https://developers.cloudflare.com/workers/databases/connecting-to-databases/ but in general, I'd say that D1 will be the most straightforward 🙂
Cloudflare Docs
Connect to databases · Cloudflare Workers docs
Learn about the different kinds of database integrations Cloudflare supports.
Faith
Faith•4mo ago
Alright thank you so much!