How to obtain x objects with highest timestamp value?

179 views Asked by At

I'm new to web app development and I couldn't find a solution to the following problem. Basically I'm trying to get sorted the array of newest objects from IndexedDB database. Each object contains a timestamp value. I'v created an index on timestamp and I'm able to get the object with the highest value.

    function getMails (numberOfMessages, _function) {
/*  This function uses asynchronous methods, hence you have to pass the key and function that will receive
    the messageitem as a parametr */

    if (typeof optionalArg === 'undefined') {
     optionalArg = 10;
    }
    function _getMails (db) {
        var transaction = db.transaction(["MessageItem"], "readonly");
        var store = transaction.objectStore("MessageItem");

        var index = store.index("timestamp");
        var cursor = index.openCursor(null, 'prev');
        var maxTimestampObject = null;

        cursor.onsuccess = function(e) {
            if (e.target.result) {
                maxTimestampObject = e.target.result.value;
                _function(maxTimestampObject);
            }
        };

    }
    openDB(_getMails);
}

Function openDB opens the database and passes the db object to the _getMails function as a parametr. Function getMails currently passes only the object with a highest timestamp value. I could iterate over the databse x(numberOfMessages) times and allways select the object with a highest timestamp while excluding the objects that are already in the array I'm trying to get. But I'm not sure if it's the most convenient way to do so. Thank You for your responses. Jan

2

There are 2 answers

0
Kyaw Tun On BEST ANSWER

You just need to call cursor.continue() in onsuccess function. It will be called again with next cursor result.

0
Jan Beneš On

Thank you Kyaw Tun. Here is my final code for those who are interested:

function openDB(_function) {
// Opening the DB
var openRequest = indexedDB.open("TsunamiDB",1);
openRequest.onupgradeneeded = function(e) {
    console.log("Upgrading...");
    var thisDB = e.target.result;

    if (!thisDB.objectStoreNames.contains("MessageItem")) {
        var objectStore = thisDB.createObjectStore("MessageItem");
        objectStore.createIndex("timestamp", "envelope.timestamp", {unique:false});
    }
}

openRequest.onsuccess = function(e) {
    console.log("Success!");
    _function(e.target.result);
}

openRequest.onerror = function(e) {
    console.log("Error");
    console.dir(e);
}}

This function uses asynchronous methods, hence you have to pass the key and function that will receive the messageitem as a parametr. Pareametr is an array of x(numberOfMessages) newest messages. The array is sorted so that the message with index 0 is the newest one.

function getMails ( _function, numberOfMessages) {

if (typeof numberOfMessages === 'undefined') {
 numberOfMessages = 10;
}
function _getMails (db) {
    var transaction = db.transaction(["MessageItem"], "readonly");
    var store = transaction.objectStore("MessageItem");

    var index = store.index("timestamp");
    var objectsArray = [];
    var i = 0;

    index.openCursor(null, 'prev').onsuccess = function(e) {
        var cursor = e.target.result;
        if (cursor && i < numberOfMessages) {
            objectsArray.push(cursor.value)
            ++i;
            cursor.continue();
        }
    };
    transaction.oncomplete = function(e) {
        _function(objectsArray);
    }

}
openDB(_getMails);}