IDBKeyRange - Web APIs | MDN
Instance properties
IDBKeyRange.lowerRead only-
Lower bound of the key range.
IDBKeyRange.upperRead only-
Upper bound of the key range.
IDBKeyRange.lowerOpenRead only-
Returns false if the lower-bound value is included in the key range.
IDBKeyRange.upperOpenRead only-
Returns false if the upper-bound value is included in the key range.
Static methods
IDBKeyRange.bound()-
Creates a new key range with upper and lower bounds.
IDBKeyRange.only()-
Creates a new key range containing a single value.
IDBKeyRange.lowerBound()-
Creates a new key range with only a lower bound.
IDBKeyRange.upperBound()-
Creates a new upper-bound key range.
Instance methods
IDBKeyRange.includes()-
Returns a boolean indicating whether a specified key is inside the key range.
Examples
The following example illustrates how you'd use a key range. Here we declare a keyRangeValue as a range between values of "A" and "F". We open a transaction (using IDBTransaction) and an object store, and open a cursor with IDBObjectStore.openCursor, declaring keyRangeValue 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 IDBKeyRange.bound("A", "F", true, true);, then the range would not include "A" and "F", only the values between them.
js
function displayData() {
const keyRangeValue = IDBKeyRange.bound("A", "F");
const transaction = db.transaction(["fThings"], "readonly");
const objectStore = transaction.objectStore("fThings");
objectStore.openCursor(keyRangeValue).onsuccess = (event) => {
const cursor = event.target.result;
if (cursor) {
const listItem = document.createElement("li");
listItem.textContent = `${cursor.value.fThing}, ${cursor.value.fRating}`;
list.appendChild(listItem);
cursor.continue();
} else {
console.log("Entries all displayed.");
}
};
}
Specifications
| Specification |
|---|
| Indexed Database API 3.0 # keyrange |
Browser compatibility
See also
- Using IndexedDB
- Starting transactions:
IDBDatabase - Using transactions:
IDBTransaction - Retrieving and making changes to your data:
IDBObjectStore - Using cursors:
IDBCursor - Reference example: To-do Notifications (View the example live).