Reputation: 11
I'm using this plugin: https://github.com/axemclion/jquery-indexeddb/tree/gh-pages I want to use indexes but I can't understand how the get Method works on indexes. My idea is to search by given index, e.g. $.indexedDB("OfflineDB").objectStore("users").index("userName").get("My user name") So I want to find the data for user with name "My user name" directly, I don't want to iterate over all stored objects because it would be too slowly. Any ideas how I could make this work?
Upvotes: 0
Views: 4081
Reputation: 11
var index = store.index("name");
var request = index.get("frstname");
request.onsuccess = function () {
var matching = request.result;
if (matching !== undefined) {
// A match was found.
report(matching.isbn, matching.title, matching.author);
} else {
// No match was found.
report(null);
}
};
Upvotes: 1
Reputation: 18690
Using the get method on an index does not iterate over all stored objects in an object store. Looking at the source code of this wrapper, the get method just wraps the normal IDBIndex.get method. According to the MDB documentation, "[t]he IDBIndex.get method ... finds ... the value in the referenced object store that corresponds to the given key ..." According to the indexedDB spec, IDBIndex.get follows the normal steps for retrieving a value by key. This essentially involves the following step: "If key is not a key range then find the first record with key key from index. If key is a key range, then find the first record from index whose key is in key."
Retrieving an object by key does not involve explicit iteration. It is similar to using a hashmap to obtain a value for a key. It is similar to looking up the value in an array by specifying its index.
For example, it is basically doing this:
var data = {"My user name": user1, "other username": user2,
"another username": user3};
function lookupValueForKey(username, index) {
return index[username];
}
var user = lookupValueforKey("My user name", data);
It is not doing this:
function lookupValueForKey(username, index) {
for(var key in index) {
if(key == username) {
return index[key];
}
}
}
Because that would defeat the entire purpose of using an index. That does not even make sense.
Upvotes: 0