Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 178322d

Browse files
committed
Propagate Durable Object transaction return values
1 parent 1c97a3b commit 178322d

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/kv/do.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,9 @@ export class DurableObjectStorage implements DurableObjectOperator {
341341
return this.#transaction((txn) => txn.list(options));
342342
}
343343

344-
async transaction(
345-
closure: (txn: DurableObjectTransaction) => Promise<void>
346-
): Promise<void> {
347-
await this.#transaction(closure);
344+
async transaction<T>(
345+
closure: (txn: DurableObjectTransaction) => Promise<T>
346+
): Promise<T> {
347+
return this.#transaction(closure);
348348
}
349349
}

test/kv/do.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,15 @@ test("transaction: cannot perform more operations after rollback", async (t) =>
372372
await t.throws(() => txn.rollback(), { instanceOf: AssertionError });
373373
});
374374
});
375+
test("transaction: propagates return value", async (t) => {
376+
const { backing, storage } = t.context;
377+
await backing.put("a", storedValue(1));
378+
const res = await storage.transaction(async (txn) => {
379+
const value = (await txn.get<number>("a")) ?? 0;
380+
return value + 2;
381+
});
382+
t.is(res, 3);
383+
});
375384
test("transaction: aborts all in-progress transactions", async (t) => {
376385
const { backing, storage } = t.context;
377386
await backing.put("key", storedValue("old"));

0 commit comments

Comments
 (0)