Setting the file. One moment.
Chapter 04 · Cloudflare Deploy
Subchapter 4.86
references/do-storage/README.mdMarkdown3 KBView on GitHub
Persistent storage API for Durable Objects with SQLite and KV backends, PITR, and automatic concurrency control.
DO Storage provides:
Use cases: Stateful coordination, real-time collaboration, counters, sessions, rate limiters
Billing: Charged by request, GB-month storage, and rowsRead/rowsWritten for SQL operations
export class Counter extends DurableObject {
sql: SqlStorage;
constructor(ctx: DurableObjectState, env: Env) {
super(ctx, env);
this.sql = ctx.storage.sql;
this.sql.exec('CREATE TABLE IF NOT EXISTS data(key TEXT PRIMARY KEY, value INTEGER)');
}
async increment(): Promise<number> {
const result = this.sql.exec(
'INSERT INTO data VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = value + 1 RETURNING value',
'counter', 1
).one();
return result?.value || 1;
}
}| Backend | Create Method | APIs | PITR |
|---|---|---|---|
| SQLite (recommended) | new_sqlite_classes | SQL + sync KV + async KV | ✅ |
| KV (legacy) | new_classes | async KV only | ❌ |
ctx.storage.sql): Full SQLite with extensions (FTS5, JSON, math)ctx.storage.kv): Synchronous key-value (SQLite only)ctx.storage): Asynchronous key-value (both backends)transactionSync(), transaction())getBookmarkForTime(), onNextSessionRestoreBookmark())setAlarm(), alarm() handler)New to DO storage: configuration.md → api.md → patterns.md → gotchas.md
Building features: patterns.md → api.md → gotchas.md
Debugging issues: gotchas.md → api.md
Writing tests: testing.md