IndexedDB之索引(Indexes)

欢欢欢欢 发表于 2018-2-28 16:13

Indexes

For some data sets, you may want to specify more than one key for an object store. For example, if you’re tracking users by both a user ID and a username, you may want to access records using either piece of data. To do so, you would likely consider the user ID as the primary key and create an index on the username. 

翻译:

对于有些数据集来说,你可能想要指定不止一个的键值。例如,假如你想即通过用户ID又通过用户名来定位用户,你想要访问任意的这两块数据。为了这样做,你可能会考虑用户ID作为主键然后再用户名上创建一个索引。

To create a new index, first retrieve a reference to the object store and then call createIndex(), as in this example:

翻译:

为了创建一个新的索引,首先引用对象存储,然后调用方法createIndex(),如下例:

var store = db.transaction(“users”).objectStore(“users”), index = store.createIndex(“username”, “username”, { unique: true });

The first argument to createIndex() is the name of the index, the second is the name of the property to index, and third is an options object containing the key unique. This option should always be specified so as to indicate whether or not the key is unique across all records. Since username may not be duplicated, this index is not unique.

 翻译:

方法createIndex()的第一个参数是索引名,第二个是相对索引的属性名,第三个是一个选项,限制键值的唯一性。这个选项应该总是指定,以便指明所有记录里面键值是否唯一。既然用户名可能不是复制的,那么索引也不是唯一的。

The returned value from createIndex() is an instance of IDBIndex. You can also retrieve the same instance via the index() method on an object store. For example, to use an already existing index named “username”, the code would be:

翻译:

方法createIndex()返回是IDBIndex的实例。对象存储上的方法index()也能取出相同的实例。例如,使用已有的名为"username"的索引,代码如下:

var store = db.transaction(“users”).objectStore(“users”), index = store.index(“username”);

An index acts a lot like an object store. You can create a new cursor on the index using the openCursor() method, which works exactly the same as openCursor() on an object store except that the result.key property is filled in with the index key instead of the primary key. Here’s an example:

翻译:

一个索引更像一个对象存储。你能使用方法openCursor()在索引上创建一个新的光标。该光标和在对象存储上通过方法openCursor()创建的光标类似,除了一点,结果中的键值属性是索引的键值而不是主键。如下例:

var store = db.transaction(“users”).objectStore(“users”),

index = store.index(“username”), request = index.openCursor();

request.onsuccess = function(event){

//handle success };

An index can also create a special cursor that returns just the primary key for each record using the openKeyCursor() method, which accepts the same arguments as openCursor(). The big difference is that event.result.key is the index key and event.result.value is the primary key instead of the entire record.

翻译:

一个索引通过方法openKeyCursor()也能创建返回主键的光标,它接受和openCursor()一样的参数。最大的不同是event.result.key是索引键值而event.result.value是主键而不是整个记录。

var store = db.transaction(“users”).objectStore(“users”),

index = store.index(“username”), request = index.openKeyCursor();

request.onsuccess = function(event){

//handle success

//event.result.key is the index key, event.result.value is the primary key };

You can also retrieve a single value from an index by using get() and passing in the index key, which creates a new request:

翻译:通过方法get()然后传入索引键值,你也能从一个索引中取出一个单一值。他也创建了一个请求:

var store = db.transaction(“users”).objectStore(“users”), index = store.index(“username”), request = index.get(“007”);

request.onsuccess = function(event){ //handle success };

request.onfailure = function(event){ //handle failure };

To retrieve just the primary key for a given index key, use the getKey() method. This also creates a new request but result.value is equal to the primary key value rather than the entire record:

翻译:

为了取出一个已有索引键值的主键,可以使用方法getKey()。该方法也创建了一个新的请求,但result.value等于主键而不是整个记录。

var store = db.transaction(“users”).objectStore(“users”), index = store.index(“username”), request = index.getKey(“007”);

request.onsuccess = function(event){

//handle success

//event.result.key is the index key, event.result.value is the primary key };

In the onsuccess event handler in this example, event.result.value would be the user ID.

翻译:

该示例中的onsuccess事件柄,event.result.value将是用户的ID。

At any point in time, you can retrieve information about the index by using properties on the IDBIndex object:

翻译:

在任何时间点,通过IDBIndex对象的属性,你能获得索引的信息:

name — The name of the index.

keyPath — The property path that was passed into createIndex().

objectStore — The object store that this index works on.

unique — A Boolean indicating if the index key is unique.

翻译:

name — 索引名。

keyPath — 被传入createIndex()的属性路径。

objectStore — 索引隶属于的对象存储。

unique — 标识索引键值是否唯一的布尔值。

The object store itself also tracks the indexes by name in the indexNames property. This makes it easy to figure out which indexes already exist on an object using the following code:

翻译:

对象存储本身通过属性indexNames中的名字也能定位索引。这使得通过下列代码找到对象中已经存在哪些索引变得简单:

var store = db.transaction(“users”).objectStore(“users”),

indexNames = store.indexNames, index, i = 0, len = indexNames.length;

while(i < len){

index = store.index(indexNames[i++]);

console.log(“Index name: “ + index.name + “, KeyPath: “ + index.keyPath + “, Unique: “ + index.unique); }

This code iterates over each index and outputs its information to the console.

翻译:

上述代码遍历了所有索引,并且将索引信息输出到控制台。

An index can be deleted by calling the deleteIndex() method on an object store and passing in the name of the index:

翻译:

通过调用对象存储上的方法deleteIndex()并传入索引名,可以删除一个索引。

var store = db.transaction(“users”).objectStore(“users”);

store.deleteIndex(“username”);

Since deleting an index doesn’t touch the data in the object store, the operation happens without any callbacks.

翻译:

因为删除索引不影响对象存储的数据,该操作没有任何回调。