Skip to content

Commit 42e8b88

Browse files
committed
Surfacing SQL storage API ahead of KV storage API
1 parent 405cc3d commit 42e8b88

File tree

7 files changed

+38
-53
lines changed

7 files changed

+38
-53
lines changed

src/content/docs/durable-objects/api/sql-storage.mdx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: SQL Storage
2+
title: SQL storage
33
pcx_content_type: concept
44
sidebar:
55
order: 6
@@ -8,6 +8,21 @@ sidebar:
88

99
import { Render, Type, MetaInfo, GlossaryTooltip } from "~/components";
1010

11+
The Durable Object Storage API allows <GlossaryTooltip term="Durable Object">Durable Objects</GlossaryTooltip> to access transactional and strongly consistent storage. A Durable Object's attached storage is private to its unique instance and cannot be accessed by other objects.
12+
13+
:::note[Scope of Durable Object storage]
14+
Note that Durable Object storage is scoped by individual <GlossaryTooltip term="Durable Object">Durable Objects</GlossaryTooltip>.
15+
16+
- An account can have many Durable Object <GlossaryTooltip term="namespace">namespaces</GlossaryTooltip>.
17+
- A namespace can have many Durable Objects.
18+
19+
However, storage is scoped per individual Durable Object.
20+
:::
21+
22+
Durable Objects gain access to a persistent Durable Object Storage API via the `DurableObjectStorage` interface and accessed by the `DurableObjectState::storage` property. This is frequently accessed via `this.ctx.storage` when the `ctx` parameter passed to the Durable Object constructor.
23+
24+
JavaScript is a single-threaded and event-driven programming language. This means that JavaScript runtimes, by default, allow requests to interleave with each other which can lead to concurrency bugs. The Durable Objects runtime uses a combination of <GlossaryTooltip term="input gate">input gates</GlossaryTooltip> and <GlossaryTooltip term="output gate">output gates</GlossaryTooltip> to avoid this type of concurrency bug when performing storage operations.
25+
1126
The `SqlStorage` interface encapsulates methods that modify the SQLite database embedded within a Durable Object. The `SqlStorage` interface is accessible via the `sql` property of `DurableObjectStorage` class.
1227

1328
For example, using `sql.exec()`, a user can create a table, then insert rows into the table.
@@ -47,6 +62,8 @@ Specifically for Durable Object classes with SQLite storage backend, KV operatio
4762

4863
## Methods
4964

65+
<Render file="storage-intro-text"/>
66+
5067
### `exec`
5168

5269
<code>exec(query: <Type text='string'/>, ...bindings: <Type text='any[]'/>)</code>: <Type text='SqlStorageCursor' />

src/content/docs/durable-objects/api/storage-api.mdx

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
title: Durable Object Storage
2+
title: Key-Value storage
33
pcx_content_type: concept
44
sidebar:
5-
order: 6
5+
order: 7
66
---
77

88
import {
@@ -13,21 +13,6 @@ import {
1313
TypeScriptExample,
1414
} from "~/components";
1515

16-
The Durable Object Storage API allows <GlossaryTooltip term="Durable Object">Durable Objects</GlossaryTooltip> to access transactional and strongly consistent storage. A Durable Object's attached storage is private to its unique instance and cannot be accessed by other objects.
17-
18-
:::note[Scope of Durable Object storage]
19-
Note that Durable Object storage is scoped by individual <GlossaryTooltip term="Durable Object">Durable Objects</GlossaryTooltip>.
20-
21-
- An account can have many Durable Object <GlossaryTooltip term="namespace">namespaces</GlossaryTooltip>.
22-
- A namespace can have many Durable Objects.
23-
24-
However, storage is scoped per individual Durable Object.
25-
:::
26-
27-
Durable Objects gain access to a persistent Durable Object Storage API via the `DurableObjectStorage` interface and accessed by the `DurableObjectState::storage` property. This is frequently accessed via `this.ctx.storage` when the `ctx` parameter passed to the Durable Object constructor.
28-
29-
JavaScript is a single-threaded and event-driven programming language. This means that JavaScript runtimes, by default, allow requests to interleave with each other which can lead to concurrency bugs. The Durable Objects runtime uses a combination of <GlossaryTooltip term="input gate">input gates</GlossaryTooltip> and <GlossaryTooltip term="output gate">output gates</GlossaryTooltip> to avoid this type of concurrency bug when performing storage operations.
30-
3116
The following code snippet shows you how to store and retrieve data using the Durable Object Storage API.
3217

3318
<TypeScriptExample>
@@ -51,17 +36,7 @@ export class Counter extends DurableObject {
5136

5237
## Methods
5338

54-
:::note[SQLite in Durable Objects]
55-
SQLite-backed Durable Objects can have a private, embedded SQLite database. When deploying a new Durable Object class, users can [use a SQLite storage backend](/durable-objects/features/access-sqlite-storage/#sqlite-storage-backend) to access the [SQL API](/durable-objects/api/sql-storage/#exec).
56-
57-
:::
58-
59-
The Durable Object Storage API comes with several methods, including key-value (KV) API, SQL API, and point-in-time-recovery (PITR) API.
60-
61-
- Durable Object classes with the default, key-value storage backend can use KV API.
62-
- Durable Object classes with the [SQLite storage backend](/durable-objects/features/access-sqlite-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.
63-
64-
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.
39+
<Render file="storage-intro-text"/>
6540

6641
### `get`
6742

src/content/docs/durable-objects/features/access-sqlite-storage.mdx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ A Durable Object's [in-memory state](/durable-objects/reference/in-memory-state/
1616

1717
By default, a <GlossaryTooltip term="Durable Object class">Durable Object class</GlossaryTooltip> leverages a SQLite storage backend.
1818

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 key-value APIs and SQL APIs. Only Durable Object classes with a SQLite storage backend can access SQL API.
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.
2020

2121
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.
2222

@@ -42,17 +42,15 @@ export class Counter extends DurableObject {
4242
}
4343
}
4444
```
45-
### Removing a Durable Object's storage
45+
## Remove storage
4646

4747
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 <GlossaryTooltip term="alarm">alarms</GlossaryTooltip>), then storage remains empty, and so the Durable Object will no longer exist once it shuts down.
4848

4949
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.
5050

51-
## SQLite storage backend
51+
## Wrangler configuration for SQLite Durable Objects
5252

53-
By default, Durable Objects use SQLite storage backend.
54-
55-
To allow a new Durable Object class to use SQLite storage backend, use `new_sqlite_classes` on the migration in your Worker's Wrangler file:
53+
By default, Durable Objects use SQLite storage backend. This means the Wrangler file uses `new_sqlite_classes` on the migration, by default:
5654

5755
<WranglerConfig>
5856

@@ -66,7 +64,7 @@ new_sqlite_classes = ["MyDurableObject"] # Array of new classes
6664

6765
[SQL API](/durable-objects/api/sql-storage/#exec) is available on `ctx.storage.sql` parameter passed to the Durable Object constructor.
6866

69-
### Examples
67+
## Examples
7068

7169
<Render file="durable-objects-sql" />
7270

src/content/docs/durable-objects/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { Render, CardGrid, Description, Feature, LinkTitleCard, Plan, RelatedPro
1616
Create collaborative applications, real-time chat, multiplayer games and more without needing to coordinate state or manage infrastructure.
1717
</Description>
1818

19-
<Plan type="paid" />
19+
<Plan type="workers-all" />
2020

2121
Durable Objects provide a building block for stateful applications and distributed systems.
2222

src/content/docs/durable-objects/reference/durable-objects-migrations.mdx

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -270,20 +270,5 @@ new_classes = ["DurableObjectExample"] # Array of new classes
270270
271271
You can rename the `DurableObjectExample` class to `UpdatedName` and delete an outdated `DeprecatedClass` entirely. You can create separate migrations for each operation, or combine them into a single migration as shown below. */}
272272

273-
## Enable SQLite storage backend on new Durable Object class migration
274273

275-
To allow a new Durable Object class to use a SQLite storage backend, use `new_sqlite_classes` on the migration in your Worker's `wrangler` configuration file:
276-
277-
<WranglerConfig>
278-
279-
```toml
280-
[[migrations]]
281-
tag = "v1" # Should be unique for each entry
282-
new_sqlite_classes = ["MyDurableObject"] # Array of new classes
283-
```
284-
285-
</WranglerConfig>
286-
287-
:::caution
288274
You cannot enable a SQLite storage backend on an existing, deployed Durable Object class, so setting `new_sqlite_classes` on later migrations will fail with an error. Automatic migration of deployed classes from their key-value storage backend to SQLite storage backend will be available in the future.
289-
:::

src/content/docs/workers/platform/pricing.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { GlossaryTooltip, Render } from "~/components";
1111

1212
By default, users have access to the Workers Free plan.
1313

14-
The Workers Free plan includes limited usage of Workers, Pages Functions and Workers KV. The daily free limit resets at 00:00 UTC. When the daily limit is reached, further operations of that type will fail with an error, until the limits reset again. For more information on the Free plan, refer to [Free plan limits](/workers/platform/limits/#worker-limits).
14+
The Workers Free plan includes limited usage of Workers and other products. Daily free limits for these products reset at 00:00 UTC. When the daily limit is reached, further operations of that type will fail with an error, until the limits reset again. For more information on the Free plan, refer to [Free plan limits](/workers/platform/limits/#worker-limits).
1515

1616
The Workers Paid plan includes Workers, Pages Functions, Workers KV, and Durable Objects usage for a minimum charge of $5 USD per month for an account. The plan includes increased initial usage allotments, with clear charges for usage that exceeds the base plan.
1717

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
{}
3+
---
4+
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+
7+
- Durable Object classes with the default, [SQLite storage backend](/durable-objects/features/access-sqlite-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+
- Durable Object classes with the key-value storage backend can use KV API.
9+
10+
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

Comments
 (0)