cloudflare-d1
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
What this skill does
# 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 stateme
Related in Backend & APIs
jfrog
IncludedInteract with the JFrog Platform via the JFrog CLI and REST/GraphQL APIs. Use this skill when the user wants to manage Artifactory repositories, upload or download artifacts, manage builds, configure permissions, manage users and groups, work with access tokens, configure JFrog CLI servers, search artifacts, manage properties, set up replication, manage JFrog Projects, run security audits or scans, look up CVE details, query exposures scan results from JFrog Advanced Security, manage release bundles and lifecycle operations, aggregate or export platform data, or perform any JFrog Platform administration task. Also use when the user mentions jf, jfrog, artifactory, xray, distribution, evidence, apptrust, onemodel, graphql, workers, mission control, curation, advanced security, exposures, or any JFrog product name.
cupynumeric-migration-readiness
IncludedPre-migration readiness assessor for porting NumPy to cuPyNumeric. Use BEFORE substantial porting work begins when the user asks whether code will scale on GPU, whether they should migrate to cuPyNumeric, which NumPy patterns transfer cleanly, what must be refactored before porting, or mentions pre-port assessment, scaling analysis, or refactor planning. Inspect the user's source code, look up NumPy usage, cross-reference the cuPyNumeric API support manifest, and distinguish distributed-scaling-friendly patterns from blockers such as unsupported APIs, scalar synchronization, host round-trips, Python/object-heavy control flow, shape/data-dependent branching, and in-place mutation hazards. Produce a verdict of READY, LIGHT REFACTOR, SIGNIFICANT REFACTOR, or NOT RECOMMENDED, with concrete refactor pointers.
alibabacloud-data-agent-skill
IncludedInvoke Alibaba Cloud Apsara Data Agent for Analytics via CLI to perform natural language-driven data analysis on enterprise databases. Data Agent for Analytics is an intelligent data analysis agent developed by Alibaba Cloud Database team for enterprise users. It automatically completes requirement analysis, data understanding, analysis insights, and report generation based on natural language descriptions. This tool supports: discovering data resources (instances/databases/tables) managed in DMS, initiating query or deep analysis sessions, real-time progress tracking, and retrieving analysis conclusions and generated reports. Use this Skill when users need to query databases, analyze data trends, generate data reports, ask questions in natural language, or mention "Data Agent", "data analysis", "database query", "SQL analysis", "data insights".
token-optimizer
IncludedReduce OpenClaw token usage and API costs through smart model routing, heartbeat optimization, budget tracking, and native 2026.2.15 features (session pruning, bootstrap size limits, cache TTL alignment). Use when token costs are high, API rate limits are being hit, or hosting multiple agents at scale. The 4 executable scripts (context_optimizer, model_router, heartbeat_optimizer, token_tracker) are local-only — no network requests, no subprocess calls, no system modifications. Reference files (PROVIDERS.md, config-patches.json) document optional multi-provider strategies that require external API keys and network access if you choose to use them. See SECURITY.md for full breakdown.
resend-cli
IncludedUse this skill when the task is specifically about operating Resend from an AI agent, terminal session, or CI job via the official resend CLI: installing/authenticating the CLI, sending/listing/updating/cancelling emails, batch sends, domains and DNS, webhooks and local listeners, inbound receiving, contacts, topics, segments, broadcasts, templates, API keys, profiles, or debugging Resend CLI/API failures. Trigger on mentions of Resend CLI, `resend`, `resend doctor`, `resend emails send`, `resend domains`, `resend webhooks listen`, `resend emails receiving`, or agent-friendly terminal automation.
alibabacloud-odps-maxframe-coding
IncludedUse this skill for MaxFrame SDK development and documentation navigation on Alibaba Cloud MaxCompute (ODPS). Helps answer MaxFrame API, concept, official example, and supported pandas API questions; create data processing programs; read/write MaxCompute tables; debug jobs (remote or local); and build custom DPE runtime images. Trigger when users mention MaxFrame, MaxCompute with MaxFrame, ODPS table processing, DPE runtime, MaxFrame docs/examples, DataFrame/Tensor operations, or GPU runtime setup. Works for both English and Chinese queries about Alibaba Cloud data processing with MaxFrame.