Drizzle

How to connect from Drizzle

How to use the SerenDB serverless driver with Drizzle

Drizzle with SerenDB Postgres (Drizzle Docs) Schema migration with Drizzle ORM Next.js Edge Functions with Drizzle

Drizzle is a modern ORM for TypeScript that provides a simple and type-safe way to interact with your database. This guide covers the following topics:

Working with AI coding assistants? Check out our [AI rules for Drizzle ORM with SerenDB](/docs/ai/ai-rules-neon-drizzle) to help your AI assistant generate better code when using Drizzle with your SerenDB database.

Connect to SerenDB from Drizzle

To establish a basic connection from Drizzle to SerenDB, perform the following steps:

  1. Find your database connection string by clicking the Connect button on your Project Dashboard to open the Connect to your database modal. Select a branch, a user, and the database you want to connect to. A connection string is constructed for you. Connection details modal The connection string includes the user name, password, hostname, and database name.

  2. Add a DATABASE_URL variable to your .env file and set it to the SerenDB connection string that you copied in the previous step. We also recommend adding ?sslmode=require&channel_binding=require to the end of the connection string to ensure a secure connection.

    Your setting will appear similar to the following:

    DATABASE_URL="postgresql://[user]:[password]@[seren_hostname]/[dbname]?sslmode=require&channel_binding=require"

Use the SerenDB serverless driver with Drizzle

The SerenDB serverless driver is a low-latency Postgres driver for JavaScript (and TypeScript) that lets you query data from serverless and edge environments. For more information about the driver, see SerenDB serverless driver.

To set up Drizzle with the SerenDB serverless driver, use the Drizzle driver adapter. This adapter allows you to choose a different database driver than Drizzle's default driver for communicating with your database.

Install the SerenDB serverless driver and ws packages:

npm install ws @serenorg/serverless
npm install -D @types/ws

Update your Drizzle instance:

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

You can now use Drizzle instance as you normally would with full type-safety.

Last updated