Getting All Stored Items in an IndexedDB objectStore

April 18, 2012

no comments

Getting All Stored Items in an IndexedDB objectStore

Getting All Stored Items in an IndexedDB objectStoreOne of the things that I found very common to do when you use IndexedDB is retrieving all the stored items in an objectStore. This post will show you how to implement such a function.

Getting All Stored Items in an IndexedDB objectStore

When dealing with databases there are a lot of times that you find yourself retrieving all the stored items in a database table. When using IndexedDB, which is an index database, the retrieve all items operation is also vital.

Here is an example of how to retrieve all the stored items in an objectStore:

function getAllItems(callback) {
    var trans = db.transaction(storeName, IDBTransaction.READ_ONLY);
    var store = trans.objectStore(storeName);
    var items = [];
 
    trans.oncomplete = function(evt) {  
        callback(items);
    };
 
    var cursorRequest = store.openCursor();
 
    cursorRequest.onerror = function(error) {
        console.log(error);
    };
 
    cursorRequest.onsuccess = function(evt) {                    
        var cursor = evt.target.result;
        if (cursor) {
            items.push(cursor.value);
            cursor.continue();
        }
    };
}

At first, you will create the transaction and store objects. Pay attention that the storeName is a variable that holds a string with the objectStore name and the db variable is the opened IndexedDB database. Later on, a JavaScript array with the name items is created. This array will hold all the items in the objectStore and will be used by the callback function that is passed to the getAllItems function. After the array’s creation, the transaction’s oncomplete event handler is wired and its job is to call the callback function with the array of items. In order to retrieve all the items, a cursor is opened against the objectStore and in its success handler all the items are inserted into the items array.

In order to use the getAllItems function just call it with the relevant callback function. Here is an example for calling the function:

getAllItems(function (items) {
    var len = items.length;
    for (var i = 0; i < len; i += 1) {
        console.log(items[i]);
    }
});

In the example, after the items retrieval you iterate them and output them to the console’s log.  

Other Retrieval Options

There are other options to retrieve all the stored items in an objectStore. In FireFox you can use the getAll function which is not a part of the HTML5 IndexedDB specifications (and therefore I suggest not to use it at all):

var request = store.getAll();
request.onsuccess = function(evt) {  
  // Extract all the objects from the event.target.result
};  

Summary

This post showed you how to get all the stored items in an objectStore. This operation is very common and therefore I hope it will be added to the specifications as an API function.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>