DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • Mastering Time Series Analysis: Techniques, Models, and Strategies
  • Top 10 Pillars of Zero Trust Networks
  • Front-End: Cache Strategies You Should Know
  • File Upload Security and Malware Protection

Getting All Stored Items in an IndexedDB objectStore

Gil Fink user avatar by
Gil Fink
·
Apr. 18, 12 · Interview
Like (0)
Save
Tweet
Share
18.45K Views

Join the DZone community and get the full member experience.

Join For Free

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

ObjectStore

Opinions expressed by DZone contributors are their own.

Trending

  • Mastering Time Series Analysis: Techniques, Models, and Strategies
  • Top 10 Pillars of Zero Trust Networks
  • Front-End: Cache Strategies You Should Know
  • File Upload Security and Malware Protection

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: