Database

Drizzle


Connecting with Drizzle

Drizzle ORM is a TypeScript ORM for SQL databases designed with maximum type safety in mind. You can use their ORM to connect to your database.

1

Install

Install Drizzle and related dependencies.

1
2
npm i drizzle-orm postgresnpm i -D drizzle-kit
2

Create your models

Create a schema.ts file and define your models.

1
2
3
4
5
6
7
import { pgTable, serial, text, varchar } from "drizzle-orm/pg-core";export const users = pgTable('users', { id: serial('id').primaryKey(), fullName: text('full_name'), phone: varchar('phone', { length: 256 }),});
3

Connect

Connect to your database using the Connection Pooler.

In your Database Settings, make sure Use connection pooler is checked, then copy the URI and save it as the DATABASE_URL environment variable. Remember to replace the password placeholder with your actual database password.

1
2
3
4
5
6
7
8
9
10
import 'dotenv/config'import { drizzle } from 'drizzle-orm/postgres-js'import postgres from 'postgres'const connectionString = process.env.DATABASE_URL// Disable prefetch as it is not supported for "Transaction" pool modeexport const client = postgres(connectionString, { prepare: false })export const db = drizzle(client);