Reference

Transactions & CRUD

Compressed map of scoped work and store verbs. Pair with Lesson 3.

Shape

const tx = db.transaction(storeNameOrNames, mode);
// mode: "readonly" | "readwrite"  (versionchange only from upgradeneeded)

const store = tx.objectStore("notes");
store.get(key);
store.getAll(query?, count?);
store.put(value, key?);   // upsert
store.add(value, key?);   // fail if key exists
store.delete(key);
store.clear();

tx.oncomplete = () => { /* durable for this tx */ };
tx.onerror = () => { /* aborted / failed */ };
tx.onabort = () => { /* aborted */ };

Modes

Mode Use
readonly Loads; prefer by default
readwrite Any mutation; keep short
versionchange Schema only (upgrade path)

Lifetime rules

  1. Active when created and inside request success/error handlers.
  2. Goes inactive when the turn ends without pending work keeping it busy.
  3. Auto-commits when outstanding requests finish and no new ones were scheduled while active.
  4. Do not await unrelated async mid-transaction and then issue more store requests.
Red flag

fetch / timers / user prompts between transaction(...) and the last store request — classic inactive/finished errors.

Saved locally

Prefer tx.oncomplete (or wrapper promise for the whole transaction) over a single request’s onsuccess when multiple ops must land together.