CKTIS / JavaScript
IDBIndex — querying an IndexedDB index
IDBIndex exposes object-store records through a secondary key. Compare array retrieval with getAll() against cursor iteration with openCursor(), and distinguish index keys from primary keys.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
const index = transaction.objectStore(storeName).index(indexName);
const valuesRequest = index.getAll(query, count);
const cursorRequest = index.openCursor(query, "next");IDBIndex is a handle to an IndexedDB object store's secondary index, not a constructor to use directly. Create the index with createIndex() during upgradeneeded; later obtain it with store.index(name) inside a transaction. The index updates automatically with the store.
IDBIndex.getAll(query, count) returns an IDBRequest, not a Promise. Its result is an array of store values, or [] for no matches. query accepts an index key or IDBKeyRange; omitting it matches all entries. The optional count limits the number of results, but passing 0 is equivalent to omitting count: all matches are returned. Ordering is ascending by index key, then primary key.
IDBIndex.openCursor(query, direction) also returns an IDBRequest. In success, read the cursor or null when no results remain; continue() starts the next step. cursor.key is the index key, cursor.primaryKey is the store key, and cursor.value is the record. The default direction is "next"; alternatives are "prev", "nextunique", and "prevunique".
A cursor supports incremental processing; getAll() materializes the entire result array. Issue requests while the transaction is active; do not insert an unrelated await between them. The example uses IndexedDB 2.0 syntax without the newer getAll() options form. Check your target browser: having Node 24 does not establish support for this Web API.
Examples
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>IDBIndex: querying a secondary key</title>
</head>
<body>
<h1>Querying an IndexedDB index</h1>
<script>
(() => {
const name = 'cktis-index-' + crypto.getRandomValues(new Uint32Array(4)).join('-');
const report = error => console.log(error.name === 'SecurityError'
? 'IndexedDB unavailable: SecurityError' : `IndexedDB error: ${error.name}`);
let factory, opening;
try {
factory = window.indexedDB;
if (!factory) {
console.log('IndexedDB unavailable: API missing');
return;
}
opening = factory.open(name, 1);
} catch (error) {
report(error);
return;
}
let created = false;
opening.onerror = () => report(opening.error);
opening.onupgradeneeded = () => {
created = true;
const store = opening.result.createObjectStore('tokens', { keyPath: 'id' });
store.createIndex('byColor', 'color');
store.add({ id: 2, color: 'blue', token: 'square' });
store.add({ id: 1, color: 'blue', token: 'circle' });
store.add({ id: 3, color: 'red', token: 'triangle' });
};
opening.onsuccess = () => {
const db = opening.result;
if (!created) {
db.close();
console.log('Fixture name already in use');
return;
}
const tx = db.transaction('tokens', 'readonly');
const index = tx.objectStore('tokens').index('byColor');
const all = index.getAll('blue');
const scan = index.openCursor('blue');
const rows = [];
scan.onsuccess = () => {
const cursor = scan.result;
if (cursor) {
rows.push([cursor.key, cursor.primaryKey, cursor.value.token]);
cursor.continue();
}
};
const cleanup = () => {
db.close();
factory.deleteDatabase(name);
};
tx.oncomplete = () => {
console.log(JSON.stringify(all.result.map(record => record.token)));
console.log(JSON.stringify(rows));
cleanup();
};
tx.onabort = () => {
report(tx.error);
cleanup();
};
};
})();
</script>
</body>
</html>Result
When IndexedDB access is allowed and the query succeeds, both lines show matches for the index key "blue" in ascending primary-key order: ["circle","square"] [["blue",1,"circle"],["blue",2,"square"]] If the browser denies IndexedDB access with a SecurityError, the example logs instead: IndexedDB unavailable: SecurityError
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.