NeonN
Neon11mo ago
1 reply
spotty-amber

What is the proper way to connect neon with drizzle orm in next js?

I'm working on an next js application deployed to vercel with drizzle orm - neon postgres
Following the drizzle orm docs I've setup my db as below
import { neon } from "@neondatabase/serverless"
import { config } from "dotenv"
import { drizzle } from "drizzle-orm/neon-http"

config({ path: ".env.local" })

const sql = neon(process.env.DATABASE_URL as string)
export const db = drizzle({ client: sql })

but i get the following warning during generating migrations: Warning '@neondatabase/serverless' can only connect to remote Neon/Vercel Postgres/Supabase instances through a websocket

https://neon.tech/docs/guides/drizzle#use-the-neon-serverless-driver-with-drizzle
In neon docs

import 'dotenv/config';
import { drizzle } from 'drizzle-orm/neon-http';
import { neon } 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

const sql = neon(process.env.DATABASE_URL);

export const db = drizzle({ client: sql });

Or from neon - connect to your db modal
import { Pool } from '@neondatabase/serverless';
const pool = new Pool({connectionString: 'postgresql:/xxx?sslmode=require' });


I will primarily use this connection from server components / api routes for CRUD & transactions, What's the proper setup for this case?
Neon
How to connect from Drizzle How to use the Neon serverless driver with Drizzle Drizzle with Neon Postgres (Drizzle Docs) Schema migration with Drizzle ORM Next.js Edge Functions with Drizzle Drizzle i...
Connect from Drizzle to Neon - Neon Docs
Was this page helpful?