DT
Drizzle Team•10mo ago
Jim

Defining websocket for NeonDB connection

Hey all, I'm having issues with using neondb on a playwright/test package in my monorepo. The connections work fine in my nextjs app but fail when running tests.
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
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
The error recommends defining a websocket constructor to the config of the neon client like so:
import { neonConfig } from '@neondatabase/serverless';
import ws from 'ws';
neonConfig.webSocketConstructor = ws;
import { neonConfig } from '@neondatabase/serverless';
import ws from 'ws';
neonConfig.webSocketConstructor = ws;
But currently I'm using the Pool as recommended by drizzle like so:
import { drizzle } from 'drizzle-orm/neon-serverless';
import { Pool } from '@neondatabase/serverless';
import * as schema from "./schema";
import dotenv from "dotenv";
dotenv.config({ path: ".env.local" });

const pool = new Pool({ connectionString: process.env.DB_URL });
export const db = drizzle(pool, { schema })
import { drizzle } from 'drizzle-orm/neon-serverless';
import { Pool } from '@neondatabase/serverless';
import * as schema from "./schema";
import dotenv from "dotenv";
dotenv.config({ path: ".env.local" });

const pool = new Pool({ connectionString: process.env.DB_URL });
export const db = drizzle(pool, { schema })
Pool doesn't accept a webSoctetConstructor, so I'm pretty stuck now lol. Any ideas? Thanks for reading!
1 Reply
Jim
JimOP•10mo ago
Fixed it! 🥴 should of just followed the instructions:
import { drizzle } from 'drizzle-orm/neon-serverless';
import { Pool, neonConfig } from '@neondatabase/serverless';
import dotenv from "dotenv";
import * as schema from "./schema";
import ws from 'ws';

dotenv.config({ path: ".env.local" });
neonConfig.webSocketConstructor = ws; // here
const pool = new Pool({ connectionString: process.env.DB_URL });

export const db = drizzle(pool, { schema })
import { drizzle } from 'drizzle-orm/neon-serverless';
import { Pool, neonConfig } from '@neondatabase/serverless';
import dotenv from "dotenv";
import * as schema from "./schema";
import ws from 'ws';

dotenv.config({ path: ".env.local" });
neonConfig.webSocketConstructor = ws; // here
const pool = new Pool({ connectionString: process.env.DB_URL });

export const db = drizzle(pool, { schema })
The Singleton pattern for the neonConfig threw me through a loop
Want results from more Discord servers?
Add your server