Lesson 33 of 55
HTMLIndexedDB – Browser‑Side Structured Data
11 min
Course progress60%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 33 of 55
HTML11 min
Keep these ideas in mind as you work through the lesson.
IndexedDB is a low‑level API for storing large amounts of structured data (records, indexes). It works asynchronously and supports transactions.
// Open (or create) a database
const request = indexedDB.open('myDB', 1);
request.onupgradeneeded = e => {
const db = e.target.result;
const store = db.createObjectStore('notes', { keyPath: 'id', autoIncrement: true });
store.createIndex('by_title', 'title', { unique: false });
};
request.onsuccess = e => {
const db = e.target.result;
const tx = db.transaction('notes', 'readwrite');
const store = tx.objectStore('notes');
store.add({ title: 'First note', body: 'Hello IndexedDB' });
tx.oncomplete = () => console.log('Note added');
};
onupgradeneeded.onupgradeneeded? ✅request.onerror)? ✅idb or localForage for simpler code? ✅Tip: When storing large blobs, consider using the Blob type and streaming APIs to keep memory usage low.