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
- Opening a Database
- Transactions and CRUD
- Indexes and Queries
- Versioning and Upgrades