DOM

インターフェイス

IDBObjectStore インターフェイス

  編集:2022.04.06 by T.A.

MS(en-US)

IndexedDB API の IDBObjectStore インターフェイスは、データベース内のオブジェクト ストアを表します。オブジェクト ストア内のレコードは、キーに従って並べ替えられます。この並べ替えにより、高速挿入が可能になります。調べよ、注文検索。

注:この機能は Web Workers で使用できます。

プロパティ
IDBObjectStore.indexNames 読取専用
このオブジェクト ストア内のオブジェクトのインデックスの名前のリスト。
IDBObjectStore.keyPath 読取専用
このオブジェクト ストアのキー パス。この属性がnullの場合、アプリケーションは各変更操作のキーを提供する必要があります。
IDBObjectStore.name
このオブジェクト ストアの名前。
IDBObjectStore.transaction 読取専用
このオブジェクト ストアが属する IDBTransaction オブジェクト。
IDBObjectStore.autoIncrement 読取専用
このオブジェクト ストアの自動インクリメント フラグの値。
メソッド
IDBObjectStore.add()
IDBRequest オブジェクトを返し、別のスレッドで、値の構造化されたクローンを作成します。複製された値をオブジェクト ストアに保存します。これは、オブジェクト ストアに新しいレコードを追加するためのものです。
IDBObjectStore.clear()
IDBRequest オブジェクトを作成してすぐに返し、このオブジェクト ストアを別のスレッドでクリアします。これは、オブジェクト ストアから現在のすべてのレコードを削除するためのものです。
IDBObjectStore.count()
IDBRequest オブジェクトを返し、および、別のスレッドで、指定されたキーまたは IDBKeyRange に一致するレコードの総数を返します。引数が指定されていない場合は、ストア内のレコードの総数を返します。
IDBObjectStore.createIndex()
バージョンのアップグレード中に新しいインデックスを作成し、接続されたデータベースに新しい IDBIndex オブジェクトを返します。
IDBObjectStore.delete()
IDBRequest オブジェクトを返し、そして、別のスレッドで、指定されたキーによって選択されたストア オブジェクトを削除します。これは、オブジェクト ストアから個々のレコードを削除するためのものです。
IDBObjectStore.deleteIndex()
接続されたデータベース内の指定されたインデックスを破棄し、バージョンのアップグレード中に使用されます。
IDBObjectStore.get()
IDBRequest オブジェクトを返し、および、別のスレッドで、指定されたキーによって選択されたストア オブジェクト ストアを返します。これは、オブジェクト ストアから特定のレコードを取得するためのものです。
IDBObjectStore.getKey()
IDBRequest オブジェクトを返し、および、別のスレッドで、指定されたパラメータに一致する格納されたオブジェクト内のオブジェクトのレコードキーを取得して返します。
IDBObjectStore.getAll()
IDBRequest オブジェクトを返します。指定されたパラメーターに一致するオブジェクト ストア内のすべてのオブジェクト、またはパラメーターが指定されていない場合はストア内のすべてのオブジェクトを取得します。
IDBObjectStore.getAllKeys()
IDBRequest オブジェクトを返します。指定されたパラメーターに一致するオブジェクト ストア内のすべてのオブジェクト、またはパラメーターが指定されていない場合はストア内のすべてのオブジェクトのレコード キーを取得します。
IDBObjectStore.index()
このオブジェクト ストアからインデックスを開きます。その後、たとえば、カーソルを使用してそのインデックスでソートされた一連のレコードを返すために使用できます。
IDBObjectStore.openCursor()
IDBRequest オブジェクトを返し、および、別のスレッドで、新しい IDBCursorWithValue オブジェクトを返します。カーソルを使用した主キーによるオブジェクト ストアの反復に使用されます。
IDBObjectStore.openKeyCursor()
IDBRequest オブジェクトを返し、および、別のスレッドで、新しい IDBCursor を返します。キーを使用してオブジェクト ストアを反復処理するために使用されます。
IDBObjectStore.put()
IDBRequest オブジェクトを返し、および、別のスレッドで、値の構造化されたクローンを作成し、クローンされた値をオブジェクト ストアに格納します。これは、トランザクションのモードが読み取り/書き込みのときに、オブジェクト ストア内の既存のレコードを更新するためのものです。
この例は、オブジェクト ストアのさまざまな使用法を示しています。onupgradeneeded 関数内の IDBObjectStore.createIndex でデータ構造を更新することから、IDBObjectStore.add を使用してオブジェクト ストアに新しいアイテムを追加します。完全に機能する例については、To-do通知アプリケーションをご覧ください(例をライブでご覧ください)。
// Let us open our database
var DBOpenRequest = window.indexedDB.open("toDoList", 4);

DBOpenRequest.onsuccess = function(event) {
  note.innerHTML += '<li>Database initialized.</li>';

  // store the result of opening the database in db.
  db = DBOpenRequest.result;
};

// This event handles the event whereby a new version of
// the database needs to be created Either one has not
// been created before, or a new version number has been
// submitted via the window.indexedDB.open line above
DBOpenRequest.onupgradeneeded = function(event) {
  var db = event.target.result;

  db.onerror = function(event) {
    note.innerHTML += '<li>Error loading database.</li>';
  };

  // Create an objectStore for this database

  var objectStore = db.createObjectStore("toDoList", { keyPath: "taskTitle" });

  // define what data items the objectStore will contain

  objectStore.createIndex("hours", "hours", { unique: false });
  objectStore.createIndex("minutes", "minutes", { unique: false });
  objectStore.createIndex("day", "day", { unique: false });
  objectStore.createIndex("month", "month", { unique: false });
  objectStore.createIndex("year", "year", { unique: false });

  objectStore.createIndex("notified", "notified", { unique: false });

  note.innerHTML += '<li>Object store created.</li>';
};

// Create a new item to add in to the object store
var newItem = [
  { taskTitle: "Walk dog", hours: 19, minutes: 30, day: 24, month: 'December', year: 2013, notified: "no" }
];

// open a read/write db transaction, ready for adding the data
var transaction = db.transaction(["toDoList"], "readwrite");

// report on the success of the transaction completing, when everything is done
transaction.oncomplete = function(event) {
  note.innerHTML += '<li>Transaction completed.</li>';
};

transaction.onerror = function(event) {
  note.innerHTML += '<li>Transaction not opened due to error. Duplicate items not allowed.</li>';
};

// create an object store on the transaction
var objectStore = transaction.objectStore("toDoList");
// make a request to add our newItem object to the object store
var objectStoreRequest = objectStore.add(newItem[0]);

objectStoreRequest.onsuccess = function(event) {
  note.innerHTML += '<li>Request successful .</li>';
}

テスト
HTML:

ここに表示
スクリプト組み込み:



スクリプト:


スクリプト:



表示内容:
表示内容: