0Pricing
JavaScript Academy · Lesson

Indexes and Queries

Query data efficiently with indexes.

Why Indexes?

By default you can only look up records by their primary key. An index lets you query by another property efficiently, like finding all users with a given last name.

Creating an Index

Create indexes inside onupgradeneeded, right after creating the store, with store.createIndex(name, keyPath, options).

request.onupgradeneeded = (e) => {
  const db = e.target.result;
  const store = db.createObjectStore('users', { keyPath: 'id' });
  store.createIndex('byEmail', 'email', { unique: true });
};

All lessons in this course

  1. Opening a Database
  2. Transactions and CRUD
  3. Indexes and Queries
  4. Versioning and Upgrades
← Back to JavaScript Academy