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
[Durable Objects] Document transaction API changes with SQLite. (#17698)
* [Durable Objects] Document transaction API changes with SQLite.
* [Durable Objects] Fix broken links to SQLite class creation docs.
These existing links were just broken. Though, arguably they should actually go to `/durable-objects/best-practices/access-durable-objects-storage/#sqlite-storage-backend`, perhaps? For now I just fixed the links.
Copy file name to clipboardExpand all lines: src/content/docs/durable-objects/api/storage-api.mdx
+18-2Lines changed: 18 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -191,7 +191,21 @@ The `put()` method returns a `Promise`, but most applications can discard this p
191
191
192
192
*`txn`
193
193
194
-
* Provides access to the `put()`, `get()`, `delete()` and `list()` methods documented above to run in the current transaction context. In order to get transactional behavior within a transaction closure, you must call the methods on the `txn` Object instead of on the top-level `state.storage` Object.<br/><br/>Also supports a `rollback()` function that ensures any changes made during the transaction will be rolled back rather than committed. After `rollback()` is called, any subsequent operations on the `txn` Object will fail with an exception. `rollback()` takes no parameters and returns nothing to the caller.
194
+
* Provides access to the `put()`, `get()`, `delete()` and `list()` methods documented above to run in the current transaction context. In order to get transactional behavior within a transaction closure, you must call the methods on the `txn` Object instead of on the top-level `ctx.storage` Object.<br/><br/>Also supports a `rollback()` function that ensures any changes made during the transaction will be rolled back rather than committed. After `rollback()` is called, any subsequent operations on the `txn` Object will fail with an exception. `rollback()` takes no parameters and returns nothing to the caller.
195
+
196
+
* 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.
197
+
198
+
### transactionSync
199
+
200
+
*`transactionSync(callback)`: <Typetext='any' />
201
+
202
+
* Only available when using [the SQLite-backed storage engine](/durable-objects/best-practices/access-durable-objects-storage/#sqlite-storage-backend).
203
+
204
+
* Invokes `callback()` wrapped in a transaction, and returns its result.
205
+
206
+
* If `callback()` throws an exception, the transaction will be rolled back.
207
+
208
+
* 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.
195
209
196
210
### sync
197
211
@@ -233,7 +247,7 @@ The `put()` method returns a `Promise`, but most applications can discard this p
233
247
234
248
:::note[SQLite in Durable Objects Beta]
235
249
236
-
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-create-durable-object-class-migration) and will return an error if called on Durable Object classes with a key-value storage backend.
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.
237
251
238
252
:::
239
253
@@ -282,6 +296,8 @@ A cursor (`SqlStorageCursor`) to iterate over query row results as objects. `Sql
282
296
*`rowsWritten`: <Typetext='number' />
283
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).
284
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.
Copy file name to clipboardExpand all lines: src/content/partials/workers/storage_api_pricing.mdx
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,7 +42,7 @@ You can introspect rows read and rows written using `cursor.rowsRead` and `curso
42
42
43
43
:::
44
44
45
-
For [Durable Objects classes with SQLite storage backend](/durable-objects/reference/durable-objects-migrations/#enable-sqlite-storage-backend-on-create-durable-object-class-migration) via `ctx.storage.sql` the following pricing is used instead:
45
+
For [Durable Objects classes with SQLite storage backend](/durable-objects/reference/durable-objects-migrations/#enable-sqlite-storage-backend-on-new-durable-object-class-migration) via `ctx.storage.sql` the following pricing is used instead:
0 commit comments