Reference

Open & upgrade

Compressed map of connection lifecycle and schema changes. Pair with Lesson 2.

Open a connection

const request = indexedDB.open("app-db", 2); // name, version

request.onerror = (e) => { /* open failed */ };
request.onblocked = () => { /* other connections won’t close */ };
request.onupgradeneeded = (e) => {
  const db = request.result;
  const oldVersion = e.oldVersion; // 0 if brand new
  // createObjectStore / createIndex / delete… only here
};
request.onsuccess = () => {
  const db = request.result;
  // use db.transaction(...) for reads/writes
};

Object store keys

Style Create Put shape
In-line key { keyPath: "id" } Key lives on the value object
Out-of-line key no keyPath Pass key as second arg to put/add
Generator { autoIncrement: true } Optional keyPath that receives generated number

Multi-tab upgrade dance

  1. Tab B opens with a higher version → other tabs get versionchange.
  2. Old tabs should finish work and db.close() (or reload).
  3. If they don’t, Tab B gets blocked until they close.
  4. Only then does upgradeneeded run for Tab B.
Design cost

Schema lives in the upgrade path. Bumping version is a deploy concern (old tabs, migration code, forever-forward oldVersion steps) — not a free refactor.