Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Top 1 row from IndexedDB

Tags:

indexeddb

Is there a way to get just top one row in IndexedDB? I could not find anything for it.

like image 557
strider Avatar asked Jan 06 '12 23:01

strider


2 Answers

Using a cursor and not calling .continue() isn't a hack at all. It's the intended way. On of the main points of cursors is exactly that they let you only fetch the information that you need.

So no need to call transaction.abort()

like image 67
Jonas Sicking Avatar answered Sep 23 '22 02:09

Jonas Sicking


Sort of a hack, but I believe that if you don't call continue() on the result object it will only process the first item.

Below is a quick example of querying with a cursor and only processing one result.

var db = _IndexedDatabase;
var trans = db.transaction(["Sites"], IDBTransaction.READ_ONLY);
var store = trans.objectStore("Sites");
var request = store.openCursor();
request.onsuccess = function (e)
{
    var result = e.target.result;
    if(!!result == false) { return; }
    // Use result.value some how

    // Comment out this line to process the first item only
    //result.continue();
};
like image 36
Shawn Avatar answered Sep 21 '22 02:09

Shawn