How to obtain results of an indexedDB cursor in reverse order

Viewed 2093

I've written the below code to iterate the rows of an object stored in an indexedDB. I'm using Google Chrome browser.

'use strict';
    var openRequest = indexedDB.open('Library', 1);
    var db;

    openRequest.onupgradeneeded = function(response)
    {
        console.debug(1);
        response.currentTarget.result.createObjectStore("authors",{ keypath: 'id', autoIncrement: true });
    }  

    openRequest.onsuccess = function(response) {
        console.debug('success opening indexeddb');
        db = openRequest.result;
       findAuthors();
    };

    function findAuthors() {
        var trans = db.transaction('authors', 'readonly');
        var authors = trans.objectStore("authors");
        var request = authors.openCursor();
        request.PREV = true;
        request.onsuccess = function(response) {
        var cursor = response.target.result;
        if (!cursor) {
        alert('No records found.');
        return;
        }
        alert('Id: ' + cursor.key + ' Last name: ' + cursor.value.lastName);
        cursor.continue();
        };
        request.onerror = function(response) { // display error
        };
    }

Records in my DB are as below:

enter image description here

Currently the iteration happens in order of keys 2,3 and then 4. What I want is that when I start iterating the cursor I get the rows with key 4, 3, and then 2 i.e. in reverse order. I tried using a PREV boolean attribute on the cursor object but it doesn't seem to be working:

request.PREV = true;
1 Answers
Related