Drizzle둜 λ°μ΄ν„°λ² μ΄μŠ€ μ—°κ²°ν•˜κΈ°

Drizzle ORM은 λ°μ΄ν„°λ² μ΄μŠ€ λ“œλΌμ΄λ²„λ₯Ό 톡해 λ°μ΄ν„°λ² μ΄μŠ€μ— SQL 쿼리λ₯Ό μ‹€ν–‰ν•©λ‹ˆλ‹€.

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 쿼리
                        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     
                            β”‚               ʌ
select count(*) from users -β”‚               β”‚
                            β”‚               β”‚- [{ count: 0 }]
                            v               β”‚
                         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                         β”‚    node-postgres    β”‚ <--- λ°μ΄ν„°λ² μ΄μŠ€ λ“œλΌμ΄λ²„
                         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚               ʌ
01101000 01100101 01111001 -β”‚               β”‚
                            β”‚               β”‚- 01110011 01110101 01110000
                            v               β”‚
                         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                         β”‚      Database      β”‚ 
                         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

DrizzleλŠ” λ‚΄λΆ€μ μœΌλ‘œ node-postgres λ“œλΌμ΄λ²„ μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€. ν•„μš”ν•  경우 db.$clientλ₯Ό 톡해 이 λ“œλΌμ΄λ²„μ— μ ‘κ·Όν•  수 μžˆμŠ΅λ‹ˆλ‹€.

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

const db = drizzle(process.env.DATABASE_URL);
const pool = db.$client;
// μœ„ μ½”λ“œλŠ” μ•„λž˜μ™€ λ™μΌν•©λ‹ˆλ‹€.
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 });

DrizzleλŠ” 섀계상 λͺ¨λ“  엣지 λ˜λŠ” μ„œλ²„λ¦¬μŠ€ λŸ°νƒ€μž„κ³Ό 기본적으둜 ν˜Έν™˜λ©λ‹ˆλ‹€. μ„œλ²„λ¦¬μŠ€ λ°μ΄ν„°λ² μ΄μŠ€μ— μ ‘κ·Όν•΄μ•Ό ν•  λ•Œλ„ Drizzleκ°€ λ„μ™€μ€λ‹ˆλ‹€.

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);

λ˜ν•œ, Bun SQLiteλ‚˜ Expo SQLite와 같은 λŸ°νƒ€μž„ νŠΉν™” λ“œλΌμ΄λ²„λ„ μ§€μ›ν•©λ‹ˆλ‹€.

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

const db = drizzle(); // <--- λ©”λͺ¨λ¦¬ λ‚΄ λ°μ΄ν„°λ² μ΄μŠ€ 생성
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);

λ°μ΄ν„°λ² μ΄μŠ€ μ—°κ²° URL

λ°μ΄ν„°λ² μ΄μŠ€ μ—°κ²° URL κ°œλ…μ— μ΅μˆ™ν•˜μ§€ μ•Šμ€ 뢄듀을 μœ„ν•΄ μ„€λͺ…λ“œλ¦½λ‹ˆλ‹€.

postgresql://alex:AbC123dEf@ep-cool-darkness-123456.us-east-2.aws.neon.tech/dbname
             β””β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”˜
              ʌ    ʌ          ʌ                                              ʌ
        μ—­ν•  -β”‚    β”‚          β”‚- 호슀트λͺ…                                    β”‚- λ°μ΄ν„°λ² μ΄μŠ€
                   β”‚
                   β”‚- λΉ„λ°€λ²ˆν˜Έ

μœ„ μ˜ˆμ œλŠ” PostgreSQL λ°μ΄ν„°λ² μ΄μŠ€ μ—°κ²° URL을 λ³΄μ—¬μ€λ‹ˆλ‹€. 각 뢀뢄은 λ‹€μŒκ³Ό 같은 의미λ₯Ό κ°€μ§‘λ‹ˆλ‹€:

이 ν˜•μ‹μ€ λ‹€μ–‘ν•œ λ°μ΄ν„°λ² μ΄μŠ€ μ‹œμŠ€ν…œμ—μ„œ μœ μ‚¬ν•˜κ²Œ μ‚¬μš©λ©λ‹ˆλ‹€. μ—¬λŸ¬λΆ„μ΄ μ‚¬μš©ν•˜λŠ” λ°μ΄ν„°λ² μ΄μŠ€μ— 맞게 URL을 κ΅¬μ„±ν•˜λ©΄ λ©λ‹ˆλ‹€.

λ‹€μŒ 단계

각 λ“œλΌμ΄λ²„λ³„ λ¬Έμ„œλ₯Ό 확인해 λ³΄μ„Έμš”.

MySQL λ“œλΌμ΄λ²„

MySQL PlanetsScale TiDB

SQLite λ“œλΌμ΄λ²„

SQLite Turso Cloudflare D1 Bun SQLite

λ„€μ΄ν‹°λΈŒ SQLite

Expo SQLite OP SQLite React Native SQLite

기타

Drizzle Proxy