Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

IndexedDB – Browser‑Side Structured Dat | HTML Course lesson | BraveProgrammer
  1. Courses
  2. /
  3. HTML Course

Lesson 33 of 55

·HTML

IndexedDB – Browser‑Side Structured Data

11 min

Course progress60%

What you'll learn

Keep these ideas in mind as you work through the lesson.

5 points
  • Opening an IndexedDB database with a version number
  • Creating object stores and indexes for efficient queries
  • Performing CRUD operations via transactions and the request/event model
  • Asynchronous nature of IndexedDB and handling errors
  • Using helper libraries (`idb`, `localForage`) for a promise‑based workflow

IndexedDB Overview

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');
};

Key concepts

  • Database version – Increment to trigger onupgradeneeded.
  • Object store – Like a table; each record is a JavaScript object.
  • Indexes – Enable fast queries on a property.
  • Transactions – Group operations; either all succeed or all fail.
  • Cursors – Iterate over large data sets without loading everything into memory.

Helper libraries

  • idb – Promise‑based wrapper that simplifies the verbose IndexedDB API.
  • localForage – Abstracts IndexedDB, WebSQL, and localStorage behind a simple API.

Practical use‑cases

  • Offline‑first web apps (e.g., note‑taking, todo lists).
  • Caching API responses for progressive web apps.
  • Storing large binary blobs (images, audio) locally.

Quick checklist for IndexedDB

  1. Database opened with a version number? ✅
  2. Object stores and indexes defined in onupgradeneeded? ✅
  3. All read/write operations wrapped in transactions? ✅
  4. Errors handled (request.onerror)? ✅
  5. Consider using idb or localForage for simpler code? ✅

Tip: When storing large blobs, consider using the Blob type and streaming APIs to keep memory usage low.

Make it stickPractice what you've learned1 quiz · 2 practical tests · Optional activities from HTML CourseBrowse activities
Previous lessonWeb Storage – localStorage & sessionStorageContinue to lesson 34Text Formatting TagsBack to HTML Course curriculum