IDBIndex

» IDBIndex

IndexedDB API 中的IDBIndex接口提供了异步获取数据库中一个index的功能。index是一种用于在另一个object store中查找记录的object store,其被称为被引用的object store。你可以通过使用该接口来取回数据。

你可以通过记录的键或使用一个index取回一个object store中的这些记录 (cursors 提供了第三种方式:请见 IDBCursor)。An index lets you look up records in an object store using properties of the values in the object stores records.

The index is a persistent key-value storage where the value part of its records is the key part of a record in the referenced object store. The records in an index are automatically populated whenever records in the referenced object store are inserted, updated, or deleted. Each record in an index can point to only one record in its referenced object store, but several indexes can reference the same object store. When the object store changes, all indexes that refers to the object store are automatically updated.

The records in an index are always sorted according to the records key. However, unlike object stores, a given index can contain multiple records with the same key. Such records are further sorted according to primary key in the referenced object store.

You can grab a set of keys within a range. To learn more, see IDBKeyRange.

Methods

Inherits from: EventTarget

IDBIndex.count
Returns an IDBRequest object, and in a separate thread, returns the number of records within a key range.
IDBIndex.get
Returns an IDBRequest object, and, in a separate thread, finds either the value in the referenced object store that corresponds to the given key or the first corresponding value, if key is a key range.
IDBIndex.getAll
Instantly retrieves all objects inside an IDBObjectStore, setting them as the result of the request object.
IDBIndex.getKey
Returns an IDBRequest object, and, in a separate thread, finds either the given key or the primary key, if key is a key range.
IDBIndex.getAllKeys
Instantly retrieves the keys of all objects inside an IDBObjectStore, setting them as the result of the request object.
IDBIndex.openCursor
Returns an IDBRequest object, and, in a separate thread, creates a cursor over the specified key range.
IDBIndex.openKeyCursor
Returns an IDBRequest object, and, in a separate thread, creates a cursor over the specified key range, as arranged by this index.

Properties

IDBIndex.name Read only
The name of this index.
IDBIndex.objectStore Read only
The name of the object store referenced by this index.
IDBIndex.keyPath Read only
The key path of this index. If null, this index is not auto-populated.
IDBIndex.multiEntry Read only
Affects how the index behaves when the result of evaluating the index's key path yields an array. If true, there is one record in the index for each item in an array of keys. If false, then there is one record for each key that is an array.
IDBIndex.unique Read only
If true, this index does not allow duplicate values for a key.

Examples

Opening a transaction then using get() to retrieve an object of known key:

// Let us open our database
var request = window.indexedDB.open("toDoList", 4);
// these two event handlers act on the database being opened successfully, or not
request.onerror = function(event) {
  note.innerHTML += '<li>Error loading database.</li>';
};
request.onsuccess = function(event) {
note.innerHTML += '<li>Database initialised.</li>';
// store the result of opening the database in the db variable.
db = request.result;
// Open a transaction on the current database and get a reference to the object store
//that we want to pull information out of
var transaction = db.transaction(["toDoList"]);
var objectStore = transaction.objectStore("toDoList");
// Use get() to get a specific object from the object store, the key of which is "Walk dog"
var request = objectStore.get("Walk dog");
request.onerror = function(event) {
  console.log("There is no record stored for " + request.result.taskTitle);
};
request.onsuccess = function(event) {
  // Do something with the request.result!
  console.log("The deadline time for " + request.result.taskTitle + " is " +
              request.result.hours + ":" + request.result.minutes + ".";
};

Note: need to work out a way to retrieve a series/range of objects using an index, or just all of them. Is this possible with get, or is this a job for cursor?

Specifications

Specification Status Comment
Indexed Database API
IDBIndex
Candidate Recommendation  

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 12-webkit
23
4.0 (2.0) 10 17 Not supported
count() 23 10.0 (10.0) 10 17 Not supported
getAll() and getAllKeys() Not supported 24.0 (24.0)
behind dom.indexedDB.experimental  pref
Not supported Not supported Not supported
Feature Android Firefox Mobile (Gecko) Firefox OS IE Phone Opera Mobile Safari Mobile
Basic support 4.4 4.0 (2.0) 1.0.1 10 17 Not supported
count() 4.4 10.0 (10.0) 1.0.1 10 17 Not supported
getAll() and getAllKeys() Not supported 24.0 (24.0)
behind dom.indexedDB.experimental  pref
1.1 behind
dom.indexedDB.experimental  pref
Not supported Not supported Not supported

Be careful in Chrome as it still implements the old specification along the new one. Similarly it still has the prefixed webkitIndexedDB property even if the unprefixed indexedDB is present.

See also

To learn more about various topics, see the following

  • Starting transactions: IDBDatabase
  • Setting transaction modes: IDBTransaction
  • Setting a range of keys: IDBKeyRange
  • The reference application for the examples in this reference: To-do Notifications (view example live.) Not every snippet appears in this example, but every example uses the same data structure and syntax, and they will make sense in the context of this example.

文档标签和贡献者