IndexedDB之键值范围(Key Ranges)

欢欢欢欢 发表于 2018-2-27 15:57

Key Ranges 键值范围

Working with cursors may seem suboptimal since you’re limited in the ways data can be retrieved. Key ranges are used to make working with cursors a little more manageable. A key range is represented by an instance of IDBKeyRange. The standard version is IDBKeyRange, which is supported in Internet Explorer 10+ and Firefox 4+, while Chrome supports this type with webkitIDBKeyRange. As with other types related to IndexedDB, you’ll first need to create a local copy, taking these differences into account:

翻译:

使用光标似乎有点不完美,因为你被限制在了数据取出的方式里。键值范围的使用让光标更加可控。一个键值范围是IDBKeyRange的一个实例。IDBKeyRange是标准版本,IE10+和FF4+支持,Chrome支持webkitIDBKeyRange。和其他IndexDB的类型一样,你首先需要声明一个兼容的本地版本:

var IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange;

There are four different ways to specify key ranges. The first is to use the only() method and pass in the key you want to retrieve:

翻译:

有四种方式来指定键值范围。第一个方法是only(),传入你想要取的数据的键值。

var onlyRange = IDBKeyRange.only(“007”);

This range ensures that only the value with a key of “007” will be retrieved. A cursor created using this range is similar to directly accessing an object store and calling get(“007”).

翻译:

这个键值范围确保只有键值为“007”的对象被取出。使用这种键值范围创建的光标和直接访问一个对象存储然后调用get("007")是相似的。

The second type of range defines a lower bound for the result set. The lower bound indicates the item at which the cursor should start. For example, the following key range ensures the cursor starts at the key “007” and continues until the end:

翻译:

键值范围的第二种创建方法是为结果集设置一个下限。下限指明了光标的起始条目。例如,下面的键值范围确保了光标开始于键值"007",然后直到结束。

//start at item “007”, go to the end

var lowerRange = IDBKeyRange.lowerBound(“007”);

If you want to start at the item immediately following the value at “007”, then you can pass in a second argument of true:

翻译:

假如你想要冲键值"007"之后一个条目开始,那么传第二个参数为true:

//start at item after “007”, go to the end

var lowerRange = IDBKeyRange.lowerBound(“007”, true);

The third type of range is an upper bound, indicating the key you don’t want to go past by using the upperBound() method. The following key ensures that the cursor starts at the beginning and stops when it gets to the value with key “ace”:

翻译:

第三种是设置上限。通过方法upperBound()标明你不想经过的key。下面的键值确保光标从头开始然后当遇到键值"ace"时停止。

//start at beginning, go to “ace”

var upperRange = IDBKeyRange.upperBound(“ace”);

If you don’t want to include the given key, then pass in true as the second argument:

假如你不想包含给定的键值,那么第二个参数传true:

//start at beginning, go to the item just before “ace”

var upperRange = IDBKeyRange.upperBound(“ace”, true);

To specify both a lower and an upper bound, use the bound() method. This method accepts four arguments, the lower bound key, the upper bound key, an optional Boolean indicating to skip the lower bound, and an optional Boolean indicating to skip the upper bound. Here are some examples:

翻译:

如果要同时指定上限和下限,那么使用方法bound()。这个方法接受四个参数,下限键值,上限键值,可选布尔值指明是否跳过下限,和可选布尔值指明是否跳过上限。这是一些例子:

//start at “007”, go to “ace”

var boundRange = IDBKeyRange.bound(“007”, “ace”);

//start at item after “007”, go to “ace”

var boundRange = IDBKeyRange.bound(“007”, “ace”, true);

//start at item after “007”, go to item before “ace”

var boundRange = IDBKeyRange.bound(“007”, “ace”, true, true);

//start at “007”, go to item before “ace”

var boundRange = IDBKeyRange.bound(“007”, “ace”, false, true);

Once you have defined a range, pass it into the openCursor() method and you’ll create a cursor that stays within the constraints:

一旦你定义了一个键值范围,并且把他传递进了方法openCursor(),那么你将创建一个停留在指定限制的光标。

var store = db.transaction(“users”).objectStore(“users”), range = IDBKeyRange.bound(“007”, “ace”);

request = store.openCursor(range);

request.onsuccess = function(event){

var cursor = event.target.result;

if (cursor){ //always check

console.log(“Key: “ + cursor.key + “, Value: “ + JSON.stringify(cursor.value));

cursor.continue(); //go to the next one } else { console.log(“Done!”); } };

This example outputs only the values between keys “007” and “ace”, which are fewer than the previous section’s example.

翻译:

这个例子只输出介于"007"和"ace"的值,它比上一章节的例子少很多。