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/api/storage-api.mdx
+35Lines changed: 35 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -207,6 +207,41 @@ The `put()` method returns a `Promise`, but most applications can discard this p
207
207
208
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.
209
209
210
+
```ts
211
+
// Transfer money from one account to another. It's
212
+
// important that either both accounts are updated
213
+
// or neither is!
214
+
asynctransfer(amount, fromAcct, toAcct) {
215
+
// Open a transaction, so either all changes apply or
216
+
// none do.
217
+
ctx.storage.transactionSync(() => {
218
+
// Fetch both account balances.
219
+
let account1 =ctx.storage.sql.exec(
220
+
"SELECT balance FROM accounts WHERE id = ?", fromAcct)
221
+
.one();
222
+
let account2 =ctx.storage.sql.exec(
223
+
"SELECT balance FROM accounts WHERE id = ?", toAcct)
224
+
.one();
225
+
226
+
// Verify there's enough money in the account.
227
+
if (account1.balance<amount) {
228
+
// Throwing an error rolls back the transaction.
229
+
// (Though in this example there's no changes to roll
0 commit comments