core icon indicating copy to clipboard operation
core copied to clipboard

Correct config for Prisma with Supabase on Cloudflare

Open mtzrmzia opened this issue 1 year ago • 7 comments

Describe the bug I'm trying to relocate my Vercel projects to Cloudflare through NuxtHub. I'm using Prisma ORM with Supabase and on Vercel deploy the project works as expected, but in Cloudflare not. I got 500: Cannot read properties of undefined (reading 'exec'). error but in local works!

Steps to reproduce Steps to reproduce the behavior:

  1. Install prisma and @prisma/client dependencies then add prisma generate to build command:
{
  "name": "esteba-app-appointments",
  "private": true,
  "type": "module",
  "scripts": {
    "build": "prisma generate && nuxt build",
  },
  "dependencies": {
    "@prisma/client": "^5.18.0",
    "@vueuse/core": "^11.0.1",
    "nuxt": "^3.13.0",
    "vue": "latest",
  },
  "devDependencies": {
    "prisma": "^5.18.0",
    "wrangler": "^3.72.2"
  },
  "packageManager": "[email protected]"
}
  1. Create prisma/schema.prisma file:
generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}
  1. set supabase credentials on environment variables project

and thats it, on Vercel this works

Expected behavior Request sent as expected

I'm doing my research, and this issue happens to another person Cloudflare issue

mtzrmzia avatar Aug 24 '24 03:08 mtzrmzia

Duplicate of #38

Would you mind reading https://github.com/nuxt-hub/core/pull/58/files and see if this works?

atinux avatar Aug 24 '24 10:08 atinux

Duplicate of #38

Would you mind reading https://github.com/nuxt-hub/core/pull/58/files and see if this works?

The example uses sqlite and ignores the DATABASE_URL but i'm using postgresql with supabase... just dont work

mtzrmzia avatar Aug 24 '24 17:08 mtzrmzia

I had a similar problem, you could try this https://github.com/prisma/prisma/issues/23500#issuecomment-2147548083. It seems to be a problem with Nitro and Prisma

CristobalMedrano avatar Aug 26 '24 17:08 CristobalMedrano

@mtzrmzia I'm late, but in case someone else is having trouble using Supabase + ORM + Cloudflare.

I did some research and found that Cloudflare Workers aren't based on Node, but on V8 which has a subset of the features of Node. That means if you use an ORM, it'll use default a TCP connection to your DB, which it shouldn't be supported afaik from Cloudflare Workers.

So, the solution is either you ditch:

  • Supabase for another DB with http support (like Neon or D1)
  • Prisma to use "supabasejs" and use PostgREST (to avoid TCP)
  • Cloudflare Workers to other serverless platforms that support TCP (like Vercel)

In my case I decided to migrate my DB to Neon, because it was the fastest and with fewer conflicts to my workflows

EthanITA avatar Nov 24 '24 00:11 EthanITA

so i made it work by just:

schema.prisma

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["driverAdapters"]
}

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

and than:


import { PrismaClient } from '@prisma/client';
import { PrismaPg } from '@prisma/adapter-pg'
import { Pool } from 'pg'

export function getPrismaClient(env) {
    const pool = new Pool({ connectionString: env.DATABASE_URL })
    const adapter = new PrismaPg(pool)
    const prisma = new PrismaClient({ adapter })

    return prisma;
}

works like a charm

marsch avatar Mar 13 '25 21:03 marsch

You could try Prisma with Hyperdrive

  • https://github.com/prisma/ecosystem-tests/blob/dev/driver-adapters-wasm/pg-cf-hyperdrive/prisma/schema.prisma
  • https://github.com/prisma/ecosystem-tests/blob/dev/driver-adapters-wasm/pg-cf-hyperdrive/src/index.js

RihanArfan avatar Apr 15 '25 01:04 RihanArfan