IDBIndex

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

你可以通过记录的键或使用一个index取回一个object store中的这些记录 (cursors 提供了第三种方式:请见 IDBCursor)。一个index可以让你在object store的records中,通过使用records的properties(属性)来寻找records。

index是一个持久的键-值存储,其中其记录的值部分是被引用object store中的record的关键部分。在object store中新增、更新或是删除records时,索引中的records将自动填充。索引中的每条记录只能指向其引用的object  store中的唯一一条记录,但是多个索引可以引用同一个object store。当object store变更时,所有引用object store的索引都会自动更新。

索引中的records总是按照records的key进行排序。然而,不像object stores,一个给定的index可以包含具有相同key的多条记录。这些records将根据被引用object store中的主键进一步排序。

你可以设置在一个范围内的key,点击这里查看更多: IDBKeyRange.

Methods

Inherits from: EventTarget

IDBIndex.count (en-US)
Returns an IDBRequest object, and in a separate thread, returns the number of records within a key range.
IDBIndex.get (en-US)
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 (en-US) This API has not been standardized.
Instantly retrieves all objects inside an IDBObjectStore, setting them as the result of the request object.
IDBIndex.getKey (en-US)
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 (en-US) This API has not been standardized.
Instantly retrieves the keys of all objects inside an IDBObjectStore, setting them as the result of the request object.
IDBIndex.openCursor (en-US)
Returns an IDBRequest object, and, in a separate thread, creates a cursor over the specified key range.
IDBIndex.openKeyCursor (en-US) This API has not been standardized.
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 (en-US) 只读
index的名称。
IDBIndex.objectStore (en-US) 只读
index所指向的object store的名称。
IDBIndex.keyPath (en-US) 只读
此index的关键路径。它如果为null,则index不是自动填充的。
IDBIndex.multiEntry (en-US) 只读
影响index的key路径的计算结果生成数组时索引的行为方式。如果为true,那么对于key数组中的每一项,索引中都有一条记录。如果为false,那么对于数组中的每个key都有一条记录。
IDBIndex.unique (en-US) 只读
如果为true,这个index不会允许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 2.0
IDBIndex
Recommendation

Browser compatibility

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help! (en-US)

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 12-webkit (en-US)
23
4.0 (2.0) 10 17 未实现
count() 23 10.0 (10.0) 10 17 未实现
getAll() and getAllKeys() 未实现 24.0 (24.0)
behind dom.indexedDB.experimental  pref
未实现 未实现 未实现
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 未实现
count() 4.4 10.0 (10.0) 1.0.1 10 17 未实现
getAll() and getAllKeys() 未实现 24.0 (24.0)
behind dom.indexedDB.experimental  pref
1.1 behind
dom.indexedDB.experimental  pref
未实现 未实现 未实现

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.