Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

IDBCursor — iterating over IndexedDB records

IDBCursor reads IndexedDB records sequentially in key order. A value-bearing cursor exposes cursor.value, while cursor.continue() asynchronously advances to the next record.

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 request = store.openCursor();
request.onsuccess = () => {
  const cursor = request.result;
  if (!cursor) return;
  const value = cursor.value;
  cursor.continue();
};

IDBCursor represents a position while traversing an IndexedDB object store or index. openCursor() returns an IDBRequest, not a cursor. Read request.result in onsuccess; a missing cursor ends iteration. The default direction, next, means ascending key order, not insertion order.

The name IDBCursor.value is descriptive shorthand: the value property is actually defined by IDBCursorWithValue, which inherits from IDBCursor. openCursor() exposes that cursor; openKeyCursor() does not expose value. Mutating an object returned by cursor.value does not persist changes; use update() in a readwrite transaction to save them.

IDBCursor.continue, invoked as cursor.continue(), schedules the next step and returns undefined, not a Promise. Its result arrives in another success event on the same request. Call it once per step in an active transaction: calling again before the result causes InvalidStateError, while an inactive transaction causes TransactionInactiveError. Do not wait for unrelated asynchronous operations between steps.

This is a Web API, not part of ECMAScript; having Node 24 does not establish support. The described operations are specified in Indexed Database API 2.0 and 3.0. Check support and storage availability in the target browser.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Iterating records with IDBCursor</title>
</head>
<body>
<script>
  (() => {
    // This disposable name does not require a secure-context API.
    const name = `cktis-cursor-${Date.now()}-${Math.random()}`;
    const unavailable = () => console.log('IndexedDB unavailable in this context.');
    let open;
    try {
      open = indexedDB.open(name, 1);
    } catch {
      unavailable();
      return;
    }
    open.onerror = unavailable;
    open.onupgradeneeded = () => {
      const store = open.result.createObjectStore('items');
      store.put('Beta', 2);
      store.put('Alpha', 1);
    };
    open.onsuccess = () => {
      const db = open.result;
      const rows = [];
      const tx = db.transaction('items', 'readonly');
      const request = tx.objectStore('items').openCursor();
      request.onsuccess = () => {
        const cursor = request.result;
        if (cursor === null) return;
        rows.push(`${cursor.key}: ${cursor.value}`);
        cursor.continue();
      };
      function cleanup() {
        db.close();
        indexedDB.deleteDatabase(name);
      }
      tx.oncomplete = () => {
        for (const row of rows) console.log(row);
        cleanup();
      };
      tx.onabort = () => {
        cleanup();
        console.log('Cursor transaction aborted.');
      };
    };
  })();
</script>
</body>
</html>

Result

1: Alpha
2: Beta

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources