N
Neon7mo ago
ratty-blush

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 })
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 });
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' });
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
Connect from Drizzle to Neon - Neon Docs
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...
1 Reply
flat-fuchsia
flat-fuchsia7mo ago
Hi @Rishi Kasyap, Here's how you'd want to do use the Drizzle with Next.js for API routes - https://github.com/neondatabase/examples/tree/main/with-nextjs-drizzle-edge.

Did you find this page helpful?