IndexedDB
A browser API for storing large amounts of structured, queryable data client-side, well suited to offline-capable applications.
indexedDB.open(name, version)
Purpose
IndexedDB stores large amounts of structured data client-side, with indexes for efficient querying
— unlike localStorage’s small, string-only, synchronous model, it’s designed for genuinely
sizable, offline-capable application data.
Syntax
const request = indexedDB.open('readingListDB', 1);
request.onupgradeneeded = (event) => {
const db = event.target.result;
db.createObjectStore('articles', { keyPath: 'id' });
};
request.onsuccess = (event) => {
const db = event.target.result;
const tx = db.transaction('articles', 'readwrite');
tx.objectStore('articles').put({ id: 1, title: 'Example', read: false });
};
Behavior notes
- The API is entirely asynchronous and event-based (older callback style) — most real code wraps it in a small promise-based helper, or uses a library, rather than working with raw events directly.
- Data is organized into object stores (roughly analogous to tables), each with a key path, and optional indexes for querying by other fields.
- A transaction groups related reads/writes; it auto-commits once its associated events finish, without an explicit “commit” call.
- Storage limits are much larger than Web Storage’s — often a meaningful percentage of available disk space — though exact limits and eviction policy are browser- and platform-dependent.
Common mistakes
- Reaching for IndexedDB for small amounts of simple data, where
localStoragewould be far simpler — its complexity is worth it specifically for larger, structured, or offline-critical data. - Letting a transaction span an
awaited operation unrelated to the database — some browsers auto-close a transaction if it appears idle, causing a subsequent operation on it to fail.
Runtime notes
Standard in every current browser; no built-in equivalent in Node (a compatible package exists for testing purposes, but it isn’t a runtime API there).