From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/api/idbindex/index.html | 214 ++++++++++++++++++++++++++++++++ 1 file changed, 214 insertions(+) create mode 100644 files/zh-cn/web/api/idbindex/index.html (limited to 'files/zh-cn/web/api/idbindex') diff --git a/files/zh-cn/web/api/idbindex/index.html b/files/zh-cn/web/api/idbindex/index.html new file mode 100644 index 0000000000..cb32afc0c0 --- /dev/null +++ b/files/zh-cn/web/api/idbindex/index.html @@ -0,0 +1,214 @@ +--- +title: IDBIndex +slug: Web/API/IDBIndex +translation_of: Web/API/IDBIndex +--- +

{{APIRef()}}

+ +
+

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

+
+ +

你可以通过记录的键或使用一个index取回一个object store中的这些记录 (cursors 提供了第三种方式:请见 {{ domxref("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,点击这里查看更多: {{domxref("IDBKeyRange")}}.

+ +

Methods

+ +

Inherits from: EventTarget

+ +
+
{{domxref("IDBIndex.count")}}
+
Returns an {{domxref("IDBRequest")}} object, and in a separate thread, returns the number of records within a key range.
+
{{domxref("IDBIndex.get")}}
+
Returns an {{domxref("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.
+
{{domxref("IDBIndex.getAll")}} {{ Non-Standard_inline() }}
+
Instantly retrieves all objects inside an {{domxref("IDBObjectStore")}}, setting them as the result of the request object.
+
{{domxref("IDBIndex.getKey")}}
+
Returns an {{domxref("IDBRequest")}} object, and, in a separate thread, finds either the given key or the primary key, if key is a key range.
+
{{domxref("IDBIndex.getAllKeys")}} {{ Non-Standard_inline() }}
+
Instantly retrieves the keys of all objects inside an {{domxref("IDBObjectStore")}}, setting them as the result of the request object.
+
{{domxref("IDBIndex.openCursor")}}
+
Returns an {{domxref("IDBRequest")}} object, and, in a separate thread, creates a cursor over the specified key range.
+
{{domxref("IDBIndex.openKeyCursor")}} {{ Non-Standard_inline() }}
+
Returns an {{domxref("IDBRequest")}} object, and, in a separate thread, creates a cursor over the specified key range, as arranged by this index.
+
+ +

Properties

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

Browser compatibility

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support12{{ property_prefix("-webkit") }}
+ 23
{{ CompatGeckoDesktop("2.0") }}1017{{ CompatNo() }}
count()23{{ CompatGeckoDesktop("10.0") }}1017{{ CompatNo() }}
getAll() and getAllKeys(){{ CompatNo() }}{{ CompatGeckoDesktop("24.0") }}
+ behind dom.indexedDB.experimental  pref
{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)Firefox OSIE PhoneOpera MobileSafari Mobile
Basic support4.4{{ CompatGeckoMobile("2.0") }}1.0.11017{{ CompatNo() }}
count()4.4{{ CompatGeckoMobile("10.0") }}1.0.11017{{ CompatNo() }}
getAll() and getAllKeys(){{ CompatNo() }}{{ CompatGeckoDesktop("24.0") }}
+ behind dom.indexedDB.experimental  pref
1.1 behind
+ dom.indexedDB.experimental  pref
{{ CompatNo() }}{{ CompatNo() }}{{ CompatNo() }}
+
+ +
+

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

+ + -- cgit v1.2.3-54-g00ecf