Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

IDBObjectStore — an IndexedDB record store

IDBObjectStore exposes IndexedDB records through a transaction-bound handle. It supports adding, replacing, reading, deleting, and counting data, as well as accessing secondary indexes.

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 store = transaction.objectStore(name);
store.createIndex(name, keyPath, options); // upgrade transaction only
store.add(value, key); // key may be omitted
store.put(value, key); // key may be omitted
store.getAll(query, count); // query and count are optional
store.delete(query);
store.count(query); // query is optional
store.index(name);

IDBObjectStore is a handle to an IndexedDB record store, bound to a particular transaction. Each record has a primary key and a value copied through structured cloning. keyPath: "id" takes the key from the id field; with this configuration, do not pass a separate key argument to add() or put().

add() inserts a record; a duplicate key causes ConstraintError. put() inserts a record or replaces the entire value at an existing key—it does not merge fields. delete(query) removes records by primary key or key range. These operations require a transaction that permits writes. getAll(query?, count?) retrieves values, ordered by ascending primary key by default; count(query?) counts matching records. Omitting the query covers the whole store.

createIndex(name, keyPath, options) creates an index only during a schema-upgrade transaction, usually in onupgradeneeded; unique: true enforces unique index keys. index(name) returns an existing IDBIndex for secondary-key lookups. Both methods return handles synchronously; the other listed methods return IDBRequest, not Promise.

Request success does not mean transaction commit: wait for complete and handle abort. Queue operations while the transaction is active; waiting for unrelated asynchronous work can make it inactive. The example uses API forms from IndexedDB 2.0, without the newer options-object overload of getAll(). This is a Web API, not part of ECMAScript; Node 24 does not establish browser support. Check indexedDB availability in the target environment.

Reference identifiers covered by this entry: IDBObjectStore.createIndex, IDBObjectStore.add, IDBObjectStore.getAll, IDBObjectStore.delete, IDBObjectStore.put, IDBObjectStore.count, IDBObjectStore.index.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>IDBObjectStore: working with records</title>
</head>
<body>
  <h1>Working with an IndexedDB object store</h1>
  <script>
    (() => {
      const name = 'cktis-store-' + 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('items', { keyPath: 'id' });
        store.createIndex('byLabel', 'label', { unique: true });
      };
      opening.onsuccess = () => {
        const db = opening.result;
        if (!created) {
          db.close();
          console.log('Fixture name already in use');
          return;
        }
        const tx = db.transaction('items', 'readwrite');
        const store = tx.objectStore('items');
        store.add({ id: 1, label: 'draft' });
        store.add({ id: 2, label: 'spare' });
        store.put({ id: 1, label: 'final' });
        store.delete(2);
        const all = store.getAll();
        const count = store.count();
        const indexed = store.index('byLabel').get('final');
        const cleanup = () => {
          db.close();
          factory.deleteDatabase(name);
        };
        tx.oncomplete = () => {
          console.log(JSON.stringify({ items: all.result, count: count.result, indexedId: indexed.result.id }));
          cleanup();
        };
        tx.onabort = () => {
          report(tx.error);
          cleanup();
        };
      };
    })();
  </script>
</body>
</html>

Result

Successful browser run (IndexedDB access permitted):
{"items":[{"id":1,"label":"final"}],"count":1,"indexedId":1}

If the browser blocks IndexedDB access with a SecurityError, as in the isolated environment:
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.

Sources