Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prisma calls hanging on Cloudflare Workers #128

Open
jmif opened this issue Dec 5, 2024 · 4 comments
Open

Prisma calls hanging on Cloudflare Workers #128

jmif opened this issue Dec 5, 2024 · 4 comments

Comments

@jmif
Copy link

jmif commented Dec 5, 2024

Not sure if this is a Prisma issue or a Neon issue, so opening both (Prisma issue here)

When running with wrangler dev using Prisma + NeonDB, the promises from basic database calls do not resolve, but the results are reflected in the database. I've followed the instructions here.

Steps to reproduce

I've created a repro here.

  1. Clone repo and add DATABASE_URL=xxx to .dev.vars in the root of the repo.
  2. Run npm run dev
  3. Performa GET request to the root route (port will be specified in npm run dev output)
  4. The request should hang and you should see the first console.log statement but not the second. You should also see the data from the first call inserted into the database, but not from the second.

Expected result

Request does not hang and inserts two records.

Actual result

Request hangs, promise of initial query does not resolve, but initial query is successful in that it's result is reflected in database.

Environment

OS: macOS
Database: Neon Postgres 16
Node.js version: 20.11.1

@jmif
Copy link
Author

jmif commented Dec 22, 2024

FYI I think this is an issue with this package, or at least an issue in with the docs. I removed Prisma completely and attempted to query my Neon database directly using a Pool and the same issue occurred.

When I set this, queries start working again:

neonConfig.poolQueryViaFetch = true;

According to the docs here this shouldn't be required to work in Cloudflare workers, so I think it's a bug. I also tried to set the ws constructor as I'm using Node compatibility mode but requests continued to hang.

@nvti
Copy link

nvti commented Dec 23, 2024

@jmif Did you test this with Prisma too?
I have the same issue, even with set neonConfig.poolQueryViaFetch = true;

My prisma.ts

import 'dotenv/config';
import { PrismaClient } from '@prisma/client';
import { PrismaNeon } from '@prisma/adapter-neon';
import { Pool, neonConfig } from '@neondatabase/serverless';

import ws from 'ws';
neonConfig.webSocketConstructor = ws;

// To work in edge environments (Cloudflare Workers, Vercel Edge, etc.), enable querying over fetch
neonConfig.poolQueryViaFetch = true

// Type definitions
declare global {
   var prisma: PrismaClient | undefined
}

const connectionString = `${process.env.DATABASE_URL}`;

const pool = new Pool({ connectionString });
const adapter = new PrismaNeon(pool);
const prisma = global.prisma || new PrismaClient({ adapter });

if (process.env.NODE_ENV === 'development') global.prisma = prisma;

export default prisma;

@jmif
Copy link
Author

jmif commented Dec 23, 2024

Yeah - adding the poolQueryViaFetch makes Prisma work for me

@nvti
Copy link

nvti commented Dec 23, 2024

Maybe the difference is I use NextJS (next-on-pages).
My current workaround is to recreate prisma instance on every request. It makes my API too slow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants