Cloudflare D1 logo

Cloudflare D1

Community
secondsky
cloudflare-d1

Cloudflare D1 serverless SQLite on edge. Use for databases, migrations, bindings, or encountering D1_ERROR, statement too long, too many requests queued errors.

Overview

Publishersecondsky
Repositoryclaude-skills
Skill namecloudflare-d1
Stars
219
Forks
31
Bundled files
10
LicenseMIT
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 10 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by secondsky on GitHub. Read the source before you install it.

Installation

Install the Cloudflare D1 AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/secondsky/claude-skills.git /tmp/claude-skills
mkdir -p .claude/skills
cp -r /tmp/claude-skills/plugins/cloudflare-d1/skills/cloudflare-d1 .claude/skills/cloudflare-d1
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Cloudflare D1 in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Cloudflare D1 on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Cloudflare D1 is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

Cloudflare D1 Database

Status: Production Ready ✅ | Last Verified: 2025-01-15

Table of Contents

  1. What Is D1?
  2. Quick Start
  3. Critical Rules
  4. D1 API Methods
  5. Top 5 Use Cases
  6. Migrations Best Practices
  7. Common Patterns
  8. SQLite Type Affinity
  9. Top 5 Errors Prevented

What Is D1?

Cloudflare D1 is serverless SQLite on the edge:

  • SQL database without servers
  • Global distribution
  • Zero cold starts
  • Standard SQLite syntax
  • Read replication for global performance

🆕 New in 2025

D1 received major updates throughout 2025:

Performance (January 2025)

  • 40-60% latency reduction globally (P50 query times)
  • Optimized SQLite engine for edge execution
  • Reduced cold start impact for databases <100 MB

Reliability (September 2025)

  • Automatic query retries: Read queries retry up to 2x on transient failures
  • Transparent to application code (logged in wrangler tail)

Scalability (April 2025)

  • Read Replication (Public Beta): Deploy read replicas globally
  • Up to 2x read throughput for read-heavy workloads
  • Sessions API for read-write separation

Compliance (November 2025)

  • Data Localization: Specify EU/US jurisdiction for GDPR/data sovereignty
  • Configure via --jurisdiction flag or wrangler.jsonc

⚠️ Breaking Change (February 10, 2025)

  • Free tier hard limits enforced: 10 DBs, 500 MB each, 50 queries/invocation
  • Exceeding limits = 429 errors (previously warnings only)
  • Action: Review usage with wrangler d1 list and upgrade if needed

Full details: Load references/2025-features.md


Quick Start (5 Minutes)

1. Create Database

bash
bunx wrangler d1 create my-database

Save the database_id from output!

2. Configure Binding

Add to wrangler.jsonc:

jsonc
{
  "name": "my-worker",
  "main": "src/index.ts",
  "compatibility_date": "2025-10-11",
  "d1_databases": [
    {
      "binding": "DB",                    // env.DB
      "database_name": "my-database",
      "database_id": "<UUID>",
      "preview_database_id": "local-db"
    }
  ]
}

3. Create Migration

bash
bunx wrangler d1 migrations create my-database create_users

Edit migrations/0001_create_users.sql:

sql
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
);

CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);

PRAGMA optimize;

4. Apply Migration

bash
# Local
bunx wrangler d1 migrations apply my-database --local

# Production
bunx wrangler d1 migrations apply my-database --remote

5. Query from Worker

typescript
import { Hono } from 'hono';

type Bindings = {
  DB: D1Database;
};

const app = new Hono<{ Bindings: Bindings }>();

app.get('/users/:email', async (c) => {
  const { results } = await c.env.DB.prepare(
    'SELECT * FROM users WHERE email = ?'
  )
    .bind(c.req.param('email'))
    .all();

  return c.json(results);
});

export default app;

Load references/setup-guide.md for complete walkthrough.


Critical Rules

Always Do ✅

  1. Use prepared statements with .bind() (never string concatenation)
  2. Create indexes for WHERE/JOIN/ORDER BY columns
  3. Use migrations for schema changes (never manual SQL)
  4. Batch queries for multiple operations (.batch())
  5. Run PRAGMA optimize after schema changes
  6. Handle errors explicitly (try/catch)
  7. Use INTEGER for timestamps (Date.now())
  8. Test locally before deploying migrations
  9. Use read replicas for global read performance
  10. Validate input before SQL queries

Never Do ❌

  1. Never concatenate user input into SQL
  2. Never commit database_id to public repos
  3. Never skip migrations for schema changes
  4. Never use VARCHAR (use TEXT instead)
  5. Never skip indexes for filtered columns
  6. Never ignore SQLite type affinity rules
  7. **Never use SELECT *** without LIMIT
  8. Never run migrations without testing locally
  9. Never exceed 1MB per row
  10. Never use DATETIME (use INTEGER for timestamps)

D1 API Methods

prepare() - Execute Queries

typescript
// Single result
const { results } = await env.DB.prepare(
  'SELECT * FROM users WHERE email = ?'
)
  .bind(email)
  .all();

// First result only
const user = await env.DB.prepare(
  'SELECT * FROM users WHERE user_id = ?'
)
  .bind(userId)
  .first();

// Raw results (faster)
const { results } = await env.DB.prepare(
  'SELECT username FROM users'
)
  .raw();  // Returns arrays instead of objects

batch() - Multiple Queries

typescript
const results = await env.DB.batch([
  env.DB.prepare('INSERT INTO users (email, username, created_at) VALUES (?, ?, ?)')
    .bind('user1@example.com', 'user1', Date.now()),
  env.DB.prepare('INSERT INTO users (email, username, created_at) VALUES (?, ?, ?)')
    .bind('user2@example.com', 'user2', Date.now()),
  env.DB.prepare('SELECT COUNT(*) as count FROM users')
]);

console.log('Users count:', results[2].results[0].count);

All queries execute in single transaction (all succeed or all fail).

exec() - Run SQL String

typescript
// For migrations/setup only
await env.DB.exec(`
  CREATE TABLE IF NOT EXISTS users (
    user_id INTEGER PRIMARY KEY,
    email TEXT NOT NULL
  );
  CREATE INDEX idx_email ON users(email);
`);

NEVER use for queries with user input!

Load references/query-patterns.md for complete API reference.


Top 5 Use Cases

Use Case 1: User CRUD

typescript
// Create
app.post('/users', async (c) => {
  const { email, username } = await c.req.json();

  const { results } = await c.env.DB.prepare(
    'INSERT INTO users (email, username, created_at) VALUES (?, ?, ?) RETURNING *'
  )
    .bind(email, username, Date.now())
    .all();

  return c.json(results[0]);
});

// Read
app.get('/users/:id', async (c) => {
  const user = await c.env.DB.prepare(
    'SELECT * FROM users WHERE user_id = ?'
  )
    .bind(c.req.param('id'))
    .first();

  if (!user) {
    return c.json({ error: 'Not found' }, 404);
  }

  return c.json(user);
});

// Update
app.patch('/users/:id', async (c) => {
  const { username } = await c.req.json();

  await c.env.DB.prepare(
    'UPDATE users SET username = ?, updated_at = ? WHERE user_id = ?'
  )
    .bind(username, Date.now(), c.req.param('id'))
    .run();

  return c.json({ success: true });
});

// Delete
app.delete('/users/:id', async (c) => {
  await c.env.DB.prepare(
    'DELETE FROM users WHERE user_id = ?'
  )
    .bind(c.req.param('id'))
    .run();

  return c.json({ success: true });
});

Use Case 2: Batch Operations

typescript
app.post('/users/bulk', async (c) => {
  const users = await c.req.json();  // Array of users

  const statements = users.map(user =>
    c.env.DB.prepare(
      'INSERT INTO users (email, username, created_at) VALUES (?, ?, ?)'
    ).bind(user.email, user.username, Date.now())
  );

  const results = await c.env.DB.batch(statements);

  return c.json({ inserted: results.length });
});

Use Case 3: Read Replication (Global Reads)

typescript
// Configure read replica (any region)
const session = c.env.DB.withSession({
  preferredRegion: 'auto'  // or 'weur', 'wnam', 'enam', 'apac'
});

// Read from nearest replica
const { results } = await session.prepare(
  'SELECT * FROM users WHERE email = ?'
)
  .bind(email)
  .all();

// Check which region served request
console.log('Served by:', results[0].served_by_region);

Load references/read-replication.md for complete guide.

Use Case 4: Transactions with Batch

typescript
// Transfer credits between users (atomic)
const results = await c.env.DB.batch([
  c.env.DB.prepare(
    'UPDATE users SET credits = credits - ? WHERE user_id = ?'
  ).bind(amount, fromUserId),
  c.env.DB.prepare(
    'UPDATE users SET credits = credits + ? WHERE user_id = ?'
  ).bind(amount, toUserId),
  c.env.DB.prepare(
    'INSERT INTO transactions (from_user, to_user, amount, created_at) VALUES (?, ?, ?, ?)'
  ).bind(fromUserId, toUserId, amount, Date.now())
]);

// All succeed or all fail (transaction)

Use Case 5: Pagination

typescript
app.get('/users', async (c) => {
  const page = parseInt(c.req.query('page') || '1');
  const limit = 20;
  const offset = (page - 1) * limit;

  const { results } = await c.env.DB.prepare(
    'SELECT * FROM users ORDER BY created_at DESC LIMIT ? OFFSET ?'
  )
    .bind(limit, offset)
    .all();

  return c.json({
    users: results,
    page,
    limit
  });
});

Migrations Best Practices

1. Always Use Migrations

bash
bunx wrangler d1 migrations create my-database add_users_avatar

2. Make Migrations Idempotent

sql
-- ✅ GOOD: Idempotent
CREATE TABLE IF NOT EXISTS users (...);
CREATE INDEX IF NOT EXISTS idx_email ON users(email);
DROP TABLE IF EXISTS old_table;

-- ❌ BAD: Fails on re-run
CREATE TABLE users (...);
CREATE INDEX idx_email ON users(email);

3. Test Locally First

bash
bunx wrangler d1 migrations apply my-database --local
bunx wrangler d1 execute my-database --local --command "SELECT * FROM users"

4. Add PRAGMA optimize

sql
-- End of migration
PRAGMA optimize;

Load templates/schema-example.sql for complete schema template.


When to Load References

Load references/setup-guide.md when:

  • First-time D1 setup
  • Creating first database
  • Configuring bindings
  • Applying first migration

Load references/query-patterns.md when:

  • Need complete API reference
  • Complex query patterns
  • Batch operations
  • Error handling

Load references/read-replication.md when:

  • Setting up global reads
  • Need low latency worldwide
  • Understanding Sessions API
  • Sequential consistency required

Load references/best-practices.md when:

  • Optimizing query performance
  • Schema design decisions
  • Index strategies
  • Production deployment checklist

Load references/limits.md when:

  • Encountering 429 errors or quota warnings
  • Planning capacity for free vs paid tiers
  • Understanding database/query limits
  • Migrating to paid plan

Load references/metrics-analytics.md when:

  • Investigating performance issues
  • Setting up monitoring and alerts
  • Using wrangler d1 insights command
  • Analyzing query efficiency

Load references/2025-features.md when:

  • Upgrading from v2.x to v3.x
  • Enabling new features (auto-retry, jurisdiction, replication)
  • Understanding breaking changes (Feb 10, 2025 enforcement)
  • Migrating before deadlines

Interactive Tools

Agents (Autonomous diagnostics):

  • agents/d1-debugger.md: 9-phase diagnostic (config, migrations, queries, bindings, errors, limits, performance, Time Travel)
  • agents/d1-query-optimizer.md: Performance analysis (slow queries, missing indexes, optimization recommendations)

Commands (Interactive wizards):

  • commands/cloudflare-d1:setup.md: Interactive first-time setup wizard
  • commands/d1-create-migration.md: Guided migration creation with validation

Using Bundled Resources

References (references/)

  • setup-guide.md - Complete setup walkthrough
  • query-patterns.md - Complete API reference with examples
  • read-replication.md - Global read replicas setup
  • best-practices.md - Performance and optimization

Templates (templates/)

  • schema-example.sql - Complete schema with indexes
  • d1-worker-queries.ts - All query patterns in Workers
  • cloudflare-d1:setup-migration.sh - Complete setup script

Common Patterns

Error Handling

typescript
try {
  const { results } = await env.DB.prepare(
    'SELECT * FROM users WHERE email = ?'
  )
    .bind(email)
    .all();

  return c.json(results);
} catch (error) {
  console.error('D1 Error:', error);
  return c.json({ error: 'Database error' }, 500);
}

Raw Mode (Performance)

typescript
// Returns arrays instead of objects (faster)
const { results } = await env.DB.prepare(
  'SELECT user_id, email FROM users'
)
  .raw();

// results = [[1, 'user1@example.com'], [2, 'user2@example.com']]

COUNT Queries

typescript
const count = await env.DB.prepare(
  'SELECT COUNT(*) as count FROM users'
)
  .first('count');  // Get single column value

console.log('Total users:', count);

SQLite Type Affinity

D1 uses SQLite type affinity:

Declared TypeAffinity
INTEGER, INTINTEGER
TEXT, VARCHAR, CHARTEXT
REAL, FLOAT, DOUBLEREAL
BLOBBLOB
(no type)BLOB

Best practices:

  • Use INTEGER for numbers
  • Use TEXT for strings (not VARCHAR)
  • Use INTEGER for timestamps (Date.now())
  • Use BLOB for binary data

Top 5 Errors Prevented

  1. SQL Injection: Use .bind(), never string concatenation
  2. Missing Indexes: Create indexes for WHERE/JOIN columns
  3. Migration Failures: Test locally first
  4. Type Confusion: Use INTEGER for timestamps
  5. Batch Size: Limit batch to <500 statements

Load references/best-practices.md for complete error prevention.


Secure Installation

When installing D1 driver packages, follow supply chain security best practices:

  • Block post-install scriptsnpm config set ignore-scripts true (or Bun: disabled by default)
  • Cooldown period — Wait 7 days for new package versions to be vetted by the community
  • Audit before installing — Run socket package score npm <pkg> or use socket npm install <pkg> to check packages

Load the dependency-upgrade skill for full security configuration including Socket CLI integration, cooldown setup, lockfile validation, and CI enforcement.

Official Documentation


Questions? Issues?

  1. Check references/setup-guide.md for setup
  2. Review references/query-patterns.md for API reference
  3. See references/read-replication.md for global reads
  4. Load references/best-practices.md for optimization

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the Cloudflare D1 AI skill do?

Cloudflare D1 serverless SQLite on edge. Use for databases, migrations, bindings, or encountering D1_ERROR, statement too long, too many requests queued errors.

Why use Cloudflare D1 on TypingMind?

Because you install it once and use it with any model. Cloudflare D1 is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Cloudflare D1 in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/secondsky/claude-skills/tree/main/plugins/cloudflare-d1/skills/cloudflare-d1. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use Cloudflare D1?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Cloudflare D1?

As many as you like. As long as a model supports skills, you can use Cloudflare D1 with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Cloudflare D1 AI skill free?

Yes. It is published on GitHub by secondsky under the MIT license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇