Over a million developers have joined DZone.

Getting All Stored Items in an IndexedDB objectStore

DZone 's Guide to

Getting All Stored Items in an IndexedDB objectStore

· Web Dev Zone ·
Free Resource

One 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) {  
    var cursorRequest = store.openCursor();
    cursorRequest.onerror = function(error) {
    cursorRequest.onsuccess = function(evt) {                    
        var cursor = evt.target.result;
        if (cursor) {

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) {

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


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.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}