1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
---
title: IDBKeyRange
slug: Web/API/IDBKeyRange
tags:
- API
- Database
- IDBKeyRange
- IndexedDB
- Interface
- NeedsTranslation
- Reference
- Storage
- TopicStub
translation_of: Web/API/IDBKeyRange
---
<p>{{APIRef("IndexedDB")}}</p>
<div>
<p>The <strong><code>IDBKeyRange</code></strong> interface of the <a href="/en/IndexedDB" title="en/IndexedDB">IndexedDB API</a> represents a continuous interval over some data type that is used for keys. Records can be retrieved from {{domxref("IDBObjectStore")}} and {{domxref("IDBIndex")}} objects using keys or a range of keys. You can limit the range using lower and upper bounds. For example, you can iterate over all values of a key in the value range A–Z.</p>
</div>
<p>A key range can be a single value or a range with upper and lower bounds or endpoints. If the key range has both upper and lower bounds, then it is <em>bounded</em>; if it has no bounds, it is <em>unbounded</em>. A bounded key range can either be open (the endpoints are excluded) or closed (the endpoints are included). To retrieve all keys within a certain range, you can use the following code constructs:</p>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Range</th>
<th scope="col">Code</th>
</tr>
<tr>
<td>All keys ≤ <strong>x</strong></td>
<td>{{domxref("IDBKeyRange.upperBound")}}<code>(<strong>x</strong>)</code></td>
</tr>
<tr>
<td>All keys < <strong>x</strong></td>
<td>{{domxref("IDBKeyRange.upperBound")}}<code>(<strong>x</strong>, true) </code></td>
</tr>
<tr>
<td>All keys ≥<strong> y</strong></td>
<td>{{domxref("IDBKeyRange.lowerBound")}}<code>(<strong>y</strong>)</code></td>
</tr>
<tr>
<td>All keys ><strong> y</strong></td>
<td>{{domxref("IDBKeyRange.lowerBound")}}<code>(<strong>y</strong>, true)</code></td>
</tr>
<tr>
<td>All keys ≥ <strong>x</strong> && ≤ <strong>y</strong></td>
<td>{{domxref("IDBKeyRange.bound")}}<code>(<strong>x</strong>, <strong>y</strong>)</code></td>
</tr>
<tr>
<td>All keys > <strong>x</strong> &&< <strong>y</strong></td>
<td>{{domxref("IDBKeyRange.bound")}}<code>(<strong>x</strong>, <strong>y</strong>, true, true)</code></td>
</tr>
<tr>
<td>All keys > <strong>x</strong> && ≤ <strong>y</strong></td>
<td>{{domxref("IDBKeyRange.bound")}}<code>(<strong>x</strong>, <strong>y</strong>, true, false)</code></td>
</tr>
<tr>
<td>All keys ≥ <strong>x</strong> &&< <strong>y</strong></td>
<td>{{domxref("IDBKeyRange.bound")}}<code>(<strong>x</strong>, <strong>y</strong>, false, true)</code></td>
</tr>
<tr>
<td>The key = <strong>z</strong></td>
<td>{{domxref("IDBKeyRange.only")}}<code>(<strong>z</strong>)</code></td>
</tr>
</thead>
</table>
<p>A key is in a key range if the following conditions are true:</p>
<ul>
<li>The lower value of the key range is one of the following:
<ul>
<li><code>undefined</code></li>
<li>Less than key value</li>
<li>Equal to key value if <code>lowerOpen</code> is <code>false</code>.</li>
</ul>
</li>
<li>The upper value of the key range is one of the following:
<ul>
<li><code>undefined</code></li>
<li>Greater than key value</li>
<li>Equal to key value if <code>upperOpen</code> is <code>false</code>.</li>
</ul>
</li>
</ul>
<p>{{AvailableInWorkers}}</p>
<h2 id="Properties">Properties</h2>
<dl>
<dt>{{domxref("IDBKeyRange.lower")}} {{readonlyInline}}</dt>
<dd>Lower bound of the key range.</dd>
<dt>{{domxref("IDBKeyRange.upper")}} {{readonlyInline}}</dt>
<dd>Upper bound of the key range.</dd>
<dt>{{domxref("IDBKeyRange.lowerOpen")}} {{readonlyInline}}</dt>
<dd>Returns false if the lower-bound value is included in the key range.</dd>
<dt>{{domxref("IDBKeyRange.upperOpen")}} {{readonlyInline}}</dt>
<dd>Returns false if the upper-bound value is included in the key range.</dd>
</dl>
<h2 id="Methods">Methods</h2>
<h3 id="Static_methods">Static methods</h3>
<dl>
<dt>{{domxref("IDBKeyRange.bound()")}}</dt>
<dd>Creates a new key range with upper and lower bounds.</dd>
<dt>{{domxref("IDBKeyRange.only()")}}</dt>
<dd>Creates a new key range containing a single value.</dd>
<dt>{{domxref("IDBKeyRange.lowerBound()")}}</dt>
<dd>Creates a new key range with only a lower bound.</dd>
<dt>{{domxref("IDBKeyRange.upperBound()")}}</dt>
<dd>Creates a new upper-bound key range.</dd>
</dl>
<h3 id="Instance_methods">Instance methods</h3>
<dl>
<dt>{{domxref("IDBKeyRange.includes()")}}</dt>
<dd>Returns a boolean indicating whether a specified key is inside the key range.</dd>
</dl>
<h2 id="Examples">Examples</h2>
<p>The following example illustrates how you'd use a key range. Here we declare a <code>keyRangeValue</code> as a range between values of "A" and "F". We open a transaction (using {{domxref("IDBTransaction")}}) and an object store, and open a Cursor with {{domxref("IDBObjectStore.openCursor")}}, declaring <code>keyRangeValue</code> as its optional key range value. This means that the cursor will only retrieve records with keys inside that range. This range includes the values "A" and "F", as we haven't declared that they should be open bounds. If we used <span style="background-color: #fafbfc; font-family: consolas,monaco,andale mono,monospace; font-size: 1rem; line-height: 19px; white-space: pre;">IDBKeyRange.bound("A", "F", true, true);</span>, then the range would not include "A" and "F", only the values between them.</p>
<div class="note">
<p><strong>Note</strong>: For a more complete example allowing you to experiment with key range, have a look at our <a href="https://github.com/mdn/indexeddb-examples/tree/master/idbkeyrange">IDBKeyRange-example</a> repo (<a href="https://mdn.github.io/indexeddb-examples/idbkeyrange/">view the example live too</a>.)</p>
</div>
<pre class="brush: js">function displayData() {
var keyRangeValue = IDBKeyRange.bound("A", "F");
var transaction = db.transaction(['fThings'], 'readonly');
var objectStore = transaction.objectStore('fThings');
objectStore.openCursor(keyRangeValue).onsuccess = function(event) {
var cursor = event.target.result;
if(cursor) {
var listItem = document.createElement('li');
listItem.innerHTML = '<strong>' + cursor.value.fThing + '</strong>, ' + cursor.value.fRating;
list.appendChild(listItem);
cursor.continue();
} else {
console.log('Entries all displayed.');
}
};
}</pre>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('IndexedDB', '#idl-def-IDBKeyRange', 'IDBKeyRange')}}</td>
<td>{{Spec2('IndexedDB')}}</td>
<td>Initial definition.</td>
</tr>
<tr>
<td>{{SpecName('IndexedDB 2', '#keyrange', 'IDBKeyRange')}}</td>
<td>{{Spec2('IndexedDB 2')}}</td>
<td>Adds <code>includes()</code>.</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility" name="Browser_compatibility">Browser compatibility</h2>
<div>
<p>{{Compat("api.IDBKeyRange")}}</p>
</div>
<h2 id="See_also">See also</h2>
<ul>
<li><a href="/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB">Using IndexedDB</a></li>
<li>Starting transactions: {{domxref("IDBDatabase")}}</li>
<li>Using transactions: {{domxref("IDBTransaction")}}</li>
<li>Setting a range of keys: {{domxref("IDBKeyRange")}}</li>
<li>Retrieving and making changes to your data: {{domxref("IDBObjectStore")}}</li>
<li>Using cursors: {{domxref("IDBCursor")}}</li>
<li>Reference example: <a class="external" href="https://github.com/mdn/to-do-notifications/tree/gh-pages">To-do Notifications</a> (<a class="external" href="http://mdn.github.io/to-do-notifications/">view example live</a>.)</li>
</ul>
|