secondsky/claude-skills / plugins/cloudflare-d1/skills/cloudflare-d1
cloudflare-d1
VerifiedCloudflare D1 serverless SQLite on edge. Use for databases, migrations, bindings, or encountering D1_ERROR, statement too long, too many requests queued errors.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 15.4 KB |
| references/2025-features.md | markdown | 15.7 KB |
| references/best-practices.md | markdown | 33.6 KB |
| references/limits.md | markdown | 12.6 KB |
| references/metrics-analytics.md | markdown | 19.0 KB |
| references/query-patterns.md | markdown | 20.8 KB |
| references/read-replication.md | markdown | 19.7 KB |
| references/setup-guide.md | markdown | 3.0 KB |
| templates/d1-setup-migration.shexecutable | script | 4.5 KB |
| templates/d1-worker-queries.ts | script | 15.9 KB |
| templates/schema-example.sql | other | 8.0 KB |
templates/d1-setup-migration.sh
View on GitHub#!/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
('alice@example.com', 'alice', unixepoch()),
('bob@example.com', 'bob', unixepoch()),
('charlie@example.com', '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("alice@example.com")
.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 "========================================="
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| deprecation-and-migrationaddyosmani/agent-skillsManages deprecation and migration. Use when removing old systems, APIs, or features. Use when migrating users from one implementation to another. Use when migrating a database schema in production, such as renaming or dropping a column without downtime (expand/contract). Use when deciding whether to maintain or sunset existing code. | Stars: 119k | Updated | Claude CodeCodex | Verified | Install |
| performance-optimizationaddyosmani/agent-skillsOptimizes application performance across frontend, backend, queries, and databases. Use when performance requirements exist, when you suspect performance regressions, when Core Web Vitals or load times need improvement, when N+1 query patterns need fixing, or when profiling reveals bottlenecks. | Stars: 119k | Updated | Claude CodeCodex | Verified | Install |
| agent-cost-reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json. | Stars: 118k | Updated | Claude CodeCodex | Verified | Install |
| agent-cost-reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json. | Stars: 118k | Updated | Claude CodeCodex | Verified | Install |