admin管理员组文章数量:1317906
In my project I'm using browser's indexed-DB and I would like to retrieve some objects from the db with specific ids. According to MDN you could use ranges to get the results you want:
According to MDN:
// Only match "Donna"
var singleKeyRange = IDBKeyRange.only("Donna");
// Match anything past "Bill", including "Bill"
var lowerBoundKeyRange = IDBKeyRange.lowerBound("Bill");
// Match anything past "Bill", but don't include "Bill"
var lowerBoundOpenKeyRange = IDBKeyRange.lowerBound("Bill", true);
// Match anything up to, but not including, "Donna"
var upperBoundOpenKeyRange = IDBKeyRange.upperBound("Donna", true);
// Match anything between "Bill" and "Donna", but not including "Donna"
var boundKeyRange = IDBKeyRange.bound("Bill", "Donna", false, true);
// To use one of the key ranges, pass it in as the first argument of openCursor()/openKeyCursor()
index.openCursor(boundKeyRange).onsuccess = function(event) {
var cursor = event.target.result;
if (cursor) {
// Do something with the matches.
cursor.continue();
}
};
However what do you do if you wish to get an array of specific ids that are not in order and are not sequential (ex:[91,819,34,24,501]) with a single request?
In my project I'm using browser's indexed-DB and I would like to retrieve some objects from the db with specific ids. According to MDN you could use ranges to get the results you want:
According to MDN:
// Only match "Donna"
var singleKeyRange = IDBKeyRange.only("Donna");
// Match anything past "Bill", including "Bill"
var lowerBoundKeyRange = IDBKeyRange.lowerBound("Bill");
// Match anything past "Bill", but don't include "Bill"
var lowerBoundOpenKeyRange = IDBKeyRange.lowerBound("Bill", true);
// Match anything up to, but not including, "Donna"
var upperBoundOpenKeyRange = IDBKeyRange.upperBound("Donna", true);
// Match anything between "Bill" and "Donna", but not including "Donna"
var boundKeyRange = IDBKeyRange.bound("Bill", "Donna", false, true);
// To use one of the key ranges, pass it in as the first argument of openCursor()/openKeyCursor()
index.openCursor(boundKeyRange).onsuccess = function(event) {
var cursor = event.target.result;
if (cursor) {
// Do something with the matches.
cursor.continue();
}
};
However what do you do if you wish to get an array of specific ids that are not in order and are not sequential (ex:[91,819,34,24,501]) with a single request?
Share Improve this question asked Feb 17, 2015 at 14:58 0x_Anakin0x_Anakin 3,2695 gold badges50 silver badges90 bronze badges3 Answers
Reset to default 6 +100You can't do this with a single request, but you can fire off multiple requests in parallel:
var keys = [91,819,34,24,501];
var results = [];
keys.forEach(function(key) {
store.get(key).onsuccess = function(e) {
results.push(e.target.result);
if (results.length === keys.length) {
// you're done!
}
};
});
Note that requests are required to execute in order, so the order of the results
array will match the order of the keys
array. If a key is not found, the corresponding result index will contain undefined
.
(There's a feature request for Indexed DB to support this directly: https://github./w3c/IndexedDB/issues/19 but no final API design yet.)
Based on your ment, it sounds like you want to do something like SELECT objects FROM objects WHERE object.id = 1 OR object.id = 2 OR object.id = 5
, as a single query (request). Unfortunately, indexedDB cannot do OR
style queries (unions). It can only do AND
style queries (intersections).
There is a possible solution if the various ids you want to retrieve are known a-priori. You can simply store an additional group-id property, put the related ids into distinct groups, and then query by group-id. This obviously does not work if the ids you want to retrieve overlap (appear in more than one group), or are variable (object to group membership changes from query to query).
Getting multiple objects by keys in 1 go is possible by using a cursor.
The keys need to be sorted.
var keys = [91,819,34,24,501].sort(function(a, b) {
return a - b;
});
var results = [];
var i = 0;
var cursorReq = store.openCursor();
cursorReq.onsuccess = function(e) {
var c = e.target.result;
if(!c) { return; /* done */ }
while(c.key > keys[i]) {
i++;
if(i === keys.length) { return; /* done */ }
}
if(c.key === keys[i]) { // found
results.push(cursor.value);
c.continue();
} else { c.continue(keys[i]); }
}
本文标签: javascriptGetting specific ids from IndexeddbStack Overflow
版权声明:本文标题:javascript - Getting specific ids from Indexeddb - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742030678a2416383.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论