Drizzle <> Turso

This guide assumes familiarity with:

**곡식 μ›Ήμ‚¬μ΄νŠΈ**에 λ”°λ₯΄λ©΄, TursoλŠ” libSQL 기반의 에지 SQLite λ°μ΄ν„°λ² μ΄μŠ€ μ„œλΉ„μŠ€μž…λ‹ˆλ‹€.

Drizzle ORM은 libSQL λ“œλΌμ΄λ²„λ₯Ό 기본적으둜 μ§€μ›ν•˜λ©°, SQL λ°©μ–Έκ³Ό 방언별 λ“œλΌμ΄λ²„ 및 문법을 μˆ˜μš©ν•©λ‹ˆλ‹€. λ˜ν•œ SQLite와 μœ μ‚¬ν•œ all, get, values, run 쿼리 λ©”μ„œλ“œ 문법을 λŒ€λΆ€λΆ„ λ°˜μ˜ν•©λ‹ˆλ‹€.

1단계 - νŒ¨ν‚€μ§€ μ„€μΉ˜

npm
yarn
pnpm
bun
npm i drizzle-orm @libsql/client
npm i -D drizzle-kit

μœ„ λͺ…λ Ήμ–΄λ₯Ό μ‚¬μš©ν•˜μ—¬ ν•„μš”ν•œ νŒ¨ν‚€μ§€λ₯Ό μ„€μΉ˜ν•©λ‹ˆλ‹€. drizzle-ormκ³Ό @libsql/clientλŠ” ν”„λ‘œμ νŠΈμ— ν•„μˆ˜μ μΈ νŒ¨ν‚€μ§€μ΄λ©°, drizzle-kit은 개발 ν™˜κ²½μ—μ„œλ§Œ μ‚¬μš©ν•˜λŠ” 개발 μ˜μ‘΄μ„± νŒ¨ν‚€μ§€μž…λ‹ˆλ‹€.

2단계 - λ“œλΌμ΄λ²„ μ΄ˆκΈ°ν™”

Drizzle은 λͺ¨λ“  @libsql/client λ“œλΌμ΄λ²„ λ³€ν˜•μ„ 기본적으둜 μ§€μ›ν•©λ‹ˆλ‹€.

@libsql/client기본적으둜 node λͺ¨λ“ˆλ‘œ μž„ν¬νŠΈλ˜λ©°, λ²ˆλ“€λŸ¬μ—μ„œ targetμ΄λ‚˜ platform이 μ„€μ •λ˜λ©΄ μžλ™μœΌλ‘œ web λͺ¨λ“ˆλ‘œ λ³€κ²½λ©λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄, esbuild --platform=browser와 같은 섀정이 μ μš©λ©λ‹ˆλ‹€.
@libsql/client/nodenode ν˜Έν™˜ λͺ¨λ“ˆλ‘œ, :memory:, file, wss, http, turso μ—°κ²° ν”„λ‘œν† μ½œμ„ μ§€μ›ν•©λ‹ˆλ‹€.
@libsql/client/webnext, nuxt, astro와 같은 ν’€μŠ€νƒ μ›Ή ν”„λ ˆμž„μ›Œν¬λ₯Ό μœ„ν•œ λͺ¨λ“ˆμž…λ‹ˆλ‹€.
@libsql/client/httphttp 및 https μ—°κ²° ν”„λ‘œν† μ½œμ„ μœ„ν•œ λͺ¨λ“ˆμž…λ‹ˆλ‹€.
@libsql/client/wsws 및 wss μ—°κ²° ν”„λ‘œν† μ½œμ„ μœ„ν•œ λͺ¨λ“ˆμž…λ‹ˆλ‹€.
@libsql/client/sqlite3:memory: 및 file μ—°κ²° ν”„λ‘œν† μ½œμ„ μœ„ν•œ λͺ¨λ“ˆμž…λ‹ˆλ‹€.
@libsql/client-wasmWASM을 μœ„ν•œ λ³„λ„μ˜ μ‹€ν—˜μ  νŒ¨ν‚€μ§€μž…λ‹ˆλ‹€.

κΈ°λ³Έ
Node
μ›Ή
HTTP
μ›Ή μ†ŒμΌ“
WASM
import { drizzle } from 'drizzle-orm/libsql';

const db = drizzle({ connection: {
  url: process.env.DATABASE_URL, 
  authToken: process.env.DATABASE_AUTH_TOKEN 
}});

κΈ°μ‘΄ λ“œλΌμ΄λ²„λ₯Ό μ œκ³΅ν•΄μ•Ό ν•˜λŠ” 경우:

default
web
import { drizzle } from 'drizzle-orm/libsql';
import { createClient } from '@libsql/client';

const client = createClient({ 
  url: process.env.DATABASE_URL,
  authToken: process.env.DATABASE_AUTH_TOKEN
});

const db = drizzle({ client });

const result = await db.select().from(users).all()

3단계 - 쿼리 λ§Œλ“€κΈ°

import { drizzle } from 'drizzle-orm/libsql';
import * as s from 'drizzle-orm/sqlite-core';

// λ°μ΄ν„°λ² μ΄μŠ€ μ—°κ²° μ„€μ •
const db = drizzle({ 
  connection: {
    url: process.env.DATABASE_URL, 
    authToken: process.env.DATABASE_AUTH_TOKEN 
  }
});

// users ν…Œμ΄λΈ” μ •μ˜
const users = s.sqliteTable("users", {
  id: s.integer(),
  name: s.text(),
});

// users ν…Œμ΄λΈ”μ—μ„œ 데이터 쑰회
const result = await db.select().from(users);

λ‹€μŒ λ‹¨κ³„λŠ” λ¬΄μ—‡μΈκ°€μš”?