You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/durable-objects/best-practices/access-durable-objects-storage.mdx
+24-21Lines changed: 24 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,10 +14,28 @@ A Durable Object's [in-memory state](/durable-objects/reference/in-memory-state/
14
14
15
15
## Access storage
16
16
17
-
By default, a <GlossaryTooltipterm="Durable Object class">Durable Object class</GlossaryTooltip> leverages a SQLite storage backend.
17
+
We recommend all new <GlossaryTooltipterm="Durable Object class">Durable Object classes</GlossaryTooltip> use the SQLite storage backend. Key-value storage backend remains for backwards compatibility, and a migration path from key-value storage to SQL storage for existing Durable Object classes will be offered in the future.
18
18
19
19
[Storage API methods](/durable-objects/api/storage-api/#methods) are available on `ctx.storage` parameter passed to the Durable Object constructor. Storage API has SQL APIs and key-value APIs. Only Durable Object classes with a SQLite storage backend can access SQL API.
20
20
21
+
### Wrangler configuration for SQLite-backed Durable Objects
22
+
23
+
Use `new_sqlite_classes` on the migration in your Worker's Wrangler file:
24
+
25
+
<WranglerConfig>
26
+
27
+
```toml
28
+
[[migrations]]
29
+
tag = "v1"# Should be unique for each entry
30
+
new_sqlite_classes = ["MyDurableObject"] # Array of new classes
31
+
```
32
+
33
+
</WranglerConfig>
34
+
35
+
[SQL API](/durable-objects/api/sql-storage/#exec) is available on `ctx.storage.sql` parameter passed to the Durable Object constructor.
36
+
37
+
### Initialize instance variables from storage
38
+
21
39
A common pattern is to initialize a Durable Object from [persistent storage](/durable-objects/api/storage-api/) and set instance variables the first time it is accessed. Since future accesses are routed to the same Durable Object, it is then possible to return any initialized values without making further calls to persistent storage.
22
40
23
41
```ts
@@ -42,38 +60,23 @@ export class Counter extends DurableObject {
42
60
}
43
61
}
44
62
```
63
+
45
64
### Remove a Durable Object's storage
46
65
47
66
A Durable Object fully ceases to exist if, when it shuts down, its storage is empty. If you never write to a Durable Object's storage at all (including setting <GlossaryTooltipterm="alarm">alarms</GlossaryTooltip>), then storage remains empty, and so the Durable Object will no longer exist once it shuts down.
48
67
49
68
However if you ever write using [Storage API](/durable-objects/api/storage-api/), including setting alarms, then you must explicitly call [`storage.deleteAll()`](/durable-objects/api/storage-api/#deleteall) to empty storage. It is not sufficient to simply delete the specific data that you wrote, such as deleting a key or dropping a table, as some metadata may remain. The only way to remove all storage is to call `deleteAll()`. Calling `deleteAll()` ensures that a Durable Object will not be billed for storage.
50
69
51
-
## Wrangler configuration for SQLite Durable Objects
52
-
53
-
Use `new_sqlite_classes` on the migration in your Worker's Wrangler file:
54
-
55
-
<WranglerConfig>
56
-
57
-
```toml
58
-
[[migrations]]
59
-
tag = "v1"# Should be unique for each entry
60
-
new_sqlite_classes = ["MyDurableObject"] # Array of new classes
61
-
```
62
-
63
-
</WranglerConfig>
64
-
65
-
[SQL API](/durable-objects/api/sql-storage/#exec) is available on `ctx.storage.sql` parameter passed to the Durable Object constructor.
66
-
67
-
## Examples
70
+
## SQL API Examples
68
71
69
72
<Renderfile="durable-objects-sql" />
70
73
71
-
<Renderfile="durable-objects-vs-d1" />
72
-
73
-
## Index for SQLite Durable Objects
74
+
## Indexes in SQLite
74
75
75
76
Creating indexes for your most queried tables and filtered columns reduces how much data is scanned and improves query performance at the same time. If you have a read-heavy workload (most common), this can be particularly advantageous. Writing to columns referenced in an index will add at least one (1) additional row written to account for updating the index, but this is typically offset by the reduction in rows read due to the benefits of an index.
76
77
78
+
<Renderfile="durable-objects-vs-d1" />
79
+
77
80
## Related resources
78
81
79
82
*[Zero-latency SQLite storage in every Durable Object blog post](https://blog.cloudflare.com/sqlite-in-durable-objects)
Create collaborative applications, real-time chat, multiplayer games and more without needing to coordinate state or manage infrastructure.
16
+
Create AI agents, collaborative applications, real-time interactions like chat or multiplayer games, and more without needing to coordinate state or manage infrastructure.
17
17
</Description>
18
18
19
19
<Plantype="workers-all" />
20
20
21
21
Durable Objects provide a building block for stateful applications and distributed systems.
22
22
23
-
Use Durable Objects to build applications that need coordination among multiple clients, like collaborative editing tools, interactive chat, multiplayer games, and deep distributed systems, without requiring you to build serialization and coordination primitives on your own.
23
+
Use Durable Objects to build applications that need coordination among multiple clients, like collaborative editing tools, interactive chat, multiplayer games, live notifications, and deep distributed systems, without requiring you to build serialization and coordination primitives on your own.
SQLite-backed Durable Objects have now reached GA.
28
+
SQLite-backed Durable Objects are now available on the Workers Free plan with these [limits](/durable-objects/platform/pricing/).
29
29
30
-
You can use SQLite-backed Durable Objects with the Workers Free plan.
30
+
SQLite storage and [SQL API](/durable-objects/api/sql-storage/) have moved from beta to general availability. New Durable Object classes should use wrangler configuration for [SQLite storage](/durable-objects/best-practices/access-durable-objects-storage/#wrangler-configuration-for-sqlite-durable-objects).
Copy file name to clipboardExpand all lines: src/content/partials/durable-objects/storage-intro-text.mdx
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,8 +4,8 @@
4
4
5
5
The Durable Object Storage API comes with several methods, including key-value (KV) API, SQL API, and point-in-time-recovery (PITR) API.
6
6
7
-
- Durable Object classes with the default, [SQLite storage backend](/durable-objects/best-practices/access-durable-objects-storage/#sqlite-storage-backend) can use KV API, SQL API, and PITR API. KV API methods like `get()`, `put()`, `delete()`, or `list()` store data in a hidden SQLite table.
7
+
- Durable Object classes with the recommended, [SQLite storage backend](/durable-objects/best-practices/access-durable-objects-storage/#sqlite-storage-backend) can use KV API, SQL API, and PITR API. KV API methods like `get()`, `put()`, `delete()`, or `list()` store data in a hidden SQLite table.
8
8
- Specifically for Durable Object classes with SQLite storage backend, KV operations which were previously asynchronous (for example, [`get`](/durable-objects/api/storage-api/#get), [`put`](/durable-objects/api/storage-api/#put), [`delete`](/durable-objects/api/storage-api/#delete), [`deleteAll`](/durable-objects/api/storage-api/#deleteall), [`list`](/durable-objects/api/storage-api/#list)) are synchronous, even though they return promises. These methods will have completed their operations before they return the promise.
9
-
- Durable Object classes with the key-value storage backend can use KV API.
9
+
- Durable Object classes with the key-value storage backend can only use KV API.
10
10
11
11
Each method is implicitly wrapped inside a transaction, such that its results are atomic and isolated from all other storage operations, even when accessing multiple key-value pairs.
0 commit comments