Skills for Claude Code CLI such as full stack dev Cloudflare, React, Tailwind v4, and AI integrations.
Python aianthropicautomationclaude-codeclaude-skillscloudflaredevtoolsproductivityreactskillstailwindvite
Complete cloudflare-d1 skill
Jez0b15ae31daf2
diff --git a/ATOMIC-SKILLS-SUMMARY.md b/ATOMIC-SKILLS-SUMMARY.mdindex 4ee600f7..057886ae 100644--- a/ATOMIC-SKILLS-SUMMARY.md+++ b/ATOMIC-SKILLS-SUMMARY.md@@ -37,20 +37,20 @@ We pivoted from **bundled composite skills** to **atomic, domain-focused skills*## Current Status-### ✅ Completed Skills (3)+### ✅ Completed Skills (4)- `tailwind-v4-shadcn` - Tailwind v4 + shadcn/ui (6h dev, ~70% token savings)- `cloudflare-worker-base` - Hono + Vite + wrangler (2h dev, ~60% token savings)- `firecrawl-scraper` - Firecrawl v2 API for web scraping (1.5h dev, ~60% token savings)+- `cloudflare-d1` - D1 serverless database (2.5h dev, ~58% token savings)-### 📋 Planned Atomic Skills (11)+### 📋 Planned Atomic Skills (10)-**Batch 1 - Cloudflare Services (6 skills)**-1. `cloudflare-d1` - D1 database (migrations, queries, bindings)-2. `cloudflare-r2` - R2 storage (uploads, CORS, presigned URLs)-3. `cloudflare-kv` - KV namespaces (caching patterns)-4. `cloudflare-workers-ai` - Workers AI (models, streaming, embeddings)-5. `cloudflare-vectorize` - Vector search (embeddings, RAG)-6. `cloudflare-queues` - Async processing (batch jobs, retries)+**Batch 1 - Cloudflare Services (5 skills remaining)**+1. `cloudflare-r2` - R2 object storage (uploads, CORS, presigned URLs)+2. `cloudflare-kv` - KV namespaces (caching patterns)+3. `cloudflare-workers-ai` - Workers AI (models, streaming, embeddings)+4. `cloudflare-vectorize` - Vector search (embeddings, RAG)+5. `cloudflare-queues` - Async processing (batch jobs, retries)**Batch 2 - Auth & Frameworks (3 skills)**7. `clerk-auth` - All Clerk knowledge (React, Next.js, CF Workers, JWT)diff --git a/planning/skills-roadmap.md b/planning/skills-roadmap.mdindex 4ffb4bed..c84f7f13 100644--- a/planning/skills-roadmap.md+++ b/planning/skills-roadmap.md@@ -117,29 +117,32 @@ Result: Integrated solution from atomic skills----### Batch 1 - Cloudflare Services (Week 1) ⭐⭐⭐--#### 1. cloudflare-d1-**Status**: Planned+#### cloudflare-d1+**Status**: ✅ Complete (2025-10-21)**Priority**: Critical**Dependencies**: cloudflare-worker-base-**Estimated Dev Time**: 4 hours-**Token Savings**: ~55%+**Actual Dev Time**: 2.5 hours+**Token Savings**: ~58%+**Errors Prevented**: 6**What It Does**:-- D1 database setup + migrations-- wrangler bindings configuration-- SQL schema patterns-- Query patterns and best practices-- Drizzle ORM integration (optional)-- Seeding and backup strategies+- D1 database creation and setup with wrangler+- SQL migrations system (create, list, apply)+- D1 bindings configuration in wrangler.jsonc+- Complete D1 Workers API (prepare, bind, batch, exec)+- Prepared statements for SQL injection prevention+- Batch queries for performance optimization+- Indexes and query optimization patterns+- Error handling and retry strategies+- Production-ready schema examples+- Local and remote development workflows-**Auto-Trigger Keywords**:-- `d1 database`, `cloudflare d1`, `d1 migrations`-- `d1 schema`, `sqlite cloudflare`, `sql workers`+**Production Validated**: Templates tested with working examples---+### Batch 1 - Cloudflare Services (Week 1) ⭐⭐⭐+#### 2. cloudflare-r2**Status**: Planned**Priority**: High@@ -473,8 +476,8 @@ Calculate:## 📈 Progress Tracking### Overall Progress:-- **Completed**: 3 skills ✅-- **Batch 1 - Cloudflare Services**: 0/6 complete+- **Completed**: 4 skills ✅+- **Batch 1 - Cloudflare Services**: 1/6 complete- **Batch 2 - Auth & Frameworks**: 0/3 complete- **Batch 3 - Data & Utilities**: 0/2 complete@@ -485,7 +488,7 @@ Calculate:| **tailwind-v4-shadcn** | **✅ Complete** | **6h** | **~70%** | **3** | High || **cloudflare-worker-base** | **✅ Complete** | **2h** | **~60%** | **6** | Critical || **firecrawl-scraper** | **✅ Complete** | **1.5h** | **~60%** | **6** | Medium |-| cloudflare-d1 | Planned | 4h (est.) | ~55% | - | Critical |+| **cloudflare-d1** | **✅ Complete** | **2.5h** | **~58%** | **6** | Critical || cloudflare-r2 | Planned | 4h (est.) | ~55% | - | High || cloudflare-kv | Planned | 3h (est.) | ~50% | - | High || cloudflare-workers-ai | Planned | 5h (est.) | ~60% | - | High |@@ -497,7 +500,7 @@ Calculate:| tanstack-query | Planned | 4h (est.) | ~55% | - | Medium || drizzle-orm-d1 | Planned | 5h (est.) | ~60% | - | Low |-**Total Skills Planned**: 13 (3 complete, 10 to build)+**Total Skills Planned**: 13 (4 complete, 9 to build)---diff --git a/skills/cloudflare-d1/README.md b/skills/cloudflare-d1/README.mdnew file mode 100644index 00000000..12eddb75--- /dev/null+++ b/skills/cloudflare-d1/README.md@@ -0,0 +1,187 @@+# Cloudflare D1 Database Skill++**Auto-Discovery Skill for Claude Code CLI**++Complete knowledge domain for working with Cloudflare D1 - serverless SQLite database on Cloudflare's edge network.++---++## Auto-Trigger Keywords++Claude will automatically suggest this skill when you mention any of these keywords:++### Primary Triggers (Technologies)+- d1+- d1 database+- cloudflare d1+- wrangler d1+- d1 migrations+- d1 bindings+- sqlite workers+- serverless database cloudflare+- edge database+- d1 queries+- sql cloudflare++### Secondary Triggers (Commands & Patterns)+- wrangler d1 create+- wrangler d1 migrations+- prepared statements d1+- batch queries d1+- d1 api+- d1 schema+- d1 indexes+- sqlite cloudflare workers+- sql workers api+- database bindings cloudflare++### Error-Based Triggers+- "D1_ERROR"+- "D1_EXEC_ERROR"+- "D1_TYPE_ERROR"+- "D1_COLUMN_NOTFOUND"+- "statement too long"+- "too many requests queued"+- "DB's isolate exceeded its memory limit"+- "DB exceeded its CPU time limit"+- "Network connection lost"+- "cannot start a transaction within a transaction"++---++## What This Skill Does++- ✅ Creates D1 databases with wrangler CLI+- ✅ Manages SQL migrations (create, list, apply)+- ✅ Configures D1 bindings in wrangler.jsonc+- ✅ Writes type-safe D1 queries in Workers+- ✅ Handles prepared statements with parameter binding+- ✅ Optimizes performance with batch queries+- ✅ Creates indexes for faster queries+- ✅ Implements error handling and retry logic+- ✅ Provides CRUD operation patterns+- ✅ Supports local and remote development workflows++---++## Known Issues Prevented++| Issue | Error Message | How Skill Prevents |+|-------|---------------|-------------------|+| **SQL Injection** | - | Always uses prepared statements with `.bind()` |+| **Statement Too Long** | "statement too long" | Templates show batching inserts (100-250 rows) |+| **Transaction Conflicts** | "cannot start a transaction within a transaction" | Migrations don't include BEGIN TRANSACTION |+| **Type Mismatch** | "D1_TYPE_ERROR" | Uses `null` instead of `undefined` for optional values |+| **Rate Limiting** | "too many requests queued" | Uses `.batch()` instead of individual queries |+| **Memory Exceeded** | "DB's isolate exceeded its memory limit" | Adds LIMIT clauses and pagination patterns |+| **Foreign Key Violations** | "foreign key constraint failed" | Uses `PRAGMA defer_foreign_keys = true` |+| **Index Not Used** | Slow queries | Templates include EXPLAIN QUERY PLAN examples |++---++## Token Efficiency++### Manual D1 Setup (Without Skill):+- Create database: 800 tokens+- Configure bindings: 600 tokens+- Write migrations: 1,200 tokens+- Implement queries: 2,000 tokens+- Add error handling: 800 tokens+- Create indexes: 600 tokens+- **Total: ~6,000 tokens**++### With cloudflare-d1 Skill:+- Reference skill templates: 1,500 tokens+- Customize for your use case: 1,000 tokens+- **Total: ~2,500 tokens**++**Savings: ~58% token reduction** (3,500 tokens saved)++---++## When to Use This Skill++### ✅ Use When:+- Building Cloudflare Workers that need relational data+- Storing user data, content, or application state+- Need SQL queries with JOINs and complex filtering+- Migrating from MySQL/PostgreSQL to edge database+- Building multi-tenant applications (per-user databases)+- Need ACID transactions and data consistency++### ❌ Don't Use When:+- Simple key-value storage needed → Use KV instead+- Large file/blob storage → Use R2 instead+- Real-time collaborative state → Use Durable Objects instead+- Need MySQL/PostgreSQL compatibility → Use Hyperdrive+- Embedding/vector search → Use Vectorize instead++---++## Quick Usage Example++```bash+# Create database+npx wrangler d1 create my-app-db++# Create migration+npx wrangler d1 migrations create my-app-db create_users_table++# Apply migration+npx wrangler d1 migrations apply my-app-db --local+```++```typescript+// Query from Worker+const user = await env.DB.prepare('SELECT * FROM users WHERE email = ?')+ .bind('[email protected]')+ .first();+```++---++## File Structure++```+~/.claude/skills/cloudflare-d1/+├── SKILL.md # Complete D1 documentation+├── README.md # This file (auto-trigger keywords)+├── templates/+│ ├── d1-setup-migration.sh # Complete workflow script+│ ├── schema-example.sql # Production-ready schema+│ └── d1-worker-queries.ts # TypeScript query examples+└── reference/+ ├── query-patterns.md # All D1 API methods+ └── best-practices.md # Performance & security tips+```++---++## Dependencies++- **Required**: cloudflare-worker-base skill (for Worker setup)+- **CLI**: [email protected]++- **Types**: @cloudflare/[email protected]+++---++## Related Skills++- `cloudflare-worker-base` - Base Worker + Hono setup+- `cloudflare-kv` - Key-value storage (simpler than D1)+- `cloudflare-r2` - Object storage for files+- `hono-routing` - Advanced Hono patterns++---++## Learn More++- **SKILL.md**: Complete D1 documentation with examples+- **templates/**: Working code templates for common patterns+- **reference/**: Deep-dive guides for query optimization++---++**Status**: Production Ready ✅+**Last Updated**: 2025-10-21+**Maintainer**: Jeremy Dawes (Jezweb)diff --git a/skills/cloudflare-d1/SKILL.md b/skills/cloudflare-d1/SKILL.mdnew file mode 100644index 00000000..94de3045--- /dev/null+++ b/skills/cloudflare-d1/SKILL.md@@ -0,0 +1,893 @@+---+name: Cloudflare D1 Database+description: |+ Complete knowledge domain for Cloudflare D1 - serverless SQLite database on Cloudflare's edge network.++ Use when: creating D1 databases, writing SQL migrations, configuring D1 bindings, querying D1 from Workers,+ handling SQLite data, building relational data models, or encountering "D1_ERROR", "statement too long",+ "too many requests queued", migration failures, or query performance issues.++ Keywords: d1, d1 database, cloudflare d1, wrangler d1, d1 migrations, d1 bindings, sqlite workers,+ serverless database, edge database, d1 queries, sql cloudflare, prepared statements, batch queries,+ d1 api, wrangler migrations, D1_ERROR, D1_EXEC_ERROR, statement too long, database bindings,+ sqlite cloudflare, sql workers api, d1 indexes, query optimization, d1 schema+---++# Cloudflare D1 Database++**Status**: Production Ready ✅+**Last Updated**: 2025-10-21+**Dependencies**: cloudflare-worker-base (for Worker setup)+**Latest Versions**: [email protected], @cloudflare/[email protected]++---++## Quick Start (5 Minutes)++### 1. Create D1 Database++```bash+# Create a new D1 database+npx wrangler d1 create my-database++# Output includes database_id - save this!+# ✅ Successfully created DB 'my-database'+#+# [[d1_databases]]+# binding = "DB"+# database_name = "my-database"+# database_id = "<UUID>"+```++### 2. Configure Bindings++Add to your `wrangler.jsonc`:++```jsonc+{+ "name": "my-worker",+ "main": "src/index.ts",+ "compatibility_date": "2025-10-11",+ "d1_databases": [+ {+ "binding": "DB", // Available as env.DB in your Worker+ "database_name": "my-database", // Name from wrangler d1 create+ "database_id": "<UUID>", // ID from wrangler d1 create+ "preview_database_id": "local-db" // For local development+ }+ ]+}+```++**CRITICAL:**+- `binding` is how you access the database in code (`env.DB`)+- `database_id` is the production database UUID+- `preview_database_id` is for local dev (can be any string)+- **Never commit real `database_id` values to public repos** - use environment variables or secrets++### 3. Create Your First Migration++```bash+# Create migration file+npx wrangler d1 migrations create my-database create_users_table++# This creates: migrations/0001_create_users_table.sql+```++Edit the migration file:++```sql+-- migrations/0001_create_users_table.sql+DROP TABLE IF EXISTS users;+CREATE TABLE IF NOT EXISTS users (+ user_id INTEGER PRIMARY KEY AUTOINCREMENT,+ email TEXT NOT NULL UNIQUE,+ username TEXT NOT NULL,+ created_at INTEGER NOT NULL,+ updated_at INTEGER+);++-- Create index for common queries+CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);++-- Optimize database+PRAGMA optimize;+```++### 4. Apply Migration++```bash+# Apply locally first (for testing)+npx wrangler d1 migrations apply my-database --local++# Apply to production when ready+npx wrangler d1 migrations apply my-database --remote+```++### 5. Query from Your Worker++```typescript+// src/index.ts+import { Hono } from 'hono';++type Bindings = {+ DB: D1Database;+};++const app = new Hono<{ Bindings: Bindings }>();++app.get('/api/users/:email', async (c) => {+ const email = c.req.param('email');++ try {+ // ALWAYS use prepared statements with bind()+ const result = await c.env.DB.prepare(+ 'SELECT * FROM users WHERE email = ?'+ )+ .bind(email)+ .first();++ if (!result) {+ return c.json({ error: 'User not found' }, 404);+ }++ return c.json(result);+ } catch (error: any) {+ console.error('D1 Error:', error.message);+ return c.json({ error: 'Database error' }, 500);+ }+});++export default app;+```++---++## D1 Migrations System++### Migration Workflow++```bash+# 1. Create migration+npx wrangler d1 migrations create <DATABASE_NAME> <MIGRATION_NAME>++# 2. List unapplied migrations+npx wrangler d1 migrations list <DATABASE_NAME> --local+npx wrangler d1 migrations list <DATABASE_NAME> --remote++# 3. Apply migrations+npx wrangler d1 migrations apply <DATABASE_NAME> --local # Test locally+npx wrangler d1 migrations apply <DATABASE_NAME> --remote # Deploy to production+```++### Migration File Naming++Migrations are automatically versioned:++```+migrations/+├── 0000_initial_schema.sql+├── 0001_add_users_table.sql+├── 0002_add_posts_table.sql+└── 0003_add_indexes.sql+```++**Rules:**+- Files are executed in sequential order+- Each migration runs once (tracked in `d1_migrations` table)+- Failed migrations roll back (transactional)+- Can't modify or delete applied migrations++### Custom Migration Configuration++```jsonc+{+ "d1_databases": [+ {+ "binding": "DB",+ "database_name": "my-database",+ "database_id": "<UUID>",+ "migrations_dir": "db/migrations", // Custom directory (default: migrations/)+ "migrations_table": "schema_migrations" // Custom tracking table (default: d1_migrations)+ }+ ]+}+```++### Migration Best Practices++#### ✅ Always Do:++```sql+-- Use IF NOT EXISTS to make migrations idempotent+CREATE TABLE IF NOT EXISTS users (...);+CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);++-- Run PRAGMA optimize after schema changes+PRAGMA optimize;++-- Use transactions for data migrations+BEGIN TRANSACTION;+UPDATE users SET updated_at = unixepoch() WHERE updated_at IS NULL;+COMMIT;+```++#### ❌ Never Do:++```sql+-- DON'T include BEGIN TRANSACTION at start (D1 handles this)+BEGIN TRANSACTION; -- ❌ Remove this++-- DON'T use MySQL/PostgreSQL syntax+ALTER TABLE users MODIFY COLUMN email VARCHAR(255); -- ❌ Not SQLite++-- DON'T create tables without IF NOT EXISTS+CREATE TABLE users (...); -- ❌ Fails if table exists+```++### Handling Foreign Keys in Migrations++```sql+-- Temporarily disable foreign key checks during schema changes+PRAGMA defer_foreign_keys = true;++-- Make schema changes that would violate foreign keys+ALTER TABLE posts DROP COLUMN author_id;+ALTER TABLE posts ADD COLUMN user_id INTEGER REFERENCES users(user_id);++-- Foreign keys re-enabled automatically at end of migration+```++---++## D1 Workers API++### Type Definitions++```typescript+// Add to env.d.ts or worker-configuration.d.ts+interface Env {+ DB: D1Database;+ // ... other bindings+}++// For Hono+type Bindings = {+ DB: D1Database;+};++const app = new Hono<{ Bindings: Bindings }>();+```++### prepare() - Prepared Statements (PRIMARY METHOD)++**Always use prepared statements for queries with user input.**++```typescript+// Basic prepared statement+const stmt = env.DB.prepare('SELECT * FROM users WHERE user_id = ?');+const bound = stmt.bind(userId);+const result = await bound.first();++// Chained (most common pattern)+const user = await env.DB.prepare('SELECT * FROM users WHERE email = ?')+ .bind(email)+ .first();+```++**Why use prepare():**+- ✅ Prevents SQL injection+- ✅ Can be reused with different parameters+- ✅ Better performance (query plan caching)+- ✅ Type-safe with TypeScript++### Query Result Methods++#### .all() - Get All Rows++```typescript+const { results, meta } = await env.DB.prepare(+ 'SELECT * FROM users WHERE created_at > ?'+)+.bind(timestamp)+.all();++console.log(results); // Array of rows+console.log(meta); // { duration, rows_read, rows_written }+```++#### .first() - Get First Row++```typescript+// Returns first row or null+const user = await env.DB.prepare('SELECT * FROM users WHERE email = ?')+ .bind('[email protected]')+ .first();++if (!user) {+ return c.json({ error: 'Not found' }, 404);+}+```++#### .first(column) - Get Single Column Value++```typescript+// Returns the value of a specific column from first row+const count = await env.DB.prepare('SELECT COUNT(*) as total FROM users')+ .first('total');++console.log(count); // 42 (just the number, not an object)+```++#### .run() - Execute Without Results++```typescript+// For INSERT, UPDATE, DELETE+const { success, meta } = await env.DB.prepare(+ 'INSERT INTO users (email, username, created_at) VALUES (?, ?, ?)'+)+.bind(email, username, Date.now())+.run();++console.log(meta); // { duration, rows_read, rows_written, last_row_id }+```++### batch() - Execute Multiple Queries++**CRITICAL FOR PERFORMANCE**: Use batch() to reduce latency.++```typescript+// Prepare multiple statements+const stmt1 = env.DB.prepare('SELECT * FROM users WHERE user_id = ?').bind(1);+const stmt2 = env.DB.prepare('SELECT * FROM users WHERE user_id = ?').bind(2);+const stmt3 = env.DB.prepare('SELECT * FROM posts WHERE user_id = ?').bind(1);++// Execute all in one round trip+const results = await env.DB.batch([stmt1, stmt2, stmt3]);++console.log(results[0].results); // Users query 1+console.log(results[1].results); // Users query 2+console.log(results[2].results); // Posts query+```++**Batch Behavior:**+- Executes sequentially (in order)+- Each statement commits individually (auto-commit mode)+- If one fails, remaining statements don't execute+- Much faster than individual queries (single network round trip)++**Batch Use Cases:**+```typescript+// ✅ Insert multiple rows efficiently+const inserts = users.map(user =>+ env.DB.prepare('INSERT INTO users (email, username) VALUES (?, ?)')+ .bind(user.email, user.username)+);+await env.DB.batch(inserts);++// ✅ Fetch related data in parallel+const [user, posts, comments] = await env.DB.batch([+ env.DB.prepare('SELECT * FROM users WHERE user_id = ?').bind(userId),+ env.DB.prepare('SELECT * FROM posts WHERE user_id = ?').bind(userId),+ env.DB.prepare('SELECT * FROM comments WHERE user_id = ?').bind(userId)+]);+```++### exec() - Execute Raw SQL (AVOID IN PRODUCTION)++```typescript+// Only for migrations, maintenance, and one-off tasks+const result = await env.DB.exec(`+ SELECT * FROM users;+ SELECT * FROM posts;+`);++console.log(result); // { count: 2, duration: 5 }+```++**NEVER use exec() for:**+- ❌ Queries with user input (SQL injection risk)+- ❌ Production queries (poor performance)+- ❌ Queries that need results (exec doesn't return data)++**ONLY use exec() for:**+- ✅ Running migration SQL files locally+- ✅ One-off maintenance tasks+- ✅ Database initialization scripts++---++## Query Patterns++### Basic CRUD Operations++#### Create (INSERT)++```typescript+// Single insert+const { meta } = await env.DB.prepare(+ 'INSERT INTO users (email, username, created_at) VALUES (?, ?, ?)'+)+.bind(email, username, Date.now())+.run();++const newUserId = meta.last_row_id;++// Bulk insert with batch()+const users = [+ { email: '[email protected]', username: 'user1' },+ { email: '[email protected]', username: 'user2' }+];++const inserts = users.map(u =>+ env.DB.prepare('INSERT INTO users (email, username, created_at) VALUES (?, ?, ?)')+ .bind(u.email, u.username, Date.now())+);++await env.DB.batch(inserts);+```++#### Read (SELECT)++```typescript+// Single row+const user = await env.DB.prepare('SELECT * FROM users WHERE user_id = ?')+ .bind(userId)+ .first();++// Multiple rows+const { results } = await env.DB.prepare(+ 'SELECT * FROM users WHERE created_at > ? ORDER BY created_at DESC LIMIT ?'+)+.bind(timestamp, 10)+.all();++// Count+const count = await env.DB.prepare('SELECT COUNT(*) as total FROM users')+ .first('total');++// Exists check+const exists = await env.DB.prepare('SELECT 1 FROM users WHERE email = ? LIMIT 1')+ .bind(email)+ .first();++if (exists) {+ // Email already registered+}+```++#### Update (UPDATE)++```typescript+const { meta } = await env.DB.prepare(+ 'UPDATE users SET username = ?, updated_at = ? WHERE user_id = ?'+)+.bind(newUsername, Date.now(), userId)+.run();++const rowsAffected = meta.rows_written;++if (rowsAffected === 0) {+ // User not found+}+```++#### Delete (DELETE)++```typescript+const { meta } = await env.DB.prepare('DELETE FROM users WHERE user_id = ?')+ .bind(userId)+ .run();++const rowsDeleted = meta.rows_written;+```++### Advanced Queries++#### Pagination++```typescript+app.get('/api/users', async (c) => {+ const page = parseInt(c.req.query('page') || '1');+ const limit = parseInt(c.req.query('limit') || '20');+ const offset = (page - 1) * limit;++ const [countResult, usersResult] = await c.env.DB.batch([+ c.env.DB.prepare('SELECT COUNT(*) as total FROM users'),+ c.env.DB.prepare('SELECT * FROM users ORDER BY created_at DESC LIMIT ? OFFSET ?')+ .bind(limit, offset)+ ]);++ const total = countResult.results[0].total as number;+ const users = usersResult.results;++ return c.json({+ users,+ pagination: {+ page,+ limit,+ total,+ pages: Math.ceil(total / limit)+ }+ });+});+```++#### Joins++```typescript+const { results } = await env.DB.prepare(`+ SELECT+ posts.*,+ users.username as author_name,+ users.email as author_email+ FROM posts+ INNER JOIN users ON posts.user_id = users.user_id+ WHERE posts.published = ?+ ORDER BY posts.created_at DESC+ LIMIT ?+`)+.bind(1, 10)+.all();+```++#### Transactions (Batch Pattern)++D1 doesn't support multi-statement transactions, but batch() provides sequential execution:++```typescript+// Transfer credits between users (pseudo-transaction)+await env.DB.batch([+ env.DB.prepare('UPDATE users SET credits = credits - ? WHERE user_id = ?')+ .bind(amount, fromUserId),+ env.DB.prepare('UPDATE users SET credits = credits + ? WHERE user_id = ?')+ .bind(amount, toUserId),+ env.DB.prepare('INSERT INTO transactions (from_user, to_user, amount) VALUES (?, ?, ?)')+ .bind(fromUserId, toUserId, amount)+]);+```++**Note**: If any statement fails, the batch stops. This provides some transaction-like behavior.++---++## Error Handling++### Error Types++```typescript+try {+ const result = await env.DB.prepare('SELECT * FROM users WHERE user_id = ?')+ .bind(userId)+ .first();+} catch (error: any) {+ // D1 errors have a message property+ const errorMessage = error.message;++ if (errorMessage.includes('D1_ERROR')) {+ // D1-specific error+ } else if (errorMessage.includes('D1_EXEC_ERROR')) {+ // SQL syntax error+ } else if (errorMessage.includes('D1_TYPE_ERROR')) {+ // Type mismatch (e.g., undefined instead of null)+ } else if (errorMessage.includes('D1_COLUMN_NOTFOUND')) {+ // Column doesn't exist+ }++ console.error('Database error:', errorMessage);+ return c.json({ error: 'Database operation failed' }, 500);+}+```++### Common Errors and Fixes++#### "Statement too long"++```typescript+// ❌ DON'T: Single massive INSERT+await env.DB.exec(`+ INSERT INTO users (email) VALUES+ ('[email protected]'),+ ('[email protected]'),+ ... // 1000 more rows+`);++// ✅ DO: Break into batches+const batchSize = 100;+for (let i = 0; i < users.length; i += batchSize) {+ const batch = users.slice(i, i + batchSize);+ const inserts = batch.map(u =>+ env.DB.prepare('INSERT INTO users (email) VALUES (?)').bind(u.email)+ );+ await env.DB.batch(inserts);+}+```++#### "Too many requests queued"++```typescript+// ❌ DON'T: Fire off many individual queries+for (const user of users) {+ await env.DB.prepare('INSERT INTO users (email) VALUES (?)').bind(user.email).run();+}++// ✅ DO: Use batch()+const inserts = users.map(u =>+ env.DB.prepare('INSERT INTO users (email) VALUES (?)').bind(u.email)+);+await env.DB.batch(inserts);+```++#### "D1_TYPE_ERROR" (undefined vs null)++```typescript+// ❌ DON'T: Use undefined+await env.DB.prepare('INSERT INTO users (email, bio) VALUES (?, ?)')+ .bind(email, undefined); // ❌ D1 doesn't support undefined++// ✅ DO: Use null for optional values+await env.DB.prepare('INSERT INTO users (email, bio) VALUES (?, ?)')+ .bind(email, bio || null);+```++### Retry Logic++```typescript+async function queryWithRetry<T>(+ queryFn: () => Promise<T>,+ maxRetries = 3+): Promise<T> {+ for (let attempt = 0; attempt < maxRetries; attempt++) {+ try {+ return await queryFn();+ } catch (error: any) {+ const message = error.message;++ // Retry on transient errors+ const isRetryable =+ message.includes('Network connection lost') ||+ message.includes('storage caused object to be reset') ||+ message.includes('reset because its code was updated');++ if (!isRetryable || attempt === maxRetries - 1) {+ throw error;+ }++ // Exponential backoff+ const delay = Math.min(1000 * Math.pow(2, attempt), 5000);+ await new Promise(resolve => setTimeout(resolve, delay));+ }+ }++ throw new Error('Retry logic failed');+}++// Usage+const user = await queryWithRetry(() =>+ env.DB.prepare('SELECT * FROM users WHERE user_id = ?')+ .bind(userId)+ .first()+);+```++---++## Performance Optimization++### Indexes++Indexes dramatically improve query performance for filtered columns.++#### When to Create Indexes++```typescript+// ✅ Index columns used in WHERE clauses+CREATE INDEX idx_users_email ON users(email);++// ✅ Index foreign keys+CREATE INDEX idx_posts_user_id ON posts(user_id);++// ✅ Index columns used for sorting+CREATE INDEX idx_posts_created_at ON posts(created_at DESC);++// ✅ Multi-column indexes for complex queries+CREATE INDEX idx_posts_user_published ON posts(user_id, published);+```++#### Test Index Usage++```sql+-- Check if index is being used+EXPLAIN QUERY PLAN SELECT * FROM users WHERE email = '[email protected]';++-- Should see: SEARCH users USING INDEX idx_users_email+```++#### Partial Indexes++```sql+-- Index only non-deleted records+CREATE INDEX idx_users_active ON users(email) WHERE deleted = 0;++-- Index only published posts+CREATE INDEX idx_posts_published ON posts(created_at DESC) WHERE published = 1;+```++### PRAGMA optimize++Run after creating indexes or making schema changes:++```sql+-- In your migration file+CREATE INDEX idx_users_email ON users(email);+PRAGMA optimize;+```++Or from Worker:++```typescript+await env.DB.exec('PRAGMA optimize');+```++### Query Optimization Tips++```typescript+// ✅ Use specific columns instead of SELECT *+const users = await env.DB.prepare(+ 'SELECT user_id, email, username FROM users'+).all();++// ✅ Use LIMIT to prevent scanning entire table+const latest = await env.DB.prepare(+ 'SELECT * FROM posts ORDER BY created_at DESC LIMIT 10'+).all();++// ✅ Use indexes for WHERE conditions+// Create index first: CREATE INDEX idx_users_email ON users(email)+const user = await env.DB.prepare('SELECT * FROM users WHERE email = ?')+ .bind(email)+ .first();++// ❌ Avoid functions in WHERE (can't use indexes)+// Bad: WHERE LOWER(email) = '[email protected]'+// Good: WHERE email = '[email protected]' (store email lowercase)+```++---++## Local Development++### Local vs Remote Databases++```bash+# Create local database (automatic on first --local command)+npx wrangler d1 migrations apply my-database --local++# Query local database+npx wrangler d1 execute my-database --local --command "SELECT * FROM users"++# Query remote database+npx wrangler d1 execute my-database --remote --command "SELECT * FROM users"+```++### Local Database Location++Local D1 databases are stored in:+```+.wrangler/state/v3/d1/miniflare-D1DatabaseObject/<database_id>.sqlite+```++### Seeding Local Database++```bash+# Create seed file+cat > seed.sql << 'EOF'+INSERT INTO users (email, username, created_at) VALUES+ ('[email protected]', 'alice', 1698000000),+ ('[email protected]', 'bob', 1698000060);+EOF++# Apply seed+npx wrangler d1 execute my-database --local --file=seed.sql+```++---++## Drizzle ORM (Optional)++While D1 works great with raw SQL, some developers prefer ORMs. Drizzle ORM supports D1:++```bash+npm install drizzle-orm+npm install -D drizzle-kit+```++**Note**: Drizzle adds complexity and another layer to learn. For most D1 use cases, **raw SQL with wrangler is simpler and more direct**. Only consider Drizzle if you:+- Prefer TypeScript schema definitions over SQL+- Want auto-complete for queries+- Are building a very large application with complex schemas++**Official Drizzle D1 docs**: https://orm.drizzle.team/docs/get-started-sqlite#cloudflare-d1++---++## Best Practices Summary++### ✅ Always Do:++1. **Use prepared statements** with `.bind()` for user input+2. **Use `.batch()`** for multiple queries (reduces latency)+3. **Create indexes** on frequently queried columns+4. **Run `PRAGMA optimize`** after schema changes+5. **Use `IF NOT EXISTS`** in migrations for idempotency+6. **Test migrations locally** before applying to production+7. **Handle errors gracefully** with try/catch+8. **Use `null`** instead of `undefined` for optional values+9. **Validate input** before binding to queries+10. **Check `meta.rows_written`** after UPDATE/DELETE++### ❌ Never Do:++1. **Never use `.exec()`** with user input (SQL injection risk)+2. **Never hardcode `database_id`** in public repos+3. **Never use `undefined`** in bind parameters (causes D1_TYPE_ERROR)+4. **Never fire individual queries in loops** (use batch instead)+5. **Never forget `LIMIT`** on potentially large result sets+6. **Never use `SELECT *`** in production (specify columns)+7. **Never include `BEGIN TRANSACTION`** in migration files+8. **Never modify applied migrations** (create new ones)+9. **Never skip error handling** on database operations+10. **Never assume queries succeed** (always check results)++---++## Known Issues Prevented++| Issue | Description | How to Avoid |+|-------|-------------|--------------|+| **Statement too long** | Large INSERT statements exceed D1 limits | Break into batches of 100-250 rows |+| **Transaction conflicts** | `BEGIN TRANSACTION` in migration files | Remove BEGIN/COMMIT (D1 handles this) |+| **Foreign key violations** | Schema changes break foreign key constraints | Use `PRAGMA defer_foreign_keys = true` |+| **Rate limiting / queue overload** | Too many individual queries | Use `batch()` instead of loops |+| **Memory limit exceeded** | Query loads too much data into memory | Add LIMIT, paginate results, shard queries |+| **Type mismatch errors** | Using `undefined` instead of `null` | Always use `null` for optional values |++---++## Wrangler Commands Reference++```bash+# Database management+wrangler d1 create <DATABASE_NAME>+wrangler d1 list+wrangler d1 delete <DATABASE_NAME>+wrangler d1 info <DATABASE_NAME>++# Migrations+wrangler d1 migrations create <DATABASE_NAME> <MIGRATION_NAME>+wrangler d1 migrations list <DATABASE_NAME> --local|--remote+wrangler d1 migrations apply <DATABASE_NAME> --local|--remote++# Execute queries+wrangler d1 execute <DATABASE_NAME> --local|--remote --command "SELECT * FROM users"+wrangler d1 execute <DATABASE_NAME> --local|--remote --file=./query.sql++# Time Travel (view historical data)+wrangler d1 time-travel info <DATABASE_NAME> --timestamp "2025-10-20"+wrangler d1 time-travel restore <DATABASE_NAME> --timestamp "2025-10-20"+```++---++## Official Documentation++- **D1 Overview**: https://developers.cloudflare.com/d1/+- **Get Started**: https://developers.cloudflare.com/d1/get-started/+- **Migrations**: https://developers.cloudflare.com/d1/reference/migrations/+- **Workers API**: https://developers.cloudflare.com/d1/worker-api/+- **Best Practices**: https://developers.cloudflare.com/d1/best-practices/+- **Wrangler Commands**: https://developers.cloudflare.com/workers/wrangler/commands/#d1++---++**Ready to build with D1!** 🚀diff --git a/skills/cloudflare-d1/reference/best-practices.md b/skills/cloudflare-d1/reference/best-practices.mdnew file mode 100644index 00000000..0c7f3df5--- /dev/null+++ b/skills/cloudflare-d1/reference/best-practices.md@@ -0,0 +1,652 @@+# D1 Best Practices++**Production-ready patterns for Cloudflare D1**++---++## Table of Contents++1. [Security](#security)+2. [Performance](#performance)+3. [Migrations](#migrations)+4. [Error Handling](#error-handling)+5. [Data Modeling](#data-modeling)+6. [Testing](#testing)+7. [Deployment](#deployment)++---++## Security++### Always Use Prepared Statements++```typescript+// ❌ NEVER: SQL injection vulnerability+const email = c.req.query('email');+await env.DB.exec(`SELECT * FROM users WHERE email = '${email}'`);++// ✅ ALWAYS: Safe prepared statement+const user = await env.DB.prepare('SELECT * FROM users WHERE email = ?')+ .bind(email)+ .first();+```++**Why?** User input like `'; DROP TABLE users; --` would execute in the first example!++### Use null Instead of undefined++```typescript+// ❌ WRONG: undefined causes D1_TYPE_ERROR+await env.DB.prepare('INSERT INTO users (email, bio) VALUES (?, ?)')+ .bind(email, undefined);++// ✅ CORRECT: Use null for optional values+await env.DB.prepare('INSERT INTO users (email, bio) VALUES (?, ?)')+ .bind(email, bio || null);+```++### Never Commit Sensitive IDs++```jsonc+// ❌ WRONG: Database ID in public repo+{+ "d1_databases": [+ {+ "database_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890" // ❌+ }+ ]+}++// ✅ BETTER: Use environment variable or secret+{+ "d1_databases": [+ {+ "database_id": "$D1_DATABASE_ID" // Reference env var+ }+ ]+}+```++Or use wrangler secrets:++```bash+npx wrangler secret put D1_DATABASE_ID+```++### Validate Input Before Binding++```typescript+// ✅ Validate email format+function isValidEmail(email: string): boolean {+ return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);+}++app.post('/api/users', async (c) => {+ const { email } = await c.req.json();++ if (!isValidEmail(email)) {+ return c.json({ error: 'Invalid email format' }, 400);+ }++ // Now safe to use+ const user = await c.env.DB.prepare('INSERT INTO users (email) VALUES (?)')+ .bind(email)+ .run();+});+```++---++## Performance++### Use Batch for Multiple Queries++```typescript+// ❌ BAD: 3 network round trips (~150ms)+const user = await env.DB.prepare('SELECT * FROM users WHERE user_id = ?').bind(1).first();+const posts = await env.DB.prepare('SELECT * FROM posts WHERE user_id = ?').bind(1).all();+const comments = await env.DB.prepare('SELECT * FROM comments WHERE user_id = ?').bind(1).all();++// ✅ GOOD: 1 network round trip (~50ms)+const [userResult, postsResult, commentsResult] = await env.DB.batch([+ env.DB.prepare('SELECT * FROM users WHERE user_id = ?').bind(1),+ env.DB.prepare('SELECT * FROM posts WHERE user_id = ?').bind(1),+ env.DB.prepare('SELECT * FROM comments WHERE user_id = ?').bind(1)+]);++const user = userResult.results[0];+const posts = postsResult.results;+const comments = commentsResult.results;+```++**Performance win: 3x faster!**++### Create Indexes for WHERE Clauses++```sql+-- ❌ Slow: Full table scan+SELECT * FROM posts WHERE user_id = 123;++-- ✅ Fast: Create index first+CREATE INDEX IF NOT EXISTS idx_posts_user_id ON posts(user_id);++-- Now this query is fast+SELECT * FROM posts WHERE user_id = 123;+```++**Verify index is being used:**++```sql+EXPLAIN QUERY PLAN SELECT * FROM posts WHERE user_id = 123;+-- Should see: SEARCH posts USING INDEX idx_posts_user_id+```++### Run PRAGMA optimize After Schema Changes++```sql+-- After creating indexes or altering schema+PRAGMA optimize;+```++This collects statistics that help the query planner choose the best execution plan.++### Select Only Needed Columns++```typescript+// ❌ Bad: Fetches all columns (wastes bandwidth)+const users = await env.DB.prepare('SELECT * FROM users').all();++// ✅ Good: Only fetch what you need+const users = await env.DB.prepare('SELECT user_id, email, username FROM users').all();+```++### Always Use LIMIT++```typescript+// ❌ Dangerous: Could return millions of rows+const posts = await env.DB.prepare('SELECT * FROM posts WHERE published = 1').all();++// ✅ Safe: Limit result set+const posts = await env.DB.prepare(+ 'SELECT * FROM posts WHERE published = 1 LIMIT 100'+).all();+```++### Use Partial Indexes++```sql+-- Index only published posts (smaller index, faster writes)+CREATE INDEX idx_posts_published ON posts(created_at DESC)+ WHERE published = 1;++-- Index only active users (exclude deleted)+CREATE INDEX idx_users_active ON users(email)+ WHERE deleted_at IS NULL;+```++Benefits:+- ✅ Smaller indexes (faster queries)+- ✅ Fewer index updates (faster writes)+- ✅ Only index relevant data++---++## Migrations++### Make Migrations Idempotent++```sql+-- ✅ ALWAYS use IF NOT EXISTS+CREATE TABLE IF NOT EXISTS users (+ user_id INTEGER PRIMARY KEY,+ email TEXT NOT NULL+);++CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);++-- ✅ Use IF EXISTS for drops+DROP TABLE IF EXISTS temp_table;+```++**Why?** Re-running a migration won't fail if it's already applied.++### Never Modify Applied Migrations++```bash+# ❌ WRONG: Editing applied migration+vim migrations/0001_create_users.sql # Already applied!++# ✅ CORRECT: Create new migration+npx wrangler d1 migrations create my-database add_users_bio_column+```++**Why?** D1 tracks which migrations have been applied. Modifying them causes inconsistencies.++### Test Migrations Locally First++```bash+# 1. Apply to local database+npx wrangler d1 migrations apply my-database --local++# 2. Test queries locally+npx wrangler d1 execute my-database --local --command "SELECT * FROM users"++# 3. Only then apply to production+npx wrangler d1 migrations apply my-database --remote+```++### Handle Foreign Keys Carefully++```sql+-- Disable foreign key checks temporarily during schema changes+PRAGMA defer_foreign_keys = true;++-- Make schema changes that would violate foreign keys+ALTER TABLE posts DROP COLUMN old_user_id;+ALTER TABLE posts ADD COLUMN user_id INTEGER REFERENCES users(user_id);++-- Foreign keys re-enabled automatically at end of migration+```++### Break Large Data Migrations into Batches++```sql+-- ❌ BAD: Single massive INSERT (causes "statement too long")+INSERT INTO users (email) VALUES+ ('[email protected]'),+ ('[email protected]'),+ ... -- 10,000 more rows++-- ✅ GOOD: Split into batches of 100-250 rows+-- File: 0001_migrate_users_batch1.sql+INSERT INTO users (email) VALUES+ ('[email protected]'),+ ... -- 100 rows++-- File: 0002_migrate_users_batch2.sql+INSERT INTO users (email) VALUES+ ('[email protected]'),+ ... -- next 100 rows+```++---++## Error Handling++### Check for Errors After Every Query++```typescript+try {+ const result = await env.DB.prepare('INSERT INTO users (email) VALUES (?)')+ .bind(email)+ .run();++ if (!result.success) {+ console.error('Insert failed');+ return c.json({ error: 'Failed to create user' }, 500);+ }++ // Success!+ const userId = result.meta.last_row_id;++} catch (error: any) {+ console.error('Database error:', error.message);+ return c.json({ error: 'Database operation failed' }, 500);+}+```++### Implement Retry Logic for Transient Errors++```typescript+async function queryWithRetry<T>(+ queryFn: () => Promise<T>,+ maxRetries = 3+): Promise<T> {+ for (let attempt = 0; attempt < maxRetries; attempt++) {+ try {+ return await queryFn();+ } catch (error: any) {+ const message = error.message;++ // Check if error is retryable+ const isRetryable =+ message.includes('Network connection lost') ||+ message.includes('storage caused object to be reset') ||+ message.includes('reset because its code was updated');++ if (!isRetryable || attempt === maxRetries - 1) {+ throw error;+ }++ // Exponential backoff: 1s, 2s, 4s+ const delay = Math.min(1000 * Math.pow(2, attempt), 5000);+ await new Promise(resolve => setTimeout(resolve, delay));+ }+ }++ throw new Error('Max retries exceeded');+}++// Usage+const user = await queryWithRetry(() =>+ env.DB.prepare('SELECT * FROM users WHERE user_id = ?')+ .bind(userId)+ .first()+);+```++### Handle Common D1 Errors++```typescript+try {+ await env.DB.prepare(query).bind(...params).run();+} catch (error: any) {+ const message = error.message;++ if (message.includes('D1_ERROR')) {+ // D1-specific error+ console.error('D1 error:', message);+ } else if (message.includes('UNIQUE constraint failed')) {+ // Duplicate key error+ return c.json({ error: 'Email already exists' }, 409);+ } else if (message.includes('FOREIGN KEY constraint failed')) {+ // Invalid foreign key+ return c.json({ error: 'Invalid user reference' }, 400);+ } else {+ // Unknown error+ console.error('Unknown database error:', message);+ return c.json({ error: 'Database operation failed' }, 500);+ }+}+```++---++## Data Modeling++### Use Appropriate Data Types++```sql+CREATE TABLE users (+ user_id INTEGER PRIMARY KEY AUTOINCREMENT, -- Auto-incrementing ID+ email TEXT NOT NULL, -- String+ username TEXT NOT NULL,+ age INTEGER, -- Number+ balance REAL, -- Decimal/float+ is_active INTEGER DEFAULT 1, -- Boolean (0 or 1)+ metadata TEXT, -- JSON (stored as TEXT)+ created_at INTEGER NOT NULL -- Unix timestamp+);+```++**SQLite has 5 types**: NULL, INTEGER, REAL, TEXT, BLOB++### Store Timestamps as Unix Epoch++```sql+-- ✅ RECOMMENDED: Unix timestamp (INTEGER)+created_at INTEGER NOT NULL DEFAULT (unixepoch())++-- ❌ AVOID: ISO 8601 strings (harder to query/compare)+created_at TEXT NOT NULL DEFAULT (datetime('now'))+```++**Why?** Unix timestamps are easier to compare, filter, and work with in JavaScript:++```typescript+// Easy to work with+const timestamp = Date.now(); // 1698000000+const date = new Date(timestamp);++// Easy to query+const recentPosts = await env.DB.prepare(+ 'SELECT * FROM posts WHERE created_at > ?'+).bind(Date.now() - 86400000).all(); // Last 24 hours+```++### Store JSON as TEXT++```sql+CREATE TABLE users (+ user_id INTEGER PRIMARY KEY,+ email TEXT NOT NULL,+ settings TEXT -- Store JSON here+);+```++```typescript+// Insert JSON+const settings = { theme: 'dark', language: 'en' };+await env.DB.prepare('INSERT INTO users (email, settings) VALUES (?, ?)')+ .bind(email, JSON.stringify(settings))+ .run();++// Read JSON+const user = await env.DB.prepare('SELECT * FROM users WHERE user_id = ?')+ .bind(userId)+ .first();++const settings = JSON.parse(user.settings);+console.log(settings.theme); // 'dark'+```++### Use Soft Deletes++```sql+CREATE TABLE users (+ user_id INTEGER PRIMARY KEY,+ email TEXT NOT NULL,+ deleted_at INTEGER -- NULL = active, timestamp = deleted+);++-- Index for active users only+CREATE INDEX idx_users_active ON users(user_id)+ WHERE deleted_at IS NULL;+```++```typescript+// Soft delete+await env.DB.prepare('UPDATE users SET deleted_at = ? WHERE user_id = ?')+ .bind(Date.now(), userId)+ .run();++// Query only active users+const activeUsers = await env.DB.prepare(+ 'SELECT * FROM users WHERE deleted_at IS NULL'+).all();+```++### Normalize Related Data++```sql+-- ✅ GOOD: Normalized (users in separate table)+CREATE TABLE posts (+ post_id INTEGER PRIMARY KEY,+ user_id INTEGER NOT NULL,+ title TEXT NOT NULL,+ FOREIGN KEY (user_id) REFERENCES users(user_id)+);++-- ❌ BAD: Denormalized (user data duplicated in every post)+CREATE TABLE posts (+ post_id INTEGER PRIMARY KEY,+ user_email TEXT NOT NULL,+ user_name TEXT NOT NULL,+ title TEXT NOT NULL+);+```++---++## Testing++### Test Migrations Locally++```bash+# 1. Create local database+npx wrangler d1 migrations apply my-database --local++# 2. Seed with test data+npx wrangler d1 execute my-database --local --file=seed.sql++# 3. Run test queries+npx wrangler d1 execute my-database --local --command "SELECT COUNT(*) FROM users"+```++### Use Separate Databases for Development++```jsonc+{+ "d1_databases": [+ {+ "binding": "DB",+ "database_name": "my-app-prod",+ "database_id": "<PROD_UUID>",+ "preview_database_id": "local-dev" // Local only+ }+ ]+}+```++**Benefits:**+- ✅ Never accidentally modify production data+- ✅ Fast local development (no network latency)+- ✅ Can reset local DB anytime++### Backup Before Major Migrations++```bash+# Export current database+npx wrangler d1 export my-database --remote --output=backup-$(date +%Y%m%d).sql++# Apply migration+npx wrangler d1 migrations apply my-database --remote++# If something goes wrong, restore from backup+npx wrangler d1 execute my-database --remote --file=backup-20251021.sql+```++---++## Deployment++### Use Preview Databases for Testing++```jsonc+{+ "d1_databases": [+ {+ "binding": "DB",+ "database_name": "my-app-prod",+ "database_id": "<PROD_UUID>",+ "preview_database_id": "<PREVIEW_UUID>" // Separate preview database+ }+ ]+}+```++Deploy preview:++```bash+npx wrangler deploy --env preview+```++### Apply Migrations Before Deploying Code++```bash+# 1. Apply migrations first+npx wrangler d1 migrations apply my-database --remote++# 2. Then deploy Worker code+npx wrangler deploy+```++**Why?** Ensures database schema is ready before code expects it.++### Monitor Query Performance++```typescript+app.get('/api/users', async (c) => {+ const start = Date.now();++ const { results, meta } = await c.env.DB.prepare('SELECT * FROM users LIMIT 100')+ .all();++ const duration = Date.now() - start;++ // Log slow queries+ if (duration > 100) {+ console.warn(`Slow query: ${duration}ms, rows_read: ${meta.rows_read}`);+ }++ return c.json({ users: results });+});+```++### Use Time Travel for Data Recovery++```bash+# View database state 2 hours ago+npx wrangler d1 time-travel info my-database --timestamp "2025-10-21T10:00:00Z"++# Restore database to 2 hours ago+npx wrangler d1 time-travel restore my-database --timestamp "2025-10-21T10:00:00Z"+```++**Note**: Time Travel available for last 30 days.++---++## Summary Checklist++### Security ✅+- [ ] Always use `.prepare().bind()` for user input+- [ ] Use `null` instead of `undefined`+- [ ] Validate input before binding+- [ ] Never commit database IDs to public repos++### Performance ✅+- [ ] Use `.batch()` for multiple queries+- [ ] Create indexes on filtered columns+- [ ] Run `PRAGMA optimize` after schema changes+- [ ] Select only needed columns+- [ ] Always use `LIMIT`++### Migrations ✅+- [ ] Make migrations idempotent (IF NOT EXISTS)+- [ ] Never modify applied migrations+- [ ] Test locally before production+- [ ] Break large data migrations into batches++### Error Handling ✅+- [ ] Wrap queries in try/catch+- [ ] Implement retry logic for transient errors+- [ ] Check `result.success` and `meta.rows_written`+- [ ] Log errors with context++### Data Modeling ✅+- [ ] Use appropriate SQLite data types+- [ ] Store timestamps as Unix epoch (INTEGER)+- [ ] Use soft deletes (deleted_at column)+- [ ] Normalize related data with foreign keys++### Testing ✅+- [ ] Test migrations locally first+- [ ] Use separate development/production databases+- [ ] Backup before major migrations++### Deployment ✅+- [ ] Apply migrations before deploying code+- [ ] Use preview databases for testing+- [ ] Monitor query performance+- [ ] Use Time Travel for recovery++---++## Official Documentation++- **Best Practices**: https://developers.cloudflare.com/d1/best-practices/+- **Indexes**: https://developers.cloudflare.com/d1/best-practices/use-indexes/+- **Local Development**: https://developers.cloudflare.com/d1/best-practices/local-development/+- **Retry Queries**: https://developers.cloudflare.com/d1/best-practices/retry-queries/+- **Time Travel**: https://developers.cloudflare.com/d1/reference/time-travel/diff --git a/skills/cloudflare-d1/reference/query-patterns.md b/skills/cloudflare-d1/reference/query-patterns.mdnew file mode 100644index 00000000..8d48a941--- /dev/null+++ b/skills/cloudflare-d1/reference/query-patterns.md@@ -0,0 +1,587 @@+# D1 Query Patterns Reference++**Complete guide to all D1 Workers API methods with examples**++---++## Table of Contents++1. [D1 API Methods Overview](#d1-api-methods-overview)+2. [prepare() - Prepared Statements](#prepare---prepared-statements)+3. [Query Result Methods](#query-result-methods)+4. [batch() - Multiple Queries](#batch---multiple-queries)+5. [exec() - Raw SQL](#exec---raw-sql)+6. [Common Query Patterns](#common-query-patterns)+7. [Performance Tips](#performance-tips)++---++## D1 API Methods Overview++| Method | Use Case | Returns Results | Safe for User Input |+|--------|----------|-----------------|---------------------|+| `.prepare().bind()` | **Primary method** for queries | Yes | ✅ Yes (prevents SQL injection) |+| `.batch()` | Multiple queries in one round trip | Yes | ✅ Yes (if using prepare) |+| `.exec()` | Raw SQL execution | No | ❌ No (SQL injection risk) |++---++## prepare() - Prepared Statements++**Primary method for all queries with user input.**++### Basic Syntax++```typescript+const stmt = env.DB.prepare(sql);+const bound = stmt.bind(...parameters);+const result = await bound.all(); // or .first(), .run()+```++### Method Chaining (Most Common)++```typescript+const result = await env.DB.prepare('SELECT * FROM users WHERE user_id = ?')+ .bind(userId)+ .first();+```++### Parameter Binding++```typescript+// Single parameter+const user = await env.DB.prepare('SELECT * FROM users WHERE email = ?')+ .bind('[email protected]')+ .first();++// Multiple parameters+const posts = await env.DB.prepare(+ 'SELECT * FROM posts WHERE user_id = ? AND published = ? LIMIT ?'+)+.bind(userId, 1, 10)+.all();++// Use null for optional values (NEVER undefined)+const updated = await env.DB.prepare(+ 'UPDATE users SET bio = ?, avatar_url = ? WHERE user_id = ?'+)+.bind(bio || null, avatarUrl || null, userId)+.run();+```++### Why use prepare()?++- ✅ **SQL injection protection** - Parameters are safely escaped+- ✅ **Performance** - Query plans can be cached+- ✅ **Reusability** - Same statement, different parameters+- ✅ **Type safety** - Works with TypeScript generics++---++## Query Result Methods++### .all() - Get All Rows++Returns all matching rows as an array.++```typescript+const { results, meta } = await env.DB.prepare('SELECT * FROM users')+ .all();++console.log(results); // Array of row objects+console.log(meta); // { duration, rows_read, rows_written }+```++**With Type Safety:**++```typescript+interface User {+ user_id: number;+ email: string;+ username: string;+}++const { results } = await env.DB.prepare('SELECT * FROM users')+ .all<User>();++// results is now typed as User[]+```++**Response Structure:**++```typescript+{+ success: true,+ results: [+ { user_id: 1, email: '[email protected]', username: 'alice' },+ { user_id: 2, email: '[email protected]', username: 'bob' }+ ],+ meta: {+ duration: 2.5, // Milliseconds+ rows_read: 2, // Rows scanned+ rows_written: 0 // Rows modified+ }+}+```++---++### .first() - Get First Row++Returns the first row or `null` if no results.++```typescript+const user = await env.DB.prepare('SELECT * FROM users WHERE email = ?')+ .bind('[email protected]')+ .first();++if (!user) {+ return c.json({ error: 'User not found' }, 404);+}+```++**With Type Safety:**++```typescript+const user = await env.DB.prepare('SELECT * FROM users WHERE user_id = ?')+ .bind(userId)+ .first<User>();++// user is typed as User | null+```++**Note**: `.first()` doesn't add `LIMIT 1` automatically. For better performance:++```typescript+// ✅ Better: Add LIMIT 1 yourself+const user = await env.DB.prepare('SELECT * FROM users WHERE email = ? LIMIT 1')+ .bind(email)+ .first();+```++---++### .first(column) - Get Single Column Value++Returns the value of a specific column from the first row.++```typescript+// Get count+const total = await env.DB.prepare('SELECT COUNT(*) as total FROM users')+ .first('total');++console.log(total); // 42 (just the number, not an object)++// Get specific field+const email = await env.DB.prepare('SELECT email FROM users WHERE user_id = ?')+ .bind(userId)+ .first('email');++console.log(email); // '[email protected]'+```++**Use Cases:**+- Counting rows+- Checking existence (SELECT 1)+- Getting single values (MAX, MIN, AVG)++---++### .run() - Execute Without Results++Used for INSERT, UPDATE, DELETE when you don't need the data back.++```typescript+const { success, meta } = await env.DB.prepare(+ 'INSERT INTO users (email, username, created_at) VALUES (?, ?, ?)'+)+.bind(email, username, Date.now())+.run();++console.log(success); // true/false+console.log(meta.last_row_id); // ID of inserted row+console.log(meta.rows_written); // Number of rows affected+```++**Response Structure:**++```typescript+{+ success: true,+ meta: {+ duration: 1.2,+ rows_read: 0,+ rows_written: 1,+ last_row_id: 42 // Only for INSERT with AUTOINCREMENT+ }+}+```++**Check if rows were affected:**++```typescript+const result = await env.DB.prepare('DELETE FROM users WHERE user_id = ?')+ .bind(userId)+ .run();++if (result.meta.rows_written === 0) {+ return c.json({ error: 'User not found' }, 404);+}+```++---++## batch() - Multiple Queries++**CRITICAL FOR PERFORMANCE**: Execute multiple queries in one network round trip.++### Basic Batch++```typescript+const [users, posts, comments] = await env.DB.batch([+ env.DB.prepare('SELECT * FROM users LIMIT 10'),+ env.DB.prepare('SELECT * FROM posts LIMIT 10'),+ env.DB.prepare('SELECT * FROM comments LIMIT 10')+]);++console.log(users.results); // User rows+console.log(posts.results); // Post rows+console.log(comments.results); // Comment rows+```++### Batch with Parameters++```typescript+const stmt1 = env.DB.prepare('SELECT * FROM users WHERE user_id = ?').bind(1);+const stmt2 = env.DB.prepare('SELECT * FROM users WHERE user_id = ?').bind(2);+const stmt3 = env.DB.prepare('SELECT * FROM posts WHERE user_id = ?').bind(1);++const results = await env.DB.batch([stmt1, stmt2, stmt3]);+```++### Bulk Insert with Batch++```typescript+const users = [+ { email: '[email protected]', username: 'user1' },+ { email: '[email protected]', username: 'user2' },+ { email: '[email protected]', username: 'user3' }+];++const inserts = users.map(u =>+ env.DB.prepare('INSERT INTO users (email, username, created_at) VALUES (?, ?, ?)')+ .bind(u.email, u.username, Date.now())+);++const results = await env.DB.batch(inserts);++const successCount = results.filter(r => r.success).length;+console.log(`Inserted ${successCount} users`);+```++### Transaction-like Behavior++```typescript+// All statements execute sequentially+// If one fails, remaining statements don't execute+await env.DB.batch([+ // Deduct credits from user 1+ env.DB.prepare('UPDATE users SET credits = credits - ? WHERE user_id = ?')+ .bind(100, userId1),++ // Add credits to user 2+ env.DB.prepare('UPDATE users SET credits = credits + ? WHERE user_id = ?')+ .bind(100, userId2),++ // Record transaction+ env.DB.prepare('INSERT INTO transactions (from_user, to_user, amount) VALUES (?, ?, ?)')+ .bind(userId1, userId2, 100)+]);+```++**Batch Behavior:**+- Executes statements **sequentially** (in order)+- Each statement commits individually (auto-commit mode)+- If one fails, **remaining statements don't execute**+- All statements in one **network round trip** (huge performance win)++### Batch Performance Comparison++```typescript+// ❌ BAD: 10 separate queries = 10 network round trips+for (let i = 0; i < 10; i++) {+ await env.DB.prepare('SELECT * FROM users WHERE user_id = ?')+ .bind(i)+ .first();+}+// ~500ms total latency++// ✅ GOOD: 1 batch query = 1 network round trip+const userIds = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];+const queries = userIds.map(id =>+ env.DB.prepare('SELECT * FROM users WHERE user_id = ?').bind(id)+);+const results = await env.DB.batch(queries);+// ~50ms total latency+```++---++## exec() - Raw SQL++**AVOID IN PRODUCTION**. Only use for migrations and one-off tasks.++### Basic Exec++```typescript+const result = await env.DB.exec('SELECT * FROM users');++console.log(result);+// { count: 1, duration: 2.5 }+```++**NOTE**: `exec()` does **not return data**, only count and duration!++### Multiple Statements++```typescript+const result = await env.DB.exec(`+ DROP TABLE IF EXISTS temp_users;+ CREATE TABLE temp_users (user_id INTEGER PRIMARY KEY);+ INSERT INTO temp_users VALUES (1), (2), (3);+`);++console.log(result);+// { count: 3, duration: 5.2 }+```++### ⚠️ NEVER Use exec() For:++```typescript+// ❌ NEVER: SQL injection vulnerability+const email = userInput;+await env.DB.exec(`SELECT * FROM users WHERE email = '${email}'`);++// ✅ ALWAYS: Use prepared statements instead+await env.DB.prepare('SELECT * FROM users WHERE email = ?')+ .bind(email)+ .first();+```++### ✅ ONLY Use exec() For:++- Running migration files locally+- One-off maintenance tasks (PRAGMA optimize)+- Database initialization scripts+- CLI tools (not production Workers)++---++## Common Query Patterns++### Existence Check++```typescript+// Check if email exists+const exists = await env.DB.prepare('SELECT 1 FROM users WHERE email = ? LIMIT 1')+ .bind(email)+ .first();++if (exists) {+ return c.json({ error: 'Email already registered' }, 409);+}+```++### Get or Create++```typescript+// Try to find user+let user = await env.DB.prepare('SELECT * FROM users WHERE email = ?')+ .bind(email)+ .first<User>();++// Create if doesn't exist+if (!user) {+ const result = await env.DB.prepare(+ 'INSERT INTO users (email, username, created_at) VALUES (?, ?, ?)'+ )+ .bind(email, username, Date.now())+ .run();++ const userId = result.meta.last_row_id;++ user = await env.DB.prepare('SELECT * FROM users WHERE user_id = ?')+ .bind(userId)+ .first<User>();+}+```++### Pagination++```typescript+const page = 1;+const limit = 20;+const offset = (page - 1) * limit;++const [countResult, dataResult] = await env.DB.batch([+ env.DB.prepare('SELECT COUNT(*) as total FROM posts WHERE published = 1'),+ env.DB.prepare(+ 'SELECT * FROM posts WHERE published = 1 ORDER BY created_at DESC LIMIT ? OFFSET ?'+ ).bind(limit, offset)+]);++const total = countResult.results[0].total;+const posts = dataResult.results;++return {+ posts,+ pagination: {+ page,+ limit,+ total,+ pages: Math.ceil(total / limit)+ }+};+```++### Upsert (INSERT or UPDATE)++```typescript+// SQLite 3.24.0+ supports UPSERT+await env.DB.prepare(`+ INSERT INTO user_settings (user_id, theme, language)+ VALUES (?, ?, ?)+ ON CONFLICT(user_id) DO UPDATE SET+ theme = excluded.theme,+ language = excluded.language,+ updated_at = unixepoch()+`)+.bind(userId, theme, language)+.run();+```++### Bulk Upsert++```typescript+const settings = [+ { user_id: 1, theme: 'dark', language: 'en' },+ { user_id: 2, theme: 'light', language: 'es' }+];++const upserts = settings.map(s =>+ env.DB.prepare(`+ INSERT INTO user_settings (user_id, theme, language)+ VALUES (?, ?, ?)+ ON CONFLICT(user_id) DO UPDATE SET+ theme = excluded.theme,+ language = excluded.language+ `).bind(s.user_id, s.theme, s.language)+);++await env.DB.batch(upserts);+```++---++## Performance Tips++### Use SELECT Column Names (Not SELECT *)++```typescript+// ❌ Bad: Fetches all columns+const users = await env.DB.prepare('SELECT * FROM users').all();++// ✅ Good: Only fetch needed columns+const users = await env.DB.prepare('SELECT user_id, email, username FROM users').all();+```++### Always Use LIMIT++```typescript+// ❌ Bad: Could return millions of rows+const posts = await env.DB.prepare('SELECT * FROM posts').all();++// ✅ Good: Limit result set+const posts = await env.DB.prepare('SELECT * FROM posts LIMIT 100').all();+```++### Use Indexes++```sql+-- Create index for common queries+CREATE INDEX IF NOT EXISTS idx_posts_published_created+ ON posts(published, created_at DESC)+ WHERE published = 1;+```++```typescript+// Query will use the index+const posts = await env.DB.prepare(+ 'SELECT * FROM posts WHERE published = 1 ORDER BY created_at DESC LIMIT 10'+).all();+```++### Check Index Usage++```sql+EXPLAIN QUERY PLAN SELECT * FROM posts WHERE published = 1;+-- Should see: SEARCH posts USING INDEX idx_posts_published_created+```++### Batch Instead of Loop++```typescript+// ❌ Bad: Multiple network round trips+for (const id of userIds) {+ const user = await env.DB.prepare('SELECT * FROM users WHERE user_id = ?')+ .bind(id)+ .first();+}++// ✅ Good: One network round trip+const queries = userIds.map(id =>+ env.DB.prepare('SELECT * FROM users WHERE user_id = ?').bind(id)+);+const results = await env.DB.batch(queries);+```++---++## Meta Object Reference++Every D1 query returns a `meta` object with execution details:++```typescript+{+ duration: 2.5, // Query execution time in milliseconds+ rows_read: 100, // Number of rows scanned+ rows_written: 1, // Number of rows modified (INSERT/UPDATE/DELETE)+ last_row_id: 42, // ID of last inserted row (INSERT only)+ changed: 1 // Rows affected (UPDATE/DELETE only)+}+```++### Using Meta for Debugging++```typescript+const result = await env.DB.prepare('SELECT * FROM large_table WHERE status = ?')+ .bind('active')+ .all();++console.log(`Query took ${result.meta.duration}ms`);+console.log(`Scanned ${result.meta.rows_read} rows`);+console.log(`Returned ${result.results.length} rows`);++// If rows_read is much higher than results.length, add an index!+if (result.meta.rows_read > result.results.length * 10) {+ console.warn('Query is inefficient - consider adding an index');+}+```++---++## Official Documentation++- **Workers API**: https://developers.cloudflare.com/d1/worker-api/+- **Prepared Statements**: https://developers.cloudflare.com/d1/worker-api/prepared-statements/+- **Return Object**: https://developers.cloudflare.com/d1/worker-api/return-object/diff --git a/skills/cloudflare-d1/templates/d1-setup-migration.sh b/skills/cloudflare-d1/templates/d1-setup-migration.shnew file mode 100755index 00000000..57a6b5c0--- /dev/null+++ b/skills/cloudflare-d1/templates/d1-setup-migration.sh@@ -0,0 +1,174 @@+#!/bin/bash+#+# Cloudflare D1 Setup and Migration Workflow+#+# This script demonstrates the complete D1 workflow:+# 1. Create a D1 database+# 2. Configure bindings+# 3. Create and apply migrations+# 4. Query the database+#+# Usage:+# chmod +x d1-setup-migration.sh+# ./d1-setup-migration.sh my-app-database+#++set -e # Exit on error++DATABASE_NAME="${1:-my-database}"++echo "========================================="+echo "Cloudflare D1 Setup and Migration"+echo "========================================="+echo ""++# Step 1: Create D1 Database+echo "📦 Step 1: Creating D1 database '$DATABASE_NAME'..."+echo ""++npx wrangler d1 create "$DATABASE_NAME"++echo ""+echo "✅ Database created!"+echo ""+echo "📝 IMPORTANT: Copy the output above and add to your wrangler.jsonc:"+echo ""+echo ' {+ "d1_databases": [+ {+ "binding": "DB",+ "database_name": "'"$DATABASE_NAME"'",+ "database_id": "<UUID_FROM_OUTPUT_ABOVE>",+ "preview_database_id": "local-dev-db"+ }+ ]+ }'+echo ""+read -p "Press ENTER when you've added the binding to wrangler.jsonc..."++# Step 2: Create Migrations Directory+echo ""+echo "📁 Step 2: Setting up migrations directory..."+mkdir -p migrations++# Step 3: Create Initial Migration+echo ""+echo "🔨 Step 3: Creating initial migration..."+echo ""++npx wrangler d1 migrations create "$DATABASE_NAME" create_initial_schema++# Find the created migration file (most recent .sql file in migrations/)+MIGRATION_FILE=$(ls -t migrations/*.sql | head -n1)++echo ""+echo "✅ Migration file created: $MIGRATION_FILE"+echo ""+echo "📝 Add your schema to this file. Example:"+echo ""+echo " DROP TABLE IF EXISTS users;+ CREATE TABLE IF NOT EXISTS users (+ user_id INTEGER PRIMARY KEY AUTOINCREMENT,+ email TEXT NOT NULL UNIQUE,+ username TEXT NOT NULL,+ created_at INTEGER NOT NULL,+ updated_at INTEGER+ );++ CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);++ PRAGMA optimize;"+echo ""+read -p "Press ENTER when you've edited the migration file..."++# Step 4: Apply Migration Locally+echo ""+echo "🔧 Step 4: Applying migration to LOCAL database..."+echo ""++npx wrangler d1 migrations apply "$DATABASE_NAME" --local++echo ""+echo "✅ Local migration applied!"++# Step 5: Verify Local Database+echo ""+echo "🔍 Step 5: Verifying local database..."+echo ""++npx wrangler d1 execute "$DATABASE_NAME" --local --command "SELECT name FROM sqlite_master WHERE type='table'"++# Step 6: Seed Local Database (Optional)+echo ""+echo "🌱 Step 6: Would you like to seed the local database with test data?"+read -p "Seed database? (y/n): " -n 1 -r+echo ""++if [[ $REPLY =~ ^[Yy]$ ]]; then+ echo "Creating seed data..."++ cat > seed.sql << 'EOF'+-- Seed data for testing+INSERT INTO users (email, username, created_at) VALUES+ ('[email protected]', 'alice', unixepoch()),+ ('[email protected]', 'bob', unixepoch()),+ ('[email protected]', 'charlie', unixepoch());+EOF++ npx wrangler d1 execute "$DATABASE_NAME" --local --file=seed.sql++ echo ""+ echo "✅ Seed data inserted!"+ echo ""+ echo "🔍 Verifying data..."+ npx wrangler d1 execute "$DATABASE_NAME" --local --command "SELECT * FROM users"+fi++# Step 7: Apply to Production (Optional)+echo ""+echo "🚀 Step 7: Ready to apply migration to PRODUCTION?"+echo ""+echo "⚠️ WARNING: This will modify your production database!"+read -p "Apply to production? (y/n): " -n 1 -r+echo ""++if [[ $REPLY =~ ^[Yy]$ ]]; then+ echo "Applying migration to production..."+ npx wrangler d1 migrations apply "$DATABASE_NAME" --remote++ echo ""+ echo "✅ Production migration applied!"+else+ echo "Skipping production migration."+ echo ""+ echo "To apply later, run:"+ echo " npx wrangler d1 migrations apply $DATABASE_NAME --remote"+fi++# Summary+echo ""+echo "========================================="+echo "✅ D1 Setup Complete!"+echo "========================================="+echo ""+echo "Database: $DATABASE_NAME"+echo "Local database: ✅"+echo "Migrations: ✅"+echo ""+echo "📚 Next steps:"+echo ""+echo "1. Start dev server:"+echo " npm run dev"+echo ""+echo "2. Query from your Worker:"+echo ' const user = await env.DB.prepare("SELECT * FROM users WHERE email = ?")+ .bind("[email protected]")+ .first();'+echo ""+echo "3. Create more migrations as needed:"+echo " npx wrangler d1 migrations create $DATABASE_NAME <migration_name>"+echo ""+echo "4. View all tables:"+echo " npx wrangler d1 execute $DATABASE_NAME --local --command \"SELECT name FROM sqlite_master WHERE type='table'\""+echo ""+echo "========================================="diff --git a/skills/cloudflare-d1/templates/d1-worker-queries.ts b/skills/cloudflare-d1/templates/d1-worker-queries.tsnew file mode 100644index 00000000..6348697d--- /dev/null+++ b/skills/cloudflare-d1/templates/d1-worker-queries.ts@@ -0,0 +1,591 @@+/**+ * Cloudflare D1 Worker Query Examples+ *+ * This file demonstrates type-safe D1 queries in a Cloudflare Worker with Hono.+ *+ * Topics covered:+ * - Type definitions for D1 bindings+ * - CRUD operations (Create, Read, Update, Delete)+ * - Batch queries for performance+ * - Error handling and validation+ * - Pagination patterns+ * - JOIN queries+ * - Transaction-like behavior+ *+ * Usage:+ * 1. Copy relevant patterns to your Worker+ * 2. Update table/column names to match your schema+ * 3. Add proper input validation+ */++import { Hono } from 'hono';++// ============================================+// Type Definitions+// ============================================++interface Env {+ DB: D1Database;+ // ... other bindings+}++type Bindings = {+ DB: D1Database;+};++interface User {+ user_id: number;+ email: string;+ username: string;+ full_name: string | null;+ created_at: number;+ updated_at: number | null;+}++interface Post {+ post_id: number;+ user_id: number;+ title: string;+ content: string;+ slug: string;+ published: number;+ created_at: number;+ published_at: number | null;+}++interface PostWithAuthor extends Post {+ author_name: string;+ author_email: string;+}++// ============================================+// App Setup+// ============================================++const app = new Hono<{ Bindings: Bindings }>();++// ============================================+// CREATE Operations+// ============================================++// Create a new user+app.post('/api/users', async (c) => {+ try {+ const { email, username, full_name } = await c.req.json();++ // Validate input+ if (!email || !username) {+ return c.json({ error: 'Email and username are required' }, 400);+ }++ // Check if email already exists+ const existing = await c.env.DB.prepare(+ 'SELECT user_id FROM users WHERE email = ? LIMIT 1'+ )+ .bind(email)+ .first();++ if (existing) {+ return c.json({ error: 'Email already registered' }, 409);+ }++ // Insert new user+ const result = await c.env.DB.prepare(+ 'INSERT INTO users (email, username, full_name, created_at) VALUES (?, ?, ?, ?)'+ )+ .bind(email, username, full_name || null, Date.now())+ .run();++ const userId = result.meta.last_row_id;++ // Fetch the created user+ const user = await c.env.DB.prepare('SELECT * FROM users WHERE user_id = ?')+ .bind(userId)+ .first<User>();++ return c.json({ user }, 201);+ } catch (error: any) {+ console.error('Error creating user:', error.message);+ return c.json({ error: 'Failed to create user' }, 500);+ }+});++// Bulk insert with batch()+app.post('/api/users/bulk', async (c) => {+ try {+ const { users } = await c.req.json();++ if (!Array.isArray(users) || users.length === 0) {+ return c.json({ error: 'Invalid users array' }, 400);+ }++ // Create batch of insert statements+ const inserts = users.map(user =>+ c.env.DB.prepare(+ 'INSERT INTO users (email, username, full_name, created_at) VALUES (?, ?, ?, ?)'+ ).bind(user.email, user.username, user.full_name || null, Date.now())+ );++ // Execute all inserts in one batch+ const results = await c.env.DB.batch(inserts);++ const insertedCount = results.filter(r => r.success).length;++ return c.json({+ message: `Inserted ${insertedCount} users`,+ count: insertedCount+ }, 201);+ } catch (error: any) {+ console.error('Error bulk inserting users:', error.message);+ return c.json({ error: 'Failed to insert users' }, 500);+ }+});++// ============================================+// READ Operations+// ============================================++// Get single user by ID+app.get('/api/users/:id', async (c) => {+ try {+ const userId = parseInt(c.req.param('id'));++ const user = await c.env.DB.prepare('SELECT * FROM users WHERE user_id = ?')+ .bind(userId)+ .first<User>();++ if (!user) {+ return c.json({ error: 'User not found' }, 404);+ }++ return c.json({ user });+ } catch (error: any) {+ console.error('Error fetching user:', error.message);+ return c.json({ error: 'Failed to fetch user' }, 500);+ }+});++// Get user by email+app.get('/api/users/email/:email', async (c) => {+ try {+ const email = c.req.param('email');++ const user = await c.env.DB.prepare('SELECT * FROM users WHERE email = ?')+ .bind(email)+ .first<User>();++ if (!user) {+ return c.json({ error: 'User not found' }, 404);+ }++ return c.json({ user });+ } catch (error: any) {+ console.error('Error fetching user:', error.message);+ return c.json({ error: 'Failed to fetch user' }, 500);+ }+});++// List users with pagination+app.get('/api/users', async (c) => {+ try {+ const page = parseInt(c.req.query('page') || '1');+ const limit = Math.min(parseInt(c.req.query('limit') || '20'), 100); // Max 100+ const offset = (page - 1) * limit;++ // Use batch to get count and users in one round trip+ const [countResult, usersResult] = await c.env.DB.batch([+ c.env.DB.prepare('SELECT COUNT(*) as total FROM users WHERE deleted_at IS NULL'),+ c.env.DB.prepare(+ 'SELECT * FROM users WHERE deleted_at IS NULL ORDER BY created_at DESC LIMIT ? OFFSET ?'+ ).bind(limit, offset)+ ]);++ const total = (countResult.results[0] as any).total as number;+ const users = usersResult.results as User[];++ return c.json({+ users,+ pagination: {+ page,+ limit,+ total,+ pages: Math.ceil(total / limit)+ }+ });+ } catch (error: any) {+ console.error('Error listing users:', error.message);+ return c.json({ error: 'Failed to list users' }, 500);+ }+});++// ============================================+// UPDATE Operations+// ============================================++// Update user+app.put('/api/users/:id', async (c) => {+ try {+ const userId = parseInt(c.req.param('id'));+ const { username, full_name, bio } = await c.req.json();++ // Build dynamic update query+ const updates: string[] = [];+ const values: any[] = [];++ if (username !== undefined) {+ updates.push('username = ?');+ values.push(username);+ }+ if (full_name !== undefined) {+ updates.push('full_name = ?');+ values.push(full_name);+ }+ if (bio !== undefined) {+ updates.push('bio = ?');+ values.push(bio);+ }++ if (updates.length === 0) {+ return c.json({ error: 'No fields to update' }, 400);+ }++ // Add updated_at+ updates.push('updated_at = ?');+ values.push(Date.now());++ // Add user_id for WHERE clause+ values.push(userId);++ const sql = `UPDATE users SET ${updates.join(', ')} WHERE user_id = ?`;++ const result = await c.env.DB.prepare(sql).bind(...values).run();++ if (result.meta.rows_written === 0) {+ return c.json({ error: 'User not found' }, 404);+ }++ // Fetch updated user+ const user = await c.env.DB.prepare('SELECT * FROM users WHERE user_id = ?')+ .bind(userId)+ .first<User>();++ return c.json({ user });+ } catch (error: any) {+ console.error('Error updating user:', error.message);+ return c.json({ error: 'Failed to update user' }, 500);+ }+});++// Increment post view count (simple update)+app.post('/api/posts/:id/view', async (c) => {+ try {+ const postId = parseInt(c.req.param('id'));++ const result = await c.env.DB.prepare(+ 'UPDATE posts SET view_count = view_count + 1 WHERE post_id = ?'+ )+ .bind(postId)+ .run();++ if (result.meta.rows_written === 0) {+ return c.json({ error: 'Post not found' }, 404);+ }++ return c.json({ success: true });+ } catch (error: any) {+ console.error('Error incrementing view count:', error.message);+ return c.json({ error: 'Failed to update view count' }, 500);+ }+});++// ============================================+// DELETE Operations+// ============================================++// Soft delete user+app.delete('/api/users/:id', async (c) => {+ try {+ const userId = parseInt(c.req.param('id'));++ const result = await c.env.DB.prepare(+ 'UPDATE users SET deleted_at = ? WHERE user_id = ? AND deleted_at IS NULL'+ )+ .bind(Date.now(), userId)+ .run();++ if (result.meta.rows_written === 0) {+ return c.json({ error: 'User not found or already deleted' }, 404);+ }++ return c.json({ success: true });+ } catch (error: any) {+ console.error('Error deleting user:', error.message);+ return c.json({ error: 'Failed to delete user' }, 500);+ }+});++// Hard delete post+app.delete('/api/posts/:id/permanent', async (c) => {+ try {+ const postId = parseInt(c.req.param('id'));++ const result = await c.env.DB.prepare('DELETE FROM posts WHERE post_id = ?')+ .bind(postId)+ .run();++ if (result.meta.rows_written === 0) {+ return c.json({ error: 'Post not found' }, 404);+ }++ return c.json({ success: true });+ } catch (error: any) {+ console.error('Error deleting post:', error.message);+ return c.json({ error: 'Failed to delete post' }, 500);+ }+});++// ============================================+// JOIN Queries+// ============================================++// Get posts with author information+app.get('/api/posts', async (c) => {+ try {+ const limit = Math.min(parseInt(c.req.query('limit') || '20'), 100);++ const { results } = await c.env.DB.prepare(`+ SELECT+ posts.*,+ users.username as author_name,+ users.email as author_email+ FROM posts+ INNER JOIN users ON posts.user_id = users.user_id+ WHERE posts.published = 1+ AND users.deleted_at IS NULL+ ORDER BY posts.published_at DESC+ LIMIT ?+ `)+ .bind(limit)+ .all<PostWithAuthor>();++ return c.json({ posts: results });+ } catch (error: any) {+ console.error('Error fetching posts:', error.message);+ return c.json({ error: 'Failed to fetch posts' }, 500);+ }+});++// Get post with author and tags+app.get('/api/posts/:slug', async (c) => {+ try {+ const slug = c.req.param('slug');++ // Use batch to get post+author and tags in one round trip+ const [postResult, tagsResult] = await c.env.DB.batch([+ // Get post with author+ c.env.DB.prepare(`+ SELECT+ posts.*,+ users.username as author_name,+ users.email as author_email+ FROM posts+ INNER JOIN users ON posts.user_id = users.user_id+ WHERE posts.slug = ?+ LIMIT 1+ `).bind(slug),++ // Get post's tags+ c.env.DB.prepare(`+ SELECT tags.*+ FROM tags+ INNER JOIN post_tags ON tags.tag_id = post_tags.tag_id+ INNER JOIN posts ON post_tags.post_id = posts.post_id+ WHERE posts.slug = ?+ `).bind(slug)+ ]);++ const post = postResult.results[0] as PostWithAuthor | undefined;++ if (!post) {+ return c.json({ error: 'Post not found' }, 404);+ }++ const tags = tagsResult.results;++ return c.json({ post, tags });+ } catch (error: any) {+ console.error('Error fetching post:', error.message);+ return c.json({ error: 'Failed to fetch post' }, 500);+ }+});++// ============================================+// Transaction-like Behavior with Batch+// ============================================++// Publish post (update post + record event)+app.post('/api/posts/:id/publish', async (c) => {+ try {+ const postId = parseInt(c.req.param('id'));+ const now = Date.now();++ // Execute multiple related updates in one batch+ const results = await c.env.DB.batch([+ // Update post status+ c.env.DB.prepare(+ 'UPDATE posts SET published = 1, published_at = ?, updated_at = ? WHERE post_id = ?'+ ).bind(now, now, postId),++ // Record publish event (example analytics table)+ c.env.DB.prepare(+ 'INSERT INTO post_events (post_id, event_type, created_at) VALUES (?, ?, ?)'+ ).bind(postId, 'published', now)+ ]);++ // Check if post update succeeded+ if (results[0].meta.rows_written === 0) {+ return c.json({ error: 'Post not found' }, 404);+ }++ return c.json({ success: true });+ } catch (error: any) {+ console.error('Error publishing post:', error.message);+ return c.json({ error: 'Failed to publish post' }, 500);+ }+});++// ============================================+// Advanced Patterns+// ============================================++// Search posts by keyword (simple full-text search)+app.get('/api/posts/search', async (c) => {+ try {+ const query = c.req.query('q') || '';+ const limit = Math.min(parseInt(c.req.query('limit') || '20'), 100);++ if (query.length < 2) {+ return c.json({ error: 'Query must be at least 2 characters' }, 400);+ }++ const searchTerm = `%${query}%`;++ const { results } = await c.env.DB.prepare(`+ SELECT+ posts.*,+ users.username as author_name+ FROM posts+ INNER JOIN users ON posts.user_id = users.user_id+ WHERE posts.published = 1+ AND (posts.title LIKE ? OR posts.content LIKE ?)+ ORDER BY posts.published_at DESC+ LIMIT ?+ `)+ .bind(searchTerm, searchTerm, limit)+ .all<PostWithAuthor>();++ return c.json({ posts: results, query });+ } catch (error: any) {+ console.error('Error searching posts:', error.message);+ return c.json({ error: 'Failed to search posts' }, 500);+ }+});++// Get user stats (multiple aggregations in batch)+app.get('/api/users/:id/stats', async (c) => {+ try {+ const userId = parseInt(c.req.param('id'));++ const [userResult, statsResults] = await c.env.DB.batch([+ // Get user+ c.env.DB.prepare('SELECT * FROM users WHERE user_id = ?').bind(userId),++ // Get all stats in one query with UNION+ c.env.DB.prepare(`+ SELECT 'posts' as metric, COUNT(*) as count FROM posts WHERE user_id = ?+ UNION ALL+ SELECT 'comments', COUNT(*) FROM comments WHERE user_id = ?+ UNION ALL+ SELECT 'published_posts', COUNT(*) FROM posts WHERE user_id = ? AND published = 1+ `).bind(userId, userId, userId)+ ]);++ const user = userResult.results[0] as User | undefined;++ if (!user) {+ return c.json({ error: 'User not found' }, 404);+ }++ // Parse stats results+ const stats: Record<string, number> = {};+ for (const row of statsResults.results as any[]) {+ stats[row.metric] = row.count;+ }++ return c.json({ user, stats });+ } catch (error: any) {+ console.error('Error fetching user stats:', error.message);+ return c.json({ error: 'Failed to fetch user stats' }, 500);+ }+});++// ============================================+// Error Handling Example with Retry+// ============================================++async function queryWithRetry<T>(+ queryFn: () => Promise<T>,+ maxRetries = 3+): Promise<T> {+ for (let attempt = 0; attempt < maxRetries; attempt++) {+ try {+ return await queryFn();+ } catch (error: any) {+ const message = error.message;++ // Check if error is retryable+ const isRetryable =+ message.includes('Network connection lost') ||+ message.includes('storage caused object to be reset') ||+ message.includes('reset because its code was updated');++ if (!isRetryable || attempt === maxRetries - 1) {+ throw error;+ }++ // Exponential backoff+ const delay = Math.min(1000 * Math.pow(2, attempt), 5000);+ await new Promise(resolve => setTimeout(resolve, delay));+ }+ }++ throw new Error('Retry logic failed');+}++// Example usage with retry+app.get('/api/users/:id/with-retry', async (c) => {+ try {+ const userId = parseInt(c.req.param('id'));++ const user = await queryWithRetry(() =>+ c.env.DB.prepare('SELECT * FROM users WHERE user_id = ?')+ .bind(userId)+ .first<User>()+ );++ if (!user) {+ return c.json({ error: 'User not found' }, 404);+ }++ return c.json({ user });+ } catch (error: any) {+ console.error('Error fetching user (with retry):', error.message);+ return c.json({ error: 'Failed to fetch user' }, 500);+ }+});++// ============================================+// Export App+// ============================================++export default app;diff --git a/skills/cloudflare-d1/templates/schema-example.sql b/skills/cloudflare-d1/templates/schema-example.sqlnew file mode 100644index 00000000..7ae2514a--- /dev/null+++ b/skills/cloudflare-d1/templates/schema-example.sql@@ -0,0 +1,248 @@+-- Cloudflare D1 Schema Example+-- Production-ready database schema with best practices+--+-- This file demonstrates:+-- - Proper table creation with constraints+-- - Primary and foreign keys+-- - Indexes for performance+-- - Sample data for testing+--+-- Apply with:+-- npx wrangler d1 execute my-database --local --file=schema-example.sql++-- ============================================+-- Users Table+-- ============================================++DROP TABLE IF EXISTS users;+CREATE TABLE IF NOT EXISTS users (+ user_id INTEGER PRIMARY KEY AUTOINCREMENT,+ email TEXT NOT NULL UNIQUE,+ username TEXT NOT NULL,+ full_name TEXT,+ bio TEXT,+ avatar_url TEXT,+ created_at INTEGER NOT NULL DEFAULT (unixepoch()),+ updated_at INTEGER,+ deleted_at INTEGER -- Soft delete pattern+);++-- Index for email lookups (login, registration checks)+CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);++-- Index for filtering out deleted users+CREATE INDEX IF NOT EXISTS idx_users_active ON users(user_id) WHERE deleted_at IS NULL;++-- ============================================+-- Posts Table+-- ============================================++DROP TABLE IF EXISTS posts;+CREATE TABLE IF NOT EXISTS posts (+ post_id INTEGER PRIMARY KEY AUTOINCREMENT,+ user_id INTEGER NOT NULL,+ title TEXT NOT NULL,+ content TEXT NOT NULL,+ slug TEXT NOT NULL UNIQUE,+ published INTEGER NOT NULL DEFAULT 0, -- 0 = draft, 1 = published+ view_count INTEGER NOT NULL DEFAULT 0,+ created_at INTEGER NOT NULL DEFAULT (unixepoch()),+ updated_at INTEGER,+ published_at INTEGER,++ -- Foreign key constraint+ FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE+);++-- Index for user's posts+CREATE INDEX IF NOT EXISTS idx_posts_user_id ON posts(user_id);++-- Index for published posts (most common query)+CREATE INDEX IF NOT EXISTS idx_posts_published_created ON posts(published, created_at DESC)+ WHERE published = 1;++-- Index for slug lookups (e.g., /blog/my-post-slug)+CREATE INDEX IF NOT EXISTS idx_posts_slug ON posts(slug);++-- ============================================+-- Comments Table+-- ============================================++DROP TABLE IF EXISTS comments;+CREATE TABLE IF NOT EXISTS comments (+ comment_id INTEGER PRIMARY KEY AUTOINCREMENT,+ post_id INTEGER NOT NULL,+ user_id INTEGER NOT NULL,+ parent_comment_id INTEGER, -- For threaded comments (NULL = top-level)+ content TEXT NOT NULL,+ created_at INTEGER NOT NULL DEFAULT (unixepoch()),+ updated_at INTEGER,+ deleted_at INTEGER,++ -- Foreign keys+ FOREIGN KEY (post_id) REFERENCES posts(post_id) ON DELETE CASCADE,+ FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE,+ FOREIGN KEY (parent_comment_id) REFERENCES comments(comment_id) ON DELETE CASCADE+);++-- Index for post's comments+CREATE INDEX IF NOT EXISTS idx_comments_post_id ON comments(post_id);++-- Index for user's comments+CREATE INDEX IF NOT EXISTS idx_comments_user_id ON comments(user_id);++-- Index for threaded replies+CREATE INDEX IF NOT EXISTS idx_comments_parent ON comments(parent_comment_id)+ WHERE parent_comment_id IS NOT NULL;++-- ============================================+-- Tags Table (Many-to-Many Example)+-- ============================================++DROP TABLE IF EXISTS tags;+CREATE TABLE IF NOT EXISTS tags (+ tag_id INTEGER PRIMARY KEY AUTOINCREMENT,+ name TEXT NOT NULL UNIQUE,+ slug TEXT NOT NULL UNIQUE,+ created_at INTEGER NOT NULL DEFAULT (unixepoch())+);++-- Index for tag lookups+CREATE INDEX IF NOT EXISTS idx_tags_slug ON tags(slug);++-- ============================================+-- Post Tags Junction Table+-- ============================================++DROP TABLE IF EXISTS post_tags;+CREATE TABLE IF NOT EXISTS post_tags (+ post_id INTEGER NOT NULL,+ tag_id INTEGER NOT NULL,+ created_at INTEGER NOT NULL DEFAULT (unixepoch()),++ -- Composite primary key+ PRIMARY KEY (post_id, tag_id),++ -- Foreign keys+ FOREIGN KEY (post_id) REFERENCES posts(post_id) ON DELETE CASCADE,+ FOREIGN KEY (tag_id) REFERENCES tags(tag_id) ON DELETE CASCADE+);++-- Index for finding posts by tag+CREATE INDEX IF NOT EXISTS idx_post_tags_tag_id ON post_tags(tag_id);++-- ============================================+-- Sessions Table (Example: Auth Sessions)+-- ============================================++DROP TABLE IF EXISTS sessions;+CREATE TABLE IF NOT EXISTS sessions (+ session_id TEXT PRIMARY KEY, -- UUID or random token+ user_id INTEGER NOT NULL,+ ip_address TEXT,+ user_agent TEXT,+ created_at INTEGER NOT NULL DEFAULT (unixepoch()),+ expires_at INTEGER NOT NULL,+ last_activity_at INTEGER NOT NULL DEFAULT (unixepoch()),++ FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE+);++-- Index for session cleanup (delete expired sessions)+CREATE INDEX IF NOT EXISTS idx_sessions_expires ON sessions(expires_at);++-- Index for user's sessions+CREATE INDEX IF NOT EXISTS idx_sessions_user_id ON sessions(user_id);++-- ============================================+-- Analytics Table (High-Write Pattern)+-- ============================================++DROP TABLE IF EXISTS page_views;+CREATE TABLE IF NOT EXISTS page_views (+ view_id INTEGER PRIMARY KEY AUTOINCREMENT,+ post_id INTEGER,+ user_id INTEGER, -- NULL for anonymous views+ ip_address TEXT,+ referrer TEXT,+ user_agent TEXT,+ viewed_at INTEGER NOT NULL DEFAULT (unixepoch())+);++-- Partial index: only index recent views (last 30 days)+CREATE INDEX IF NOT EXISTS idx_page_views_recent ON page_views(post_id, viewed_at)+ WHERE viewed_at > unixepoch() - 2592000; -- 30 days in seconds++-- ============================================+-- Optimize Database+-- ============================================++-- Run PRAGMA optimize to collect statistics for query planner+PRAGMA optimize;++-- ============================================+-- Sample Seed Data (Optional - for testing)+-- ============================================++-- Insert test users+INSERT INTO users (email, username, full_name, bio) VALUES+ ('[email protected]', 'alice', 'Alice Johnson', 'Software engineer and blogger'),+ ('[email protected]', 'bob', 'Bob Smith', 'Tech enthusiast'),+ ('[email protected]', 'charlie', 'Charlie Brown', 'Writer and photographer');++-- Insert test tags+INSERT INTO tags (name, slug) VALUES+ ('JavaScript', 'javascript'),+ ('TypeScript', 'typescript'),+ ('Cloudflare', 'cloudflare'),+ ('Web Development', 'web-development'),+ ('Tutorial', 'tutorial');++-- Insert test posts+INSERT INTO posts (user_id, title, content, slug, published, published_at) VALUES+ (1, 'Getting Started with D1', 'Learn how to use Cloudflare D1 database...', 'getting-started-with-d1', 1, unixepoch()),+ (1, 'Building APIs with Hono', 'Hono is a lightweight web framework...', 'building-apis-with-hono', 1, unixepoch() - 86400),+ (2, 'My First Draft', 'This is a draft post...', 'my-first-draft', 0, NULL);++-- Link posts to tags+INSERT INTO post_tags (post_id, tag_id) VALUES+ (1, 3), -- Getting Started with D1 -> Cloudflare+ (1, 5), -- Getting Started with D1 -> Tutorial+ (2, 1), -- Building APIs with Hono -> JavaScript+ (2, 3), -- Building APIs with Hono -> Cloudflare+ (2, 5); -- Building APIs with Hono -> Tutorial++-- Insert test comments+INSERT INTO comments (post_id, user_id, content) VALUES+ (1, 2, 'Great tutorial! Really helpful.'),+ (1, 3, 'Thanks for sharing this!'),+ (2, 3, 'Looking forward to more content on Hono.');++-- Insert threaded reply+INSERT INTO comments (post_id, user_id, parent_comment_id, content) VALUES+ (1, 1, 1, 'Glad you found it useful!');++-- ============================================+-- Verification Queries+-- ============================================++-- Count records in each table+SELECT 'users' as table_name, COUNT(*) as count FROM users+UNION ALL+SELECT 'posts', COUNT(*) FROM posts+UNION ALL+SELECT 'comments', COUNT(*) FROM comments+UNION ALL+SELECT 'tags', COUNT(*) FROM tags+UNION ALL+SELECT 'post_tags', COUNT(*) FROM post_tags;++-- List all tables and indexes+SELECT+ type,+ name,+ tbl_name as table_name+FROM sqlite_master+WHERE type IN ('table', 'index')+ AND name NOT LIKE 'sqlite_%'+ORDER BY type, tbl_name, name;