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/durable-objects/configuration.md
1# Durable Objects Configuration23## Basic Setup45```jsonc6{7"name": "my-worker",8"main": "src/index.ts",9"compatibility_date": "2025-01-01", // Use latest; ≥2024-04-03 for RPC10"durable_objects": {11"bindings": [12{13"name": "MY_DO", // Env binding name14"class_name": "MyDO" // Class exported from this worker15},16{17"name": "EXTERNAL", // Access DO from another worker18"class_name": "ExternalDO",19"script_name": "other-worker"20}21]22},23"migrations": [24{ "tag": "v1", "new_sqlite_classes": ["MyDO"] } // Prefer SQLite25]26}27```2829## Binding Options3031```jsonc32{33"name": "BINDING_NAME",34"class_name": "ClassName",35"script_name": "other-worker", // Optional: external DO36"environment": "production" // Optional: isolate by env37}38```3940## Jurisdiction (Data Locality)4142Specify jurisdiction at ID creation for data residency compliance:4344```typescript45// EU data residency46const id = env.MY_DO.idFromName("user:123", { jurisdiction: "eu" })4748// Available jurisdictions49const jurisdictions = ["eu", "fedramp"] // More may be added5051// All operations on this DO stay within jurisdiction52const stub = env.MY_DO.get(id)53await stub.someMethod() // Data stays in EU54```5556**Key points:**57- Set at ID creation time, immutable afterward58- DO instance physically located within jurisdiction59- Storage and compute guaranteed within boundary60- Use for GDPR, FedRAMP, other compliance requirements61- No cross-jurisdiction access (requests fail if DO in different jurisdiction)6263## Migrations6465```jsonc66{67"migrations": [68{ "tag": "v1", "new_sqlite_classes": ["MyDO"] }, // Create SQLite (recommended)69// { "tag": "v1", "new_classes": ["MyDO"] }, // Create KV (paid only)70{ "tag": "v2", "renamed_classes": [{ "from": "Old", "to": "New" }] },71{ "tag": "v3", "transferred_classes": [{ "from": "Src", "from_script": "old", "to": "Dest" }] },72{ "tag": "v4", "deleted_classes": ["Obsolete"] } // Destroys ALL data!73]74}75```7677**Migration rules:**78- Tags must be unique and sequential (v1, v2, v3...)79- No rollback supported (test with `--dry-run` first)80- Auto-applied on deploy81- `new_sqlite_classes` recommended over `new_classes` (SQLite vs KV)82- `deleted_classes` immediately destroys ALL data (irreversible)8384## Environment Isolation8586Separate DO namespaces per environment (staging/production have distinct object instances):8788```jsonc89{90"durable_objects": {91"bindings": [{ "name": "MY_DO", "class_name": "MyDO" }]92},93"env": {94"production": {95"durable_objects": {96"bindings": [97{ "name": "MY_DO", "class_name": "MyDO", "environment": "production" }98]99}100}101}102}103```104105Deploy: `npx wrangler deploy --env production`106107## Limits & Settings108109```jsonc110{111"limits": {112"cpu_ms": 300000 // Max CPU time: 30s default, 300s max113}114}115```116117See [Gotchas](./gotchas.md) for complete limits table.118119## Types120121```typescript122import { DurableObject } from "cloudflare:workers";123124interface Env {125MY_DO: DurableObjectNamespace<MyDO>;126}127128export class MyDO extends DurableObject<Env> {}129130type DurableObjectNamespace<T> = {131newUniqueId(options?: { jurisdiction?: string }): DurableObjectId;132idFromName(name: string): DurableObjectId;133idFromString(id: string): DurableObjectId;134get(id: DurableObjectId): DurableObjectStub<T>;135};136```137138## Commands139140```bash141# Development142npx wrangler dev # Local dev143npx wrangler dev --remote # Test against production DOs144145# Deployment146npx wrangler deploy # Deploy + auto-apply migrations147npx wrangler deploy --dry-run # Validate migrations without deploying148npx wrangler deploy --env production149150# Management151npx wrangler durable-objects list # List namespaces152npx wrangler durable-objects info <namespace> <id> # Inspect specific DO153npx wrangler durable-objects delete <namespace> <id> # Delete DO (destroys data)154```155156## See Also157158- **[API](./api.md)** - DurableObjectState and lifecycle handlers159- **[Patterns](./patterns.md)** - Multi-environment patterns160- **[Gotchas](./gotchas.md)** - Migration caveats, limits161