Skip to content

secondsky/claude-skills / plugins/cloudflare-durable-objects/skills/cloudflare-durable-objects

cloudflare-durable-objects

Verified

Cloudflare Durable Objects for stateful coordination and real-time apps. Use for chat, multiplayer games, WebSocket hibernation, or encountering class export, migration, alarm errors.

FileKindSize
SKILL.mdskill md21.6 KB
references/advanced-sql-patterns.mdmarkdown15.8 KB
references/alarms-api.mdmarkdown6.5 KB
references/best-practices.mdmarkdown8.5 KB
references/common-patterns.mdmarkdown16.7 KB
references/data-modeling.mdmarkdown16.9 KB
references/error-codes.mdmarkdown7.7 KB
references/gradual-deployments.mdmarkdown13.9 KB
references/migration-cheatsheet.mdmarkdown3.2 KB
references/migrations-guide.mdmarkdown7.3 KB
references/monitoring-debugging.mdmarkdown18.2 KB
references/performance-optimization.mdmarkdown16.2 KB
references/rpc-metadata.mdmarkdown16.9 KB
references/rpc-patterns.mdmarkdown6.5 KB
references/security-best-practices.mdmarkdown15.9 KB
references/state-api-reference.mdmarkdown6.0 KB
references/stubs-routing.mdmarkdown10.6 KB
references/top-errors.mdmarkdown10.2 KB
references/typescript-config.mdmarkdown8.3 KB
references/vitest-testing.mdmarkdown18.1 KB
references/websocket-hibernation.mdmarkdown6.3 KB
references/wrangler-commands.mdmarkdown3.4 KB
scripts/check-versions.shexecutablescript593 B
scripts/migration-generator.shexecutablescript13.4 KB
scripts/setup-vitest-do.shexecutablescript10.2 KB
scripts/validate-do-config.shexecutablescript10.0 KB
templates/alarms-api-do.tsscript6.1 KB
templates/basic-do.tsscript2.9 KB
templates/gradual-deployment-config.jsoncother10.4 KB
templates/location-hints.tsscript6.8 KB
templates/multi-do-coordination.tsscript7.8 KB
templates/package.jsonmanifest384 B
templates/rpc-metadata-do.tsscript10.8 KB
templates/rpc-vs-fetch.tsscript6.0 KB
templates/state-api-patterns.tsscript5.8 KB
templates/ttl-cleanup-do.tsscript17.6 KB
templates/vitest-do-test.tsscript12.3 KB
templates/websocket-hibernation-do.tsscript6.0 KB
templates/wrangler-do-config.jsoncother2.0 KB

templates/state-api-patterns.ts

View on GitHub
/**
 * State API Patterns Example
 *
 * Demonstrates:
 * - SQL API (SQLite backend)
 * - Key-Value API (available on both SQLite and KV backends)
 * - Transactions
 * - Combining SQL and KV storage
 */

import { DurableObject, DurableObjectState, SqlStorage } from 'cloudflare:workers';

interface Env {
  STORAGE_EXAMPLE: DurableObjectNamespace<StorageExample>;
}

export class StorageExample extends DurableObject<Env> {
  sql: SqlStorage;

  constructor(ctx: DurableObjectState, env: Env) {
    super(ctx, env);

    // Access SQL storage
    this.sql = ctx.storage.sql;

    // Create tables on first run
    this.sql.exec(`
      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 TABLE IF NOT EXISTS messages (
        message_id INTEGER PRIMARY KEY AUTOINCREMENT,
        user_id INTEGER NOT NULL,
        text TEXT NOT NULL,
        created_at INTEGER NOT NULL,
        FOREIGN KEY (user_id) REFERENCES users(user_id)
      );

      CREATE INDEX IF NOT EXISTS idx_messages_user_id ON messages(user_id);
      CREATE INDEX IF NOT EXISTS idx_messages_created_at ON messages(created_at);
    `);
  }

  /**
   * SQL API Example: Insert with RETURNING
   */
  async createUser(email: string, username: string): Promise<number> {
    const cursor = this.sql.exec(
      'INSERT INTO users (email, username, created_at) VALUES (?, ?, ?) RETURNING user_id',
      email,
      username,
      Date.now()
    );

    const row = cursor.one<{ user_id: number }>();
    return row.user_id;
  }

  /**
   * SQL API Example: Query with parameters
   */
  async getUserByEmail(email: string): Promise<any | null> {
    const cursor = this.sql.exec(
      'SELECT * FROM users WHERE email = ?',
      email
    );

    return cursor.one({ allowNone: true });
  }

  /**
   * SQL API Example: Query multiple rows
   */
  async getRecentMessages(limit: number = 50): Promise<any[]> {
    const cursor = this.sql.exec(
      `SELECT m.*, u.username
       FROM messages m
       JOIN users u ON m.user_id = u.user_id
       ORDER BY m.created_at DESC
       LIMIT ?`,
      limit
    );

    return cursor.toArray();
  }

  /**
   * SQL API Example: Transaction (synchronous)
   */
  async createUserWithMessage(email: string, username: string, messageText: string): Promise<void> {
    this.ctx.storage.transactionSync(() => {
      // Insert user
      const userCursor = this.sql.exec(
        'INSERT INTO users (email, username, created_at) VALUES (?, ?, ?) RETURNING user_id',
        email,
        username,
        Date.now()
      );
      const { user_id } = userCursor.one<{ user_id: number }>();

      // Insert message
      this.sql.exec(
        'INSERT INTO messages (user_id, text, created_at) VALUES (?, ?, ?)',
        user_id,
        messageText,
        Date.now()
      );

      // All or nothing - if either fails, both are rolled back
    });
  }

  /**
   * SQL API Example: Iterate cursor
   */
  async getAllUsers(): Promise<string[]> {
    const cursor = this.sql.exec('SELECT username FROM users');

    const usernames: string[] = [];
    for (const row of cursor) {
      usernames.push(row.username as string);
    }

    return usernames;
  }

  /**
   * Key-Value API Example: Get/Put single value
   */
  async setConfig(key: string, value: any): Promise<void> {
    await this.ctx.storage.put(`config:${key}`, value);
  }

  async getConfig(key: string): Promise<any> {
    return await this.ctx.storage.get(`config:${key}`);
  }

  /**
   * Key-Value API Example: Get/Put multiple values
   */
  async setConfigs(configs: Record<string, any>): Promise<void> {
    const entries: Record<string, any> = {};

    for (const [key, value] of Object.entries(configs)) {
      entries[`config:${key}`] = value;
    }

    await this.ctx.storage.put(entries);
  }

  async getConfigs(): Promise<Record<string, any>> {
    const map = await this.ctx.storage.list({ prefix: 'config:' });

    const configs: Record<string, any> = {};
    for (const [key, value] of map.entries()) {
      const configKey = key.replace('config:', '');
      configs[configKey] = value;
    }

    return configs;
  }

  /**
   * Key-Value API Example: Delete
   */
  async deleteConfig(key: string): Promise<void> {
    await this.ctx.storage.delete(`config:${key}`);
  }

  /**
   * Key-Value API Example: List with pagination
   */
  async listKeys(prefix: string, limit: number = 100): Promise<string[]> {
    const map = await this.ctx.storage.list({ prefix, limit });
    return Array.from(map.keys());
  }

  /**
   * Key-Value API Example: Async transaction
   */
  async updateMultipleConfigs(updates: Record<string, any>): Promise<void> {
    await this.ctx.storage.transaction(async (txn) => {
      for (const [key, value] of Object.entries(updates)) {
        await txn.put(`config:${key}`, value);
      }
      // All or nothing
    });
  }

  /**
   * Combining SQL and KV: Mixed storage patterns
   */
  async recordUserActivity(userId: number, activity: string): Promise<void> {
    // Store structured data in SQL
    this.sql.exec(
      'UPDATE users SET last_activity = ? WHERE user_id = ?',
      Date.now(),
      userId
    );

    // Store ephemeral data in KV (faster access)
    await this.ctx.storage.put(`activity:${userId}`, {
      type: activity,
      timestamp: Date.now(),
    });
  }

  /**
   * Delete all storage (DO will cease to exist after shutdown)
   */
  async deleteAllStorage(): Promise<void> {
    // Delete alarm first (if set)
    await this.ctx.storage.deleteAlarm();

    // Delete all storage (atomic on SQLite backend)
    await this.ctx.storage.deleteAll();

    // After this, DO will not exist once it shuts down
  }
}

// CRITICAL: Export the class
export default StorageExample;
slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 197kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 194kUpdated Claude CodeCodexVerifiedInstall