serverless icon indicating copy to clipboard operation
serverless copied to clipboard

All attempts to open a WebSocket to connect to the database failed.

Open pantharshit007 opened this issue 1 year ago • 0 comments

I am using :

  • Turborepo
  • apps/nextjs 14
  • apps/node v18
  • apps/hono
  • app/vite
  • db (postgress with prisma)

My connection was working fine previously without this statement neonConfig.poolQueryViaFetch = true; but I just applied once in my hono prisma file and after that db calls are not working in node app when I am using flag = driverAdapter

why is that happening when It was working fine without it previously.

this is my setup for next and node I have a different one for hono

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

const prismaClientSingleton = () => {
  const useAdapter = process.env.USE_ADAPTER === "true";

  if (useAdapter) {
    neonConfig.poolQueryViaFetch = true;
    const pool = new Pool({ connectionString: process.env.DATABASE_URL });
    const adapter = new PrismaNeon(pool);
    return new PrismaClient({ adapter } as never);
    // log: process.env.NODE_ENV === "development" ? ["query", "error", "warn"] : ["error"],
  }
  return new PrismaClient();
};

type PrismaClientSingleton = ReturnType<typeof prismaClientSingleton>;

declare global {
  var prismaGlobal: PrismaClientSingleton | undefined;
}

const db = globalThis.prismaGlobal ?? prismaClientSingleton();

// in order to avoid creating too many prisma instances in development.
if (process.env.NODE_ENV !== "production") {
  globalThis.prismaGlobal = db;
}

export default db;
export type * as SchemaTypes from "@prisma/client";
export { Prisma } from "@prisma/client";

In prod my USE_ADAPTER is false its for local development since hono requires me to use the adapter and next works fine without it, but if I enable driverAdapter for hono in local this produces errors for next which leads me to use it (adapter) for next and node in local development also.

Update: If I ran node app solely with this neonConfig.poolQueryViaFetch = true; then it completed db calls and return data but when I am running all 4 apps to test, it fails when nextjs sends a REST req to node app with the error.

EXPECTED: Node app should work without producing any error

CURRENT: currently getting this error

All attempts to open a WebSocket to connect to the database failed. Please refer to https://github.com/neondatabase/serverless/blob/main/CONFIG.md#websocketconstructor-typeof-websocket--undefined. Details: fetch failed

If you want to look at the repo its, but it doesn't contain latest changes here

pantharshit007 avatar Dec 29 '24 16:12 pantharshit007