IDBObjectStore: getKey() メソッド

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2021.

IDBObjectStore インターフェイスの getKey() メソッドは、IDBRequest オブジェクトを返し、別スレッドで指定のクエリーで選択されたキーを返します。これはオブジェクトストアから指定のレコードを取得する用です。

キーが正常に見つかった場合は、その構造化複製を作成し、要求オブジェクトの result に設定します。

メモ: この機能はウェブワーカー内で利用可能です。

構文

js
getKey(key)

引数

key

取得するレコードを特定するキーまたはキー範囲です。

返値

この操作に関係する今後のイベントが発火する IDBRequest オブジェクトです。

操作に成功した場合は、この要求の result プロパティの値は与えられたキーまたはキー範囲にマッチする最初のレコードのキーになります。

例外

このメソッドは、以下の種類のいずれかの DOMException を投げる可能性があります。

InvalidStateError DOMException

この IDBObjectStore が削除されたか取り除かれたとき投げられます。

TransactionInactiveError DOMException

この IDBObjectStore のトランザクションが実行中でないとき投げられます。

DataError DOMException

与えられたキーまたはキー範囲が無効なキーを含むとき投げられます。

js
let openRequest = indexedDB.open("telemetry");
openRequest.onsuccess = (event) => {
  let db = event.target.result;
  let store = db.transaction("netlogs").objectStore("netlogs");

  let today = new Date();
  let yesterday = new Date(today);
  yesterday.setDate(today.getDate() - 1);
  let request = store.getKey(IDBKeyRange(yesterday, today));
  request.onsuccess = (event) => {
    let when = event.target.result;
    alert(`直近 24 時間で最初のアクティビティは ${when} に発生しました。`);
  };
};

仕様書

Specification
Indexed Database API 3.0
# ref-for-dom-idbobjectstore-getkey①

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
getKey

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

関連情報