Skip to content

Commit 1c0e919

Browse files
committed
Initialising PR for separating Storage API from SQL API.
1 parent f7b66cb commit 1c0e919

File tree

6 files changed

+93
-75
lines changed

6 files changed

+93
-75
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Alarms
33
pcx_content_type: concept
44
sidebar:
5-
order: 6
5+
order: 7
66
---
77

88
import { Type, GlossaryTooltip } from "~/components";

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: Runtime APIs
2+
title: Worker Binding API
33
pcx_content_type: navigation
44
sidebar:
55
order: 4
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
title: SQL Storage
3+
pcx_content_type: concept
4+
sidebar:
5+
order: 5
6+
7+
---
8+
9+
import { Render, Type, MetaInfo, GlossaryTooltip } from "~/components";
10+
11+
:::note[SQLite in Durable Objects Beta]
12+
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.
13+
:::
14+
15+
## Methods
16+
17+
### exec
18+
19+
<code>ctx.storage.sql.exec(query: <Type text='string'/>, ...bindings: <Type text='any[]'/>)</code>: <Type text='SqlStorageCursor' />
20+
21+
#### Parameters
22+
23+
* `query`: <Type text ='string' />
24+
* The SQL query string to be executed. `query` can contain `?` placeholders for parameter bindings. Multiple SQL statements, separated with a semicolon, can be executed in the `query`. With multiple SQL statements, any parameter bindings are applied to the last SQL statement in the `query`, and the returned cursor is only for the last SQL statement.
25+
* `bindings`: <Type text='any[]' /> <MetaInfo text='Optional' />
26+
* Optional variable number of arguments that correspond to the `?` placeholders in `query`.
27+
28+
#### Returns
29+
30+
A cursor (`SqlStorageCursor`) to iterate over query row results as objects. `SqlStorageCursor` is a JavaScript [Iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol), which supports iteration using `for (let row of cursor)`. `SqlStorageCursor` is also a JavaScript [Iterator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterator_protocol), which supports iteration using `cursor.next()`.
31+
32+
`SqlStorageCursor` supports the following methods:
33+
34+
* `next()`
35+
* Returns an object representing the next value of the cursor. The returned object has `done` and `value` properties adhering to the JavaScript [Iterator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterator_protocol). `done` is set to `false` when a next value is present, and `value` is set to the next row object in the query result. `done` is set to `true` when the entire cursor is consumed, and no `value` is set.
36+
* `toArray()`
37+
* Iterates through remaining cursor value(s) and returns an array of returned row objects.
38+
* `one()`
39+
* Returns a row object if query result has exactly one row. If query result has zero rows or more than one row, `one()` throws an exception.
40+
* `raw()`: <Type text='Iterator' />
41+
* Returns an Iterator over the same query results, with each row as an array of column values (with no column names) rather than an object.
42+
* Returned Iterator supports `next()`, `toArray()`, and `one()` methods above.
43+
* Returned cursor and `raw()` iterator iterate over the same query results and can be combined. For example:
44+
45+
```ts
46+
let cursor = this.sql.exec("SELECT * FROM artist ORDER BY artistname ASC;");
47+
let rawResult = cursor.raw().next();
48+
49+
if (!rawResult.done) {
50+
console.log(rawResult.value); // prints [ 123, 'Alice' ]
51+
} else {
52+
// query returned zero results
53+
}
54+
55+
console.log(cursor.toArray()); // prints [{ artistid: 456, artistname: 'Bob' },{ artistid: 789, artistname: 'Charlie' }]
56+
```
57+
`SqlStorageCursor` had the following properties:
58+
59+
* `columnNames`: <Type text='string[]' />
60+
* The column names of the query in the order they appear in each row array returned by the `raw` iterator.
61+
* `rowsRead`: <Type text='number' />
62+
* The number of rows read so far as part of this SQL `query`. This may increase as you iterate the cursor. The final value is used for [SQL billing](/durable-objects/platform/pricing/#sqlite-storage-backend).
63+
* `rowsWritten`: <Type text='number' />
64+
* The number of rows written so far as part of this SQL `query`. This may increase as you iterate the cursor. The final value is used for [SQL billing](/durable-objects/platform/pricing/#sqlite-storage-backend).
65+
66+
Note that `sql.exec()` cannot execute transaction-related statements like `BEGIN TRANSACTION` or `SAVEPOINT`. Instead, use the [`ctx.storage.transaction()`](#transaction) or [`ctx.storage.transactionSync()`](#transactionsync) APIs to start a transaction, and then execute SQL queries in your callback.
67+
68+
#### Examples
69+
70+
<Render file="durable-objects-sql" />
71+
72+
### databaseSize
73+
74+
`ctx.storage.sql.databaseSize`: <Type text ='number' />
75+
76+
#### Returns
77+
The current SQLite database size in bytes.
78+
79+
```ts
80+
let size = ctx.storage.sql.databaseSize;
81+
```
82+

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

Lines changed: 7 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Storage API
2+
title: Durable Object Storage
33
pcx_content_type: concept
44
sidebar:
55
order: 4
@@ -243,76 +243,6 @@ 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.exec
247-
248-
:::note[SQLite in Durable Objects Beta]
249-
250-
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.
251-
252-
:::
253-
254-
<code>ctx.storage.sql.exec(query: <Type text='string'/>, ...bindings: <Type text='any[]'/>)</code>: <Type text='SqlStorageCursor' />
255-
256-
#### Parameters
257-
* `query`: <Type text ='string' />
258-
* The SQL query string to be executed. `query` can contain `?` placeholders for parameter bindings. Multiple SQL statements, separated with a semicolon, can be executed in the `query`. With multiple SQL statements, any parameter bindings are applied to the last SQL statement in the `query`, and the returned cursor is only for the last SQL statement.
259-
* `bindings`: <Type text='any[]' /> <MetaInfo text='Optional' />
260-
* Optional variable number of arguments that correspond to the `?` placeholders in `query`.
261-
262-
#### Returns
263-
A cursor (`SqlStorageCursor`) to iterate over query row results as objects. `SqlStorageCursor` is a JavaScript [Iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol), which supports iteration using `for (let row of cursor)`. `SqlStorageCursor` is also a JavaScript [Iterator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterator_protocol), which supports iteration using `cursor.next()`.
264-
265-
`SqlStorageCursor` supports the following methods:
266-
267-
* `next()`
268-
* Returns an object representing the next value of the cursor. The returned object has `done` and `value` properties adhering to the JavaScript [Iterator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterator_protocol). `done` is set to `false` when a next value is present, and `value` is set to the next row object in the query result. `done` is set to `true` when the entire cursor is consumed, and no `value` is set.
269-
* `toArray()`
270-
* Iterates through remaining cursor value(s) and returns an array of returned row objects.
271-
* `one()`
272-
* Returns a row object if query result has exactly one row. If query result has zero rows or more than one row, `one()` throws an exception.
273-
* `raw()`: <Type text='Iterator' />
274-
* Returns an Iterator over the same query results, with each row as an array of column values (with no column names) rather than an object.
275-
* Returned Iterator supports `next()`, `toArray()`, and `one()` methods above.
276-
* Returned cursor and `raw()` iterator iterate over the same query results and can be combined. For example:
277-
278-
```ts
279-
let cursor = this.sql.exec("SELECT * FROM artist ORDER BY artistname ASC;");
280-
let rawResult = cursor.raw().next();
281-
282-
if (!rawResult.done) {
283-
console.log(rawResult.value); // prints [ 123, 'Alice' ]
284-
} else {
285-
// query returned zero results
286-
}
287-
288-
console.log(cursor.toArray()); // prints [{ artistid: 456, artistname: 'Bob' },{ artistid: 789, artistname: 'Charlie' }]
289-
```
290-
`SqlStorageCursor` had the following properties:
291-
292-
* `columnNames`: <Type text='string[]' />
293-
* The column names of the query in the order they appear in each row array returned by the `raw` iterator.
294-
* `rowsRead`: <Type text='number' />
295-
* The number of rows read so far as part of this SQL `query`. This may increase as you iterate the cursor. The final value is used for [SQL billing](/durable-objects/platform/pricing/#sqlite-storage-backend).
296-
* `rowsWritten`: <Type text='number' />
297-
* The number of rows written so far as part of this SQL `query`. This may increase as you iterate the cursor. The final value is used for [SQL billing](/durable-objects/platform/pricing/#sqlite-storage-backend).
298-
299-
Note that `sql.exec()` cannot execute transaction-related statements like `BEGIN TRANSACTION` or `SAVEPOINT`. Instead, use the [`ctx.storage.transaction()`](#transaction) or [`ctx.storage.transactionSync()`](#transactionsync) APIs to start a transaction, and then execute SQL queries in your callback.
300-
301-
#### Examples
302-
303-
<Render file="durable-objects-sql" />
304-
305-
### sql.databaseSize
306-
307-
`ctx.storage.sql.databaseSize`: <Type text ='number' />
308-
309-
#### Returns
310-
The current SQLite database size in bytes.
311-
312-
```ts
313-
let size = ctx.storage.sql.databaseSize;
314-
```
315-
316246
### Point in time recovery
317247

318248
For [Durable Objects classes with SQL storage](/durable-objects/reference/durable-objects-migrations/#enable-sqlite-storage-backend-on-new-durable-object-class-migration), the following point-in-time-recovery (PITR) API methods are available to restore a Durable Object's embedded SQLite database to any point in time in the past 30 days. These methods apply to the entire SQLite database contents, including both the object's stored SQL data and stored key-value data using the key-value `put()` API. The PITR API is not supported in local development because a durable log of data changes is not stored locally.
@@ -341,6 +271,12 @@ The PITR API represents points in times using "bookmarks". A bookmark is a mostl
341271
ctx.storage.onNextSessionRestoreBookmark(bookmark);
342272
```
343273

274+
### sql
275+
276+
<code>ctx.storage.sql.METHOD</code>: <Type text="SqlStorage"/>
277+
278+
- Allows you to access the `SqlStorage` methods.
279+
344280
### Related resources
345281

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
pcx_content_type: configuration
33
title: WebGPU
44
sidebar:
5-
order: 7
5+
order: 8
66
---
77

88
:::caution

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: WebSockets
33
pcx_content_type: concept
44
sidebar:
5-
order: 5
5+
order: 6
66
---
77

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

0 commit comments

Comments
 (0)