neondatabase / serverless

Connect to Neon PostgreSQL from serverless/worker/edge functions
https://www.npmjs.com/package/@neondatabase/serverless
MIT License
321 stars 13 forks source link

using the serverless/websocket driver fails locally? #26

Closed Ncage1974 closed 1 year ago

Ncage1974 commented 1 year ago

I am thinking on eventually deploying my project to cloudflare workers so i'm trying to use the serverless drivers locally but when I try to connect this is the error i'm getting:

[nuxt] [request error] [unhandled] [500] All attempts to open a WebSocket to connect to the database failed. Please refer to https://github.com/neondatabase/serverless#run-on-node                                                                                                                                                                                                      3:37:57 PM
  at ws (./node_modules/.pnpm/@neondatabase+serverless@0.4.14/node_modules/@neondatabase/serverless/index.js:43:2927)  
  at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

I was assuming i would want to use the pooling connection string but i tried to see if using the direct connection string would make any difference and it doesn't.

I'm hoping i don't have to use different methods to connect went I deploy vs when i develop locally? As you can see in the error message i'm using Nuxt. There server is called Nitro which is pretty much just a node server. I looked at the link in the error message and the only suggestion was to kind of ditch websockets & use tcp\ip but like i said i prefer not to do that because i want development to be exactly the same as when its deployed in production. I'm using drizzle orm to connect & used exactly the same that is on their site:

      console.log("hi");
      const config = useRuntimeConfig();
      const pool = new Pool({ connectionString: config.databaseUrl });
      const db = drizzle(pool);
      const result = await db.select().from(myTable);
      await pool.end();
      return 1;

line 5 is where its failing when it tries to do the select statement. Any help would be appreciated.

thanks....

feliche93 commented 1 year ago

@Ncage1974 I have exact the same issue on the following packages. Have you found any solution?

    "next": "13.4.6",
    "@neondatabase/serverless": "^0.4.14",
    "drizzle-orm": "^0.26.5",
    "drizzle-zod": "^0.4.3",

My error says the following:

Server Error
TypeError: Cannot read properties of undefined (reading 'Client')

This error happened while generating the page. Any console logs will be displayed in the terminal window.
Call Stack
mod
node_modules/.pnpm/@neondatabase+serverless@0.4.14/node_modules/@neondatabase/serverless/index.mjs (4:22)
Module.(sc_server)/./node_modules/.pnpm/@neondatabase+serverless@0.4.14/node_modules/@neondatabase/serverless/index.mjs
file:///Users/felixvemmer/Developer/backlinkgpt/packages/frontend/.next/server/app/(app)/(paid)/campaigns/page.js (8578:1)
__webpack_require__
file:///Users/felixvemmer/Developer/backlinkgpt/packages/frontend/.next/server/webpack-runtime.js (33:43)
eval
webpack-internal:///(sc_server)/./src/lib/db.ts (5:82)
Module.(sc_server)/./src/lib/db.ts
file:///Users/felixvemmer/Developer/backlinkgpt/packages/frontend/.next/server/app/(app)/(paid)/campaigns/page.js (11193:1)
__webpack_require__
file:///Users/felixvemmer/Developer/backlinkgpt/packages/frontend/.next/server/webpack-runtime.js (33:43)
eval
webpack-internal:///(sc_server)/./src/lib/neon.ts (8:61)
Module.(sc_server)/./src/lib/neon.ts
file:///Users/felixvemmer/Developer/backlinkgpt/packages/frontend/.next/server/app/(app)/(paid)/campaigns/page.js (11215:1)
__webpack_require__
file:///Users/felixvemmer/Developer/backlinkgpt/packages/frontend/.next/server/webpack-runtime.js (33:43)
eval
webpack-internal:///(sc_server)/./src/app/(app)/(paid)/layout.tsx (7:67)
jawj commented 1 year ago

@feliche93 The error message you pasted doesn't look much like the same issue. Could you post this as a new GitHub issue, including a link to a minimal repo that reproduces the problem?

@Ncage1974 The problem here seems to be that Node is not a very faithful imitation of the Cloudflare Workers environment. In particular, the serverless driver uses WebSockets, which aren't available in Node without a third-party package.

As it also says at the link in the error message, for Node you can import ws or undici and either set the relevant constructor as globalThis.WebSocket or pass it to the driver's webSocketConstructor config option ... but simply importing ws or undici will then give you errors when you try to deploy to Cloudflare Workers.

The latest version of the serverless driver experimentally supports http/fetch transport for simple one-shot queries using Pool.query, so if you're not doing anything with sessions or transactions this might be a get-out. To use that, you'd do this:

import { neonConfig } from '@neondatabase/serverless';
neonConfig.poolQueryViaFetch = true;

It's not a comprehensive fix, but does this work for you for now?

If not, would you be able to post a repo that contains a minimal reproduction of the issue, so I can take a better look?

Ncage1974 commented 1 year ago

@feliche93 i agree with @jawj it doesn't appear your issue has anything to do with mine

@jawj thanks. I will give this a try & get report back here by next Monday (crazy busy weekend). If i can't get it working i will post the repo.

Ncage1974 commented 1 year ago

@jawj thanks for your help. I tried your suggestion & it does in fact work. I appreciate it.