IDBIndex - Web API 接口参考 编辑

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
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 只读
index的名称。
IDBIndex.objectStore 只读
index所指向的object store的名称。
IDBIndex.keyPath 只读
此index的关键路径。它如果为null,则index不是自动填充的。
IDBIndex.multiEntry 只读
影响index的key路径的计算结果生成数组时索引的行为方式。如果为true,那么对于key数组中的每一项,索引中都有一条记录。如果为false,那么对于数组中的每个key都有一条记录。
IDBIndex.unique 只读
如果为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

SpecificationStatusComment
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!
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support12-webkit
23
4.0 (2.0)1017未实现
count()2310.0 (10.0)1017未实现
getAll() and getAllKeys()未实现24.0 (24.0)
behind dom.indexedDB.experimental  pref
未实现未实现未实现
FeatureAndroidFirefox Mobile (Gecko)Firefox OSIE PhoneOpera MobileSafari Mobile
Basic support4.44.0 (2.0)1.0.11017未实现
count()4.410.0 (10.0)1.0.11017未实现
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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:75 次

字数:11397

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文