IndexedDB 搜索键并排序
IndexedDB search for key and sort
目前我有两个功能。一种用于查询数据库中具有特定 key/value 对的结果,另一种用于按特定键排序的 getAll 行。
有没有办法将这两者结合起来?所以我想有一个功能,你可以在商店中搜索 a=1 按 b asc 排序的对象。
按索引获取结果的函数:
getByIndex : function(store,indexKey,indexValue,callback){
var results = [];
this._initTransaction(store,"readonly",function(err,transaction){
var index = transaction.objectStore(store).index(indexKey);
index.openCursor(IDBKeyRange.only(indexValue)).onsuccess = function(event) {
var cursor = event.target.result;
if (cursor) {
results.push(cursor.value);
cursor.continue();
} else {
callback(true,results);
}
};
})
}
获取按键排序结果的函数:
getSorted : function(store,sortKey,callback){
var results = [];
this._initTransaction(store,"readonly",function(err,transaction){
var index = transaction.objectStore(store).index(sortKey);
index.openCursor(null,'next').onsuccess = function(event) {
var cursor = event.target.result;
if (cursor) {
results.push(cursor.value);
cursor.continue();
} else {
callback(true,results);
}
};
})
},
感谢您的帮助!
为两个属性创建索引:
function myOnUpgradeNeeded(event) {
// ...
var index = store.createIndex('myIndex', ['a', 'b']);
}
定义一个在索引上打开有界游标并迭代的函数:
function query(db, a, callback) {
if(!a) {
throw new TypeError('this only works if a is defined');
}
var results = [];
var tx = db.transaction('store');
tx.oncomplete = function(event) {
callback(results);
};
var store = tx.objectStore('store');
var index = store.index('myIndex');
var lowerBound = [a];
// How you define the upper bound depends on the type. This assumes
// that a is a number. For example, if a is a string, you need to
// use a different boundary definition. See the gist by Mr. Bell
// below for an example of how to create other types of bounds. You
// simply need to determine whatever is the 'next' value that would
// come after `a`s value in your data.
var upperBound = [a+1];
var range = IDBKeyRange.bound(lowerBound, upperBound, false, true);
var request = index.openCursor(range);
request.onsuccess = function(event) {
var cursor = event.target.result;
if(cursor) {
results.push(cursor.value);
cursor.continue();
}
};
}
然后调用它:
var request = indexedDB.open(...);
request.onsuccess = function(event) {
var db = event.target.result;
query(db, 1, function(results) {
// ...
});
};
这里比较的是不同长度的数组键路径,所以即使索引定义在[a,b]上,范围也只比较[a]...[a+1]。比较不同长度的数组键路径是完全可以的,比较只比较最短数组长度的值。
索引中条目的顺序按照 [a,b]。因此,如果您将索引条目限制为包含 a 的条目,那么您将获得一个按 b 排序的子集,其中每个 a 都是目标 a,这就是我所理解的 objective.
目前我有两个功能。一种用于查询数据库中具有特定 key/value 对的结果,另一种用于按特定键排序的 getAll 行。 有没有办法将这两者结合起来?所以我想有一个功能,你可以在商店中搜索 a=1 按 b asc 排序的对象。
按索引获取结果的函数:
getByIndex : function(store,indexKey,indexValue,callback){
var results = [];
this._initTransaction(store,"readonly",function(err,transaction){
var index = transaction.objectStore(store).index(indexKey);
index.openCursor(IDBKeyRange.only(indexValue)).onsuccess = function(event) {
var cursor = event.target.result;
if (cursor) {
results.push(cursor.value);
cursor.continue();
} else {
callback(true,results);
}
};
})
}
获取按键排序结果的函数:
getSorted : function(store,sortKey,callback){
var results = [];
this._initTransaction(store,"readonly",function(err,transaction){
var index = transaction.objectStore(store).index(sortKey);
index.openCursor(null,'next').onsuccess = function(event) {
var cursor = event.target.result;
if (cursor) {
results.push(cursor.value);
cursor.continue();
} else {
callback(true,results);
}
};
})
},
感谢您的帮助!
为两个属性创建索引:
function myOnUpgradeNeeded(event) {
// ...
var index = store.createIndex('myIndex', ['a', 'b']);
}
定义一个在索引上打开有界游标并迭代的函数:
function query(db, a, callback) {
if(!a) {
throw new TypeError('this only works if a is defined');
}
var results = [];
var tx = db.transaction('store');
tx.oncomplete = function(event) {
callback(results);
};
var store = tx.objectStore('store');
var index = store.index('myIndex');
var lowerBound = [a];
// How you define the upper bound depends on the type. This assumes
// that a is a number. For example, if a is a string, you need to
// use a different boundary definition. See the gist by Mr. Bell
// below for an example of how to create other types of bounds. You
// simply need to determine whatever is the 'next' value that would
// come after `a`s value in your data.
var upperBound = [a+1];
var range = IDBKeyRange.bound(lowerBound, upperBound, false, true);
var request = index.openCursor(range);
request.onsuccess = function(event) {
var cursor = event.target.result;
if(cursor) {
results.push(cursor.value);
cursor.continue();
}
};
}
然后调用它:
var request = indexedDB.open(...);
request.onsuccess = function(event) {
var db = event.target.result;
query(db, 1, function(results) {
// ...
});
};
这里比较的是不同长度的数组键路径,所以即使索引定义在[a,b]上,范围也只比较[a]...[a+1]。比较不同长度的数组键路径是完全可以的,比较只比较最短数组长度的值。
索引中条目的顺序按照 [a,b]。因此,如果您将索引条目限制为包含 a 的条目,那么您将获得一个按 b 排序的子集,其中每个 a 都是目标 a,这就是我所理解的 objective.