JavaScript - IndexedDB
Overview
Estimated time: 15–20 minutes
IndexedDB is a low-level API for client-side storage of significant amounts of structured data, including files/blobs.
Learning Objectives
- Understand the basics of IndexedDB.
- Store and retrieve data asynchronously.
Prerequisites
Opening a Database
const request = indexedDB.open('mydb', 1);
request.onsuccess = function(event) {
const db = event.target.result;
// Use db
};
Storing Data
const tx = db.transaction('store', 'readwrite');
const store = tx.objectStore('store');
store.put({ id: 1, name: 'Alice' });
Common Pitfalls
- IndexedDB is asynchronous and can be verbose for simple use cases.
Summary
IndexedDB is powerful for large, structured client-side storage, but can be complex for beginners.