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/configuration.md
1# DO Storage Configuration23## SQLite-backed (Recommended)45**wrangler.jsonc:**6```jsonc7{8"migrations": [9{10"tag": "v1",11"new_sqlite_classes": ["Counter", "Session", "RateLimiter"]12}13]14}15```1617**Migration lifecycle:** Migrations run once per deployment. Existing DO instances get new storage backend on next invocation. Renaming/removing classes requires `renamed_classes` or `deleted_classes` entries.1819## KV-backed (Legacy)2021**wrangler.jsonc:**22```jsonc23{24"migrations": [25{26"tag": "v1",27"new_classes": ["OldCounter"]28}29]30}31```3233## TypeScript Setup3435```typescript36export class MyDurableObject extends DurableObject {37sql: SqlStorage;3839constructor(ctx: DurableObjectState, env: Env) {40super(ctx, env);41this.sql = ctx.storage.sql;4243// Initialize schema44this.sql.exec(`45CREATE TABLE IF NOT EXISTS users(46id INTEGER PRIMARY KEY,47name TEXT NOT NULL,48email TEXT UNIQUE49);50`);51}52}5354// Binding55interface Env {56MY_DO: DurableObjectNamespace;57}5859export default {60async fetch(request: Request, env: Env): Promise<Response> {61const id = env.MY_DO.idFromName('singleton');62const stub = env.MY_DO.get(id);6364// Modern RPC: call methods directly (recommended)65const result = await stub.someMethod();66return Response.json(result);6768// Legacy: forward request (still works)69// return stub.fetch(request);70}71}72```7374## CPU Limits7576```jsonc77{78"limits": {79"cpu_ms": 300000 // 5 minutes (default 30s)80}81}82```8384## Location Control8586```typescript87// Jurisdiction (GDPR/FedRAMP)88const euNamespace = env.MY_DO.jurisdiction("eu");89const id = euNamespace.newUniqueId();90const stub = euNamespace.get(id);9192// Location hint (best effort)93const stub = env.MY_DO.get(id, { locationHint: "enam" });94// Hints: wnam, enam, sam, weur, eeur, apac, oc, afr, me95```9697## Initialization9899```typescript100export class Counter extends DurableObject {101value: number;102103constructor(ctx: DurableObjectState, env: Env) {104super(ctx, env);105106// Block concurrent requests during init107ctx.blockConcurrencyWhile(async () => {108this.value = (await ctx.storage.get("value")) || 0;109});110}111}112```113