Skip to content

Commit 27684e4

Browse files
committed
Introducing SqlStorage and Sql properly.
1 parent 4785b1b commit 27684e4

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Worker Binding API
2+
title: Workers Binding API
33
pcx_content_type: navigation
44
sidebar:
55
order: 4

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,33 @@ sidebar:
88

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

11+
The `SqlStorage` interface encapsulates methods that modify the SQLite database embedded within a Durable Object.
12+
13+
For example, using `sql.exec()`, a user can create a table, then insert rows into the table.
14+
15+
```js
16+
import { DurableObject } from "cloudflare:workers";
17+
18+
export class MyDurableObject extends DurableObject {
19+
sql: SqlStorage
20+
constructor(ctx: DurableObjectState, env: Env) {
21+
super(ctx, env);
22+
this.sql = ctx.storage.sql;
23+
24+
this.sql.exec(`CREATE TABLE IF NOT EXISTS artist(
25+
artistid INTEGER PRIMARY KEY,
26+
artistname TEXT
27+
);INSERT INTO artist (artistid, artistname) VALUES
28+
(123, 'Alice'),
29+
(456, 'Bob'),
30+
(789, 'Charlie');`
31+
);
32+
}
33+
}
34+
```
35+
36+
The `SqlStorage` interface is accessible via the `sql` property of `DurableObjectStorage` class.
37+
1138
:::note[SQLite in Durable Objects Beta]
1239
SQL API methods accessed with `ctx.storage.sql` are only allowed on [Durable Object classes with SQLite storage backend](/durable-objects/reference/durable-objects-migrations/#enable-sqlite-storage-backend-on-new-durable-object-class-migration) and will return an error if called on Durable Object classes with a key-value storage backend.
1340
:::

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ The Durable Object Storage API comes with several methods, including key-value (
5454

5555
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.
5656

57-
### get
57+
### `get`
5858

5959
* <code>get(key <Type text='string' />, options <Type text='Object' /> <MetaInfo text='optional' />)</code>: <Type text='Promise<any>' />
6060

@@ -74,7 +74,7 @@ Each method is implicitly wrapped inside a transaction, such that its results ar
7474

7575
* If true, then the key/value will not be inserted into the in-memory cache. If the key is already in the cache, the cached value will be returned, but its last-used time will not be updated. Use this when you expect this key will not be used again in the near future. This flag is only a hint. This flag will never change the semantics of your code, but it may affect performance.
7676

77-
### put
77+
### `put`
7878

7979
* <code>put(key <Type text='string' />, value <Type text='any' />, options <Type text='Object' /> <MetaInfo text='optional' />)</code>: <Type text='Promise' />
8080

@@ -86,7 +86,7 @@ Each method is implicitly wrapped inside a transaction, such that its results ar
8686
* Each value can be any type supported by the [structured clone algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm), which is true of most types.
8787
* Supports up to 128 key-value pairs at a time. Each key is limited to a maximum size of 2,048 bytes and each value is limited to 128 KiB (131,072 bytes).
8888

89-
### delete
89+
### `delete`
9090

9191
* <code>delete(key <Type text='string' />, options <Type text='Object' /> <MetaInfo text='optional' />)</code>: <Type text='Promise<boolean>' />
9292

@@ -96,7 +96,7 @@ Each method is implicitly wrapped inside a transaction, such that its results ar
9696

9797
* Deletes the provided keys and their associated values. Supports up to 128 keys at a time. Returns a count of the number of key-value pairs deleted.
9898

99-
### deleteAll
99+
### `deleteAll`
100100

101101
* <code>deleteAll(options <Type text='Object' /> <MetaInfo text='optional' />)</code>: <Type text='Promise' />
102102

@@ -136,7 +136,7 @@ If you invoke `put()` (or `delete()`) multiple times without performing any `awa
136136
The `put()` method returns a `Promise`, but most applications can discard this promise without using `await`. The `Promise` usually completes immediately, because `put()` writes to an in-memory write buffer that is flushed to disk asynchronously. However, if an application performs a large number of `put()` without waiting for any I/O, the write buffer could theoretically grow large enough to cause the isolate to exceed its 128 MB memory limit. To avoid this scenario, such applications should use `await` on the `Promise` returned by `put()`. The system will then apply backpressure onto the application, slowing it down so that the write buffer has time to flush. Using `await` will disable automatic write coalescing.
137137
:::
138138

139-
### list
139+
### `list`
140140

141141
* <code>list(options <Type text='Object' /> <MetaInfo text='optional' />)</code>: <Type text= 'Promise<Map<string, any>>' />
142142

@@ -181,7 +181,7 @@ The `put()` method returns a `Promise`, but most applications can discard this p
181181

182182
* Same as the option to `get()`, above.
183183

184-
### transaction
184+
### `transaction`
185185

186186
* `transaction(closureFunction(txn))`: <Type text='Promise' />
187187

@@ -195,7 +195,7 @@ The `put()` method returns a `Promise`, but most applications can discard this p
195195

196196
* When using [the SQLite-backed storage engine](/durable-objects/best-practices/access-durable-objects-storage/#sqlite-storage-backend), the `txn` object is obsolete. Any storage operations performed directly on the `ctx.storage` object, including SQL queries using [`ctx.storage.sql.exec()`](#sqlexec), will be considered part of the transaction.
197197

198-
### transactionSync
198+
### `transactionSync`
199199

200200
* `transactionSync(callback)`: <Type text='any' />
201201

@@ -207,15 +207,15 @@ The `put()` method returns a `Promise`, but most applications can discard this p
207207

208208
* The callback must complete synchronously, that is, it should not be declared `async` nor otherwise return a Promise. Only synchronous storage operations can be part of the transaction. This is intended for use with SQL queries using [`ctx.storage.sql.exec()`](#sqlexec), which complete sychronously.
209209

210-
### sync
210+
### `sync`
211211

212212
* `sync()` : <Type text='Promise' />
213213

214214
* Synchronizes any pending writes to disk.
215215

216216
* This is similar to normal behavior from automatic write coalescing. If there are any pending writes in the write buffer (including those submitted with [the `allowUnconfirmed` option](/durable-objects/api/storage-api/#supported-options-1)), the returned promise will resolve when they complete. If there are no pending writes, the returned promise will be already resolved.
217217

218-
### getAlarm
218+
### `getAlarm`
219219

220220
* <code>getAlarm(options <Type text='Object' /> <MetaInfo text='optional' />)</code>: <Type text='Promise<Number | null>' />
221221

@@ -225,7 +225,7 @@ The `put()` method returns a `Promise`, but most applications can discard this p
225225

226226
* Same options as [`get()`](/durable-objects/api/storage-api/#get), but without `noCache`.
227227

228-
### setAlarm
228+
### `setAlarm`
229229

230230
* <code>setAlarm(scheduledTime <Type text='Date | number' />, options <Type text='Object' /> <MetaInfo text='optional' />)</code>: <Type text='Promise' />
231231

@@ -243,13 +243,13 @@ The `put()` method returns a `Promise`, but most applications can discard this p
243243

244244
* `setAlarm()` and `deleteAlarm()` support the same options as [`put()`](/durable-objects/api/storage-api/#put), but without `noCache`.
245245

246-
### sql
246+
## Properties
247247

248-
<code>ctx.storage.sql.METHOD</code>: <Type text="SqlStorage"/>
248+
### `sql`
249249

250-
- Allows you to access the `SqlStorage` methods.
250+
`sql` is a readonly property of type `DurableObjectStorage` encapsulating the [SQL API](/durable-objects/api/sql-storage).
251251

252-
### Related resources
252+
## Related resources
253253

254254
* [Durable Objects: Easy, Fast, Correct – Choose Three](https://blog.cloudflare.com/durable-objects-easy-fast-correct-choose-three/).
255255
* [WebSockets API](/durable-objects/api/websockets/).

0 commit comments

Comments
 (0)