Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Cloudflare platform skill covering Workers, D1, R2, KV, AI, Durable Objects, and security.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/do-storage/README.md
1# Cloudflare Durable Objects Storage23Persistent storage API for Durable Objects with SQLite and KV backends, PITR, and automatic concurrency control.45## Overview67DO Storage provides:8- SQLite-backed (recommended) or KV-backed9- SQL API + synchronous/async KV APIs10- Automatic input/output gates (race-free)11- 30-day point-in-time recovery (PITR)12- Transactions and alarms1314**Use cases:** Stateful coordination, real-time collaboration, counters, sessions, rate limiters1516**Billing:** Charged by request, GB-month storage, and rowsRead/rowsWritten for SQL operations1718## Quick Start1920```typescript21export class Counter extends DurableObject {22sql: SqlStorage;2324constructor(ctx: DurableObjectState, env: Env) {25super(ctx, env);26this.sql = ctx.storage.sql;27this.sql.exec('CREATE TABLE IF NOT EXISTS data(key TEXT PRIMARY KEY, value INTEGER)');28}2930async increment(): Promise<number> {31const result = this.sql.exec(32'INSERT INTO data VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = value + 1 RETURNING value',33'counter', 134).one();35return result?.value || 1;36}37}38```3940## Storage Backends4142| Backend | Create Method | APIs | PITR |43|---------|---------------|------|------|44| SQLite (recommended) | `new_sqlite_classes` | SQL + sync KV + async KV | ✅ |45| KV (legacy) | `new_classes` | async KV only | ❌ |4647## Core APIs4849- **SQL API** (`ctx.storage.sql`): Full SQLite with extensions (FTS5, JSON, math)50- **Sync KV** (`ctx.storage.kv`): Synchronous key-value (SQLite only)51- **Async KV** (`ctx.storage`): Asynchronous key-value (both backends)52- **Transactions** (`transactionSync()`, `transaction()`)53- **PITR** (`getBookmarkForTime()`, `onNextSessionRestoreBookmark()`)54- **Alarms** (`setAlarm()`, `alarm()` handler)5556## Reading Order5758**New to DO storage:** configuration.md → api.md → patterns.md → gotchas.md59**Building features:** patterns.md → api.md → gotchas.md60**Debugging issues:** gotchas.md → api.md61**Writing tests:** testing.md6263## In This Reference6465- [configuration.md](./configuration.md) - wrangler.jsonc migrations, SQLite vs KV setup, RPC binding66- [api.md](./api.md) - SQL exec/cursors, KV methods, storage options, transactions, alarms, PITR67- [patterns.md](./patterns.md) - Schema migrations, caching, rate limiting, batch processing, parent-child coordination68- [gotchas.md](./gotchas.md) - Concurrency gates, INTEGER precision, transaction rules, SQL limits69- [testing.md](./testing.md) - vitest-pool-workers setup, testing DOs with SQL/alarms/PITR7071## See Also7273- [durable-objects](../durable-objects/) - DO fundamentals and coordination patterns74- [workers](../workers/) - Worker runtime for DO stubs75- [d1](../d1/) - Shared database alternative to per-DO storage76