0% found this document useful (0 votes)
30 views3 pages

Drizzle ORM - Database Connection

The document provides an overview of how to establish a database connection using Drizzle ORM with various database drivers, including node-postgres and SQLite. It explains the process of running SQL queries and accessing the database client, as well as compatibility with serverless environments. Additionally, it includes examples of connection URLs and encourages users to explore driver-specific documentation for further details.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views3 pages

Drizzle ORM - Database Connection

The document provides an overview of how to establish a database connection using Drizzle ORM with various database drivers, including node-postgres and SQLite. It explains the process of running SQL queries and accessing the database client, as well as compatibility with serverless environments. Additionally, it includes examples of connection URLs and encourages users to explore driver-specific documentation for further details.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

2/18/25, 5:23 AM Drizzle ORM - Database connection

Database connection with Drizzle


Drizzle ORM runs SQL queries on your database via database drivers.

index.ts schema.ts

import { drizzle } from "drizzle-orm/node-postgres"


import { users } from "./schema"

const db = drizzle(process.env.DATABASE_URL);
const usersCount = await db.$count(users);

┌──────────────────────┐
│ db.$count(users) │ <--- drizzle query
└──────────────────────┘
│ ʌ
select count(*) from users -│ │
│ │- [{ count: 0 }]
v │
┌─────────────────────┐
│ node-postgres │ <--- database driver
└─────────────────────┘
│ ʌ
01101000 01100101 01111001 -│ │
│ │- 01110011 01110101 01110000
v │
┌────────────────────┐
│ Database │
└────────────────────┘

Under the hood Drizzle will create a node-postgres driver instance which you can access
via db.$client if necessary

import { drizzle } from "drizzle-orm/node-postgres"

const db = drizzle(process.env.DATABASE_URL);
const pool = db.$client;

// above is equivalent to
import { drizzle } from "drizzle-orm/node-postgres";
import { Pool } from "pg";

const pool = new Pool({


connectionString: process.env.DATABASE_URL,
});
const db = drizzle({ client: pool });

https://orm.drizzle.team/docs/connect-overview 1/3
2/18/25, 5:23 AM Drizzle ORM - Database connection

Drizzle is by design natively compatible with every edge or serverless runtime, whenever you’d
need access to a serverless database - we’ve got you covered

Neon HTTP Neon with websockets Vercel Postgres PlanetScale HTTP Cloudflare d1

import { drizzle } from "drizzle-orm/neon-http";

const db = drizzle(process.env.DATABASE_URL);

And yes, we do support runtime specific drivers like Bun SQLite or Expo SQLite:

import { drizzle } from "drizzle-orm/bun-sqlite"

const db = drizzle(); // <--- will create an in-memory db


const db = drizzle("./sqlite.db");

import { drizzle } from "drizzle-orm/expo-sqlite";


import { openDatabaseSync } from "expo-sqlite/next";

const expo = openDatabaseSync("db.db");


const db = drizzle(expo);

Database connection URL


Just in case if you’re not familiar with database connection URL concept

postgresql://alex:[email protected]/dbname
└──┘ └───────┘ └─────────────────────────────────────────────┘ └────┘
ʌ ʌ ʌ ʌ
role -│ │ │- hostname │- database

│- password

Next steps
Feel free to check out per-driver documentations

PostgreSQL drivers MySQL drivers SQLite drivers Native SQLite

PostgreSQL MySQL SQLite Expo SQLite


Neon PlanetsScale Turso OP SQLite
Vercel Postgres TiDB Cloudflare D1 React Native SQLite
Supabase Bun SQLite
Xata

https://orm.drizzle.team/docs/connect-overview 2/3
2/18/25, 5:23 AM Drizzle ORM - Database connection

PGLite

https://orm.drizzle.team/docs/connect-overview 3/3

You might also like