Database Setup - Cloudflare D1 โ
Panduan setup database Cloudflare D1 (SQLite).
๐ Overview โ
Cloudflare D1 adalah database SQLite yang berjalan di edge network Cloudflare.
Keuntungan:
- ๐ Global - Data direplikasi di 300+ lokasi
- ๐ Cepat - Query di edge terdekat dengan user
- ๐ฐ Murah - 500MB storage gratis
- ๐ฑ Edge-compatible - Works dengan Cloudflare Workers/Pages
๐ Setup Database โ
1. Create Database โ
bash
# Login ke Wrangler (jika belum login)
npx wrangler login
# Create database
npx wrangler d1 create DBOutput contoh:
โ
Successfully created DB 'DB' in region APAC
[[d1_databases]]
binding = "DB"
database_name = "DB"
database_id = "a1b2c3d4-e5f6-7890-abcd-ef1234567890"2. Update wrangler.toml โ
Copy output di atas ke wrangler.toml:
toml
name = "my-app"
compatibility_date = "2024-01-01"
[[d1_databases]]
binding = "DB"
database_name = "DB"
database_id = "a1b2c3d4-e5f6-7890-abcd-ef1234567890" # Ganti dengan ID Anda3. Apply Migrations โ
bash
# Untuk local development
npm run db:migrate:local
# Untuk production
npm run db:migrate4. (Optional) Seed Data โ
bash
# Seed database dengan data awal
npm run db:seed:local๐๏ธ Database Schema โ
Tables โ
users
sql
- id (TEXT PRIMARY KEY) - UUID
- email (TEXT UNIQUE)
- name (TEXT)
- password_hash (TEXT, nullable)
- provider ('email' | 'google')
- google_id (TEXT, nullable)
- avatar (TEXT, nullable)
- email_verified (BOOLEAN)
- created_at (TIMESTAMP)
- updated_at (TIMESTAMP)sessions (Lucia Auth)
sql
- id (TEXT PRIMARY KEY)
- user_id (TEXT FOREIGN KEY)
- expires_at (TIMESTAMP)password_reset_tokens
sql
- id (TEXT PRIMARY KEY)
- user_id (TEXT FOREIGN KEY)
- token_hash (TEXT)
- expires_at (TIMESTAMP)
- used (BOOLEAN)email_verification_tokens
sql
- id (TEXT PRIMARY KEY)
- user_id (TEXT FOREIGN KEY)
- token_hash (TEXT)
- expires_at (TIMESTAMP)
- used (BOOLEAN)posts (Example)
sql
- id (INTEGER PRIMARY KEY AUTOINCREMENT)
- title (TEXT)
- content (TEXT)
- published (BOOLEAN)
- author_id (TEXT FOREIGN KEY)
- created_at (TIMESTAMP)๐ ๏ธ Database Commands โ
bash
# Generate migration dari schema changes
npm run db:generate
# Apply migrations
npm run db:migrate # Production
npm run db:migrate:local # Local development
# Open Drizzle Studio (GUI)
npm run db:studio
# Execute SQL
npx wrangler d1 execute DB --local --command "SELECT * FROM users"๐ Drizzle Studio โ
GUI untuk manage database:
bash
npm run db:studioBuka http://local.drizzle.studio
Fitur:
- ๐ View all tables
- ๐ Query data
- โ Insert/update/delete rows
- ๐ Schema visualization
๐งช Testing Database โ
Check Database Health โ
bash
curl http://localhost:5173/api/healthResponse:
json
{
"status": "ok",
"db": "connected",
"timestamp": "2024-01-..."
}Query via API โ
bash
# List users
curl http://localhost:5173/api/users
# Create user
curl -X POST http://localhost:5173/api/users \
-H "Content-Type: application/json" \
-d '{"email":"test@test.com","name":"Test"}'๐ง Advanced โ
Manual SQL Execution โ
bash
# Local
npx wrangler d1 execute DB --local --file=./drizzle/custom.sql
# Production
npx wrangler d1 execute DB --remote --file=./drizzle/custom.sqlBackup Database โ
bash
# Export data
npx wrangler d1 export DB --local --output=./backup.sql
# Import data
npx wrangler d1 execute DB --local --file=./backup.sqlDelete Database โ
bash
npx wrangler d1 delete DB๐ฐ Pricing โ
| Usage | Free Tier | Paid |
|---|---|---|
| Storage | 500 MB | $0.75/GB-month |
| Read requests | 5 million/day | - |
| Write requests | 100,000/day | - |
๐ Troubleshooting โ
| Error | Solusi |
|---|---|
| "D1 binding not found" | Check wrangler.toml database_id |
| "Database does not exist" | Pastikan database sudah dibuat |
| "Migration failed" | Check SQL syntax di drizzle/ folder |
| "Permission denied" | Check API token punya permission D1:Edit |