First thing you need to do is create index on the revision
field.
Then you need a search function which will use that index and open the index with inverse order of the objects. Then the first object will be the object you are looking for.
var index = objectStore.index('revision');
index.openCursor(null, 'prev');
The null states that you are searching for all values not a specific one, and the second parameter is the direction of the search.
Here is the sample code:
function getMaxNumber (callback) {
var openReq = indexedDB.open(baseName);
openReq.onsuccess = function() {
var db = openReq.result;
var transaction = db.transaction(objectStoreName, 'readonly');
var objectStore = transaction.objectStore(objectStoreName);
var index = objectStore.index('revision');
var openCursorRequest = index.openCursor(null, 'prev');
var maxRevisionObject = null;
openCursorRequest.onsuccess = function (event) {
if (event.target.result) {
maxRevisionObject = event.target.result.value; //the object with max revision
}
};
transaction.oncomplete = function (event) {
db.close();
if(callback) //you'll need a calback function to return to your code
callback(maxRevisionObject);
};
}
}
Since the IndexedDB
api is async you would need a callback function to return the value to your code.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…