Skip to content

Transactions

Status: Stable
Import: @jayoncode/storage/transactions

Previous: Diagnostics · Next: Async

Same-tab batched writes with rollback on throw. Overlay journal commits only if the callback returns; throws discard staged writes. Not multi-tab ACID.

ts
import { createStorage, createMemoryAdapter } from "@jayoncode/storage";
import { transaction } from "@jayoncode/storage/transactions";

const storage = createStorage({
  namespace: "app",
  adapter: createMemoryAdapter(),
});

transaction(storage, () => {
  storage.set("a", 1);
  storage.set("b", 2);
});

try {
  transaction(storage, () => {
    storage.set("a", 99);
    throw new Error("abort");
  });
} catch {
  // "a" unchanged
}

storage.get("a"); // 1

Notes

  • Nested transaction on the same instance throws ConfigurationError
  • Read-your-writes work inside the callback
  • Sync only — matches core adapters (ADR 0003)
  • Not on the default export (ADR 0006)

See also: Core · Errors

An ecosystem of independent, headless TypeScript libraries engineered for modern web development. Every package includes interactive playgrounds and documentation that evolves alongside the code.