drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

[BUG]: postgres not working on edge, nextjs middleware

Open da1z opened this issue 1 year ago • 2 comments

What version of drizzle-orm are you using?

^0.30.2

What version of drizzle-kit are you using?

^0.20.14

Describe the Bug

Having exception in nextjs middleware. Seems like problems of postgres with edge runtime?

web:dev: ⨯ cloudflare:sockets web:dev: Module build failed: UnhandledSchemeError: Reading from "cloudflare:sockets" is not handled by plugins (Unhandled scheme). web:dev: Webpack supports "data:" and "file:" URIs by default. web:dev: You may need an additional plugin to handle "cloudflare:" URIs. web:dev: Import trace for requested module: web:dev: cloudflare:sockets web:dev: ../../node_modules/postgres/cf/polyfills.js web:dev: ../../node_modules/postgres/cf/src/index.js web:dev: ../../packages/db/src/index.ts web:dev: ./auth.ts

import { drizzle } from 'drizzle-orm/postgres-js';

import postgres from 'postgres';
import * as auth from './schema/auth';

export const schema = { ...auth };

// for query purposes
const queryClient = postgres(process.env.DATABASE_URL!);
export const db = drizzle(queryClient, { schema });

Expected behavior

No exception

Environment & setup

nextjs postgress drizzle-orm/postgres-js

da1z avatar Mar 16 '24 22:03 da1z

I have a similar problem on Next.js (and not even actually using edge) unless I remove middleware.

My error says (when using Drizzle + postgres.js):

[auth][error] AdapterError: Read more at [Auth.js | Errors](https://errors.authjs.dev/#adaptererror) 
[auth][cause]: Error: The edge runtime does not support Node.js 'net' module.
Learn More: [Using Node.js Modules in Edge Runtime](https://nextjs.org/docs/messages/node-module-in-edge-runtime) 
    at Object.apply (webpack-internal:///(middleware)/./node_modules/next/dist/esm/server/web/globals.js:43:19)
    at createSocket (webpack-internal:///(middleware)/./node_modules/postgres/src/connection.js:146:44)
    at Timeout.connect [as _onTimeout] (webpack-internal:///(middleware)/./node_modules/postgres/src/connection.js:343:31)
    at listOnTimeout (node:internal/timers:573:17)
    at process.processTimers (node:internal/timers:514:7)
[auth][error] SessionTokenError: Read more at [Auth.js | Errors](https://errors.authjs.dev/#sessiontokenerror) 
[auth][cause]: Error: The edge runtime does not support Node.js 'net' module.
Learn More: [Using Node.js Modules in Edge Runtime](https://nextjs.org/docs/messages/node-module-in-edge-runtime) 
    at Object.apply (webpack-internal:///(middleware)/./node_modules/next/dist/esm/server/web/globals.js:43:19)
    at createSocket (webpack-internal:///(middleware)/./node_modules/postgres/src/connection.js:146:44)
    at Timeout.connect [as _onTimeout] (webpack-internal:///(middleware)/./node_modules/postgres/src/connection.js:343:31)
    at listOnTimeout (node:internal/timers:573:17)
    at process.processTimers (node:internal/timers:514:7)

mlntr avatar May 20 '24 10:05 mlntr

I have a similar problem on Next.js (and not even actually using edge) unless I remove middleware.

My error says (when using Drizzle + postgres.js):

[auth][error] AdapterError: Read more at [Auth.js | Errors](https://errors.authjs.dev/#adaptererror) 
[auth][cause]: Error: The edge runtime does not support Node.js 'net' module.
Learn More: [Using Node.js Modules in Edge Runtime](https://nextjs.org/docs/messages/node-module-in-edge-runtime) 
    at Object.apply (webpack-internal:///(middleware)/./node_modules/next/dist/esm/server/web/globals.js:43:19)
    at createSocket (webpack-internal:///(middleware)/./node_modules/postgres/src/connection.js:146:44)
    at Timeout.connect [as _onTimeout] (webpack-internal:///(middleware)/./node_modules/postgres/src/connection.js:343:31)
    at listOnTimeout (node:internal/timers:573:17)
    at process.processTimers (node:internal/timers:514:7)
[auth][error] SessionTokenError: Read more at [Auth.js | Errors](https://errors.authjs.dev/#sessiontokenerror) 
[auth][cause]: Error: The edge runtime does not support Node.js 'net' module.
Learn More: [Using Node.js Modules in Edge Runtime](https://nextjs.org/docs/messages/node-module-in-edge-runtime) 
    at Object.apply (webpack-internal:///(middleware)/./node_modules/next/dist/esm/server/web/globals.js:43:19)
    at createSocket (webpack-internal:///(middleware)/./node_modules/postgres/src/connection.js:146:44)
    at Timeout.connect [as _onTimeout] (webpack-internal:///(middleware)/./node_modules/postgres/src/connection.js:343:31)
    at listOnTimeout (node:internal/timers:573:17)
    at process.processTimers (node:internal/timers:514:7)

middleware runs on edge. to be honest it is not drizzle problem it is postgres package not edge compatibl. I switch to vercel/postgress and it works on edge with drizzle

da1z avatar May 20 '24 19:05 da1z

I have a similar problem on Next.js (and not even actually using edge) unless I remove middleware. My error says (when using Drizzle + postgres.js):

[auth][error] AdapterError: Read more at [Auth.js | Errors](https://errors.authjs.dev/#adaptererror) 
[auth][cause]: Error: The edge runtime does not support Node.js 'net' module.
Learn More: [Using Node.js Modules in Edge Runtime](https://nextjs.org/docs/messages/node-module-in-edge-runtime) 
    at Object.apply (webpack-internal:///(middleware)/./node_modules/next/dist/esm/server/web/globals.js:43:19)
    at createSocket (webpack-internal:///(middleware)/./node_modules/postgres/src/connection.js:146:44)
    at Timeout.connect [as _onTimeout] (webpack-internal:///(middleware)/./node_modules/postgres/src/connection.js:343:31)
    at listOnTimeout (node:internal/timers:573:17)
    at process.processTimers (node:internal/timers:514:7)
[auth][error] SessionTokenError: Read more at [Auth.js | Errors](https://errors.authjs.dev/#sessiontokenerror) 
[auth][cause]: Error: The edge runtime does not support Node.js 'net' module.
Learn More: [Using Node.js Modules in Edge Runtime](https://nextjs.org/docs/messages/node-module-in-edge-runtime) 
    at Object.apply (webpack-internal:///(middleware)/./node_modules/next/dist/esm/server/web/globals.js:43:19)
    at createSocket (webpack-internal:///(middleware)/./node_modules/postgres/src/connection.js:146:44)
    at Timeout.connect [as _onTimeout] (webpack-internal:///(middleware)/./node_modules/postgres/src/connection.js:343:31)
    at listOnTimeout (node:internal/timers:573:17)
    at process.processTimers (node:internal/timers:514:7)

middleware runs on edge. to be honest it is not drizzle problem it is postgres package not edge compatibl. I switch to vercel/postgress and it works on edge with drizzle

can you use your own database with vercel posgres module?

kam-st avatar Jun 08 '24 20:06 kam-st

I have a similar problem on Next.js (and not even actually using edge) unless I remove middleware. My error says (when using Drizzle + postgres.js):

[auth][error] AdapterError: Read more at [Auth.js | Errors](https://errors.authjs.dev/#adaptererror) 
[auth][cause]: Error: The edge runtime does not support Node.js 'net' module.
Learn More: [Using Node.js Modules in Edge Runtime](https://nextjs.org/docs/messages/node-module-in-edge-runtime) 
    at Object.apply (webpack-internal:///(middleware)/./node_modules/next/dist/esm/server/web/globals.js:43:19)
    at createSocket (webpack-internal:///(middleware)/./node_modules/postgres/src/connection.js:146:44)
    at Timeout.connect [as _onTimeout] (webpack-internal:///(middleware)/./node_modules/postgres/src/connection.js:343:31)
    at listOnTimeout (node:internal/timers:573:17)
    at process.processTimers (node:internal/timers:514:7)
[auth][error] SessionTokenError: Read more at [Auth.js | Errors](https://errors.authjs.dev/#sessiontokenerror) 
[auth][cause]: Error: The edge runtime does not support Node.js 'net' module.
Learn More: [Using Node.js Modules in Edge Runtime](https://nextjs.org/docs/messages/node-module-in-edge-runtime) 
    at Object.apply (webpack-internal:///(middleware)/./node_modules/next/dist/esm/server/web/globals.js:43:19)
    at createSocket (webpack-internal:///(middleware)/./node_modules/postgres/src/connection.js:146:44)
    at Timeout.connect [as _onTimeout] (webpack-internal:///(middleware)/./node_modules/postgres/src/connection.js:343:31)
    at listOnTimeout (node:internal/timers:573:17)
    at process.processTimers (node:internal/timers:514:7)

middleware runs on edge. to be honest it is not drizzle problem it is postgres package not edge compatibl. I switch to vercel/postgress and it works on edge with drizzle

can you use your own database with vercel posgres module?

they use neon db under the hood. to use it with your own db you need websocket proxy , more info here https://github.com/neondatabase/serverless/blob/main/DEPLOY.md

da1z avatar Jun 10 '24 21:06 da1z

this is the issue with postgres.js and not drizzle You can use other drivers for such cases

AndriiSherman avatar Jul 11 '24 09:07 AndriiSherman