DEV Community

Arhan Khan
Arhan Khan

Posted on

IndexedDB for Beginners: A Real Database in the Browser

LocalStorage is fine for settings and short lists, but it stores strings, caps out around 5MB, and blocks the page while it works. IndexedDB is the browser's real database: it stores objects, holds hundreds of megabytes, supports indexes and queries, and never blocks the UI. If your app needs offline data that feels like a real app, this is the tool.

IndexedDB is asynchronous and event-based, which makes the raw API look intimidating. A small promise wrapper tames it:

function openDB() {
  return new Promise((resolve, reject) => {
    const request = indexedDB.open("my-app", 1);
    request.onupgradeneeded = (event) => {
      const db = event.target.result;
      db.createObjectStore("notes", { keyPath: "id", autoIncrement: true });
    };
    request.onsuccess = (event) => resolve(event.target.result);
    request.onerror = (event) => reject(event.target.error);
  });
}
Enter fullscreen mode Exit fullscreen mode

The version number matters: bump it when you change the schema, and onupgradeneeded runs your migration. Object stores are tables, and keyPath with autoIncrement gives every record an id automatically.

Reading and writing go through transactions — short-lived, and the pattern is the same every time:

async function addNote(text) {
  const db = await openDB();
  return new Promise((resolve, reject) => {
    const tx = db.transaction("notes", "readwrite");
    const request = tx.objectStore("notes").add({ text, created: Date.now() });
    request.onsuccess = () => resolve(request.result);
    request.onerror = () => reject(request.error);
  });
}

async function getAllNotes() {
  const db = await openDB();
  return new Promise((resolve, reject) => {
    const request = db.transaction("notes").objectStore("notes").getAll();
    request.onsuccess = () => resolve(request.result);
    request.onerror = () => reject(request.error);
  });
}
Enter fullscreen mode Exit fullscreen mode

Indexes turn a full scan into a lookup. If you query notes by tag, create an index once and query it forever:

// inside onupgradeneeded:
const store = db.createObjectStore("notes", { keyPath: "id", autoIncrement: true });
store.createIndex("by-tag", "tag");

// query:
const index = db.transaction("notes").objectStore("notes").index("by-tag");
index.getAll("work");
Enter fullscreen mode Exit fullscreen mode

Three things beginners get wrong: first, treating IndexedDB like localStorage — it is async, so every operation returns a request, not a value. Second, forgetting that a transaction auto-commits when the current task ends, so all requests must be made synchronously inside the same function. Third, opening the database on every operation — open once and reuse the connection.

In practice, most developers use a tiny wrapper library like idb (about 1KB) that turns all of this into async/await one-liners. Learn the raw API once so you understand what the wrapper does, then use the wrapper in real projects.

Ways to extend it: build a notes app that works fully offline and syncs when the connection returns, add a search index over note titles, or store images as blobs for an offline gallery. IndexedDB is the reason web apps can work like native apps — and it is already in every browser your users have.

Top comments (0)