IndexedDB
IndexedDB is the browser's transactional, origin-scoped database for structured data. It stores more than localStorage and can index fields for queries.
Detailed Explanation
localStorage is a synchronous string map with a small quota. IndexedDB is asynchronous, holds objects (structured clone), and supports indexes. Offline apps, large caches, and saved editors use it.
The API is event-based and awkward. You open a database (indexedDB.open(name, version)), upgrade in onupgradeneeded (create object stores and indexes), then run transactions: db.transaction(store, "readwrite"), store.put(value), store.get(key). A transaction auto-commits when its microtasks drain; holding a transaction across an await fetch will abort it.
Libraries (idb, Dexie) wrap this in promises. Use one unless you enjoy request.onsuccess.
Object stores are tables. Each record has a key (keyPath like id, or an out-of-line key). Indexes let you getAll by email. There is no SQL join. You model aggregates as documents or you run several gets.
Quota is large (a percentage of disk) and can still fail with QuotaExceededError. Data is per origin and can be evicted under storage pressure unless you call the persistent-storage permission.
What it costs you: complexity and Safari historical bugs (now better). A broken upgrade path (you shipped version 3 that drops a store) loses user data. Test migrations.
Reach for IndexedDB when you persist more than a few kilobytes or need indexes. Reach for localStorage for tiny prefs. Reach for a server when the data must survive clearing site data.
Multi-tab access is allowed. Two tabs can write the same store; the last put wins unless you add a version field and reject stale writes. The versionchange event closes other tabs' connections so an upgrade can run. If you ignore it, an upgrade from a new deploy can sit blocked while an old tab holds the database open. Close or reload on versionchange.
Compound indexes ([userId, updatedAt]) cover the queries you actually run. An index you never query is only write cost. Design the keyPath around the get-by-id path, then add indexes for lists.
Code Examples
function openDb() {
return new Promise((resolve, reject) => {
const req = indexedDB.open("app", 1);
req.onupgradeneeded = () => {
const db = req.result;
const store = db.createObjectStore("notes", { keyPath: "id" });
store.createIndex("byUpdated", "updatedAt");
};
req.onsuccess = () => resolve(req.result);
req.onerror = () => reject(req.error);
});
}
async function saveNote(note) {
const db = await openDb();
await new Promise((resolve, reject) => {
const tx = db.transaction("notes", "readwrite");
tx.objectStore("notes").put(note);
tx.oncomplete = () => resolve();
tx.onerror = () => reject(tx.error);
});
}const note = { id: "n1", text: "hello", updatedAt: Date.now() };Common Gotchas
A transaction must finish in one turn of work
do not `await` network inside it. Read, await, then open a new transaction to write.
Version upgrades run only on `onupgradeneeded`
changing indexes requires bumping `version`.
Structured clone drops functions
store data, not class instances.
The raw API is callback-based
`idb` is the usual promise wrapper.