Updating an Existing IndexedDB objectStore

February 23, 2012

5 comments

Updating an Existing IndexedDB objectStore

A few weeks ago I have published an article in the CodeProject website called “Getting Started with IndexedDB”. One of the questions that I got was how to update an existing objectStore with a new index. This post will answer the question.

The updateneeded Event

In IndexedDB the only way to update the database is through the updateneeded event. That event is raised when you use the IndexedDB open function with a database version number which is greater than the current database version number. For example, lets assume that there is an IndexedDB database with the name PeopleDB which has a version number of 1. The following code will raise the updateneeded event:

var request = indexedDB.open("PeopleDB", 2); 

The open function returns IDBRequest object (since all the functionality in IndexedDB is asynchronous by default) which you will use to wire handlers for onsuccess, onerror and onupgradeneeded. You will wire the events like in the following example:

var request = indexedDB.open("PeopleDB", 2);                  
request.onsuccess = function (evt) {
    db = request.result;                                                            
};
 
request.onerror = function (evt) {
    console.log("IndexedDB error: " + evt.target.errorCode);
};
 
request.onupgradeneeded = function (evt) {                                           
  // Change the Database structure
};

The example shows how to wire the event handlers for the success, error and upgradeneeded events. The db is a variable that will hold the database after it is opened.

Creating a New Index

In the event handler for the updateneeded event you can create a new objectStore index. Here is an example of how to do that:

var request = indexedDB.open("PeopleDB", 2);                  
request.onsuccess = function (evt) {
    db = request.result;                                                            
};
 
request.onerror = function (evt) {
    console.log("IndexedDB error: " + evt.target.errorCode);
};
 
request.onupgradeneeded = function (evt) {                                           
    var objectStore = evt.currentTarget.transaction.objectStore("people");
    objectStore.createIndex("city", "city", { unique: false });                        
 
    // Store a new value in the objectStore. 
    var req = objectStore.add({ name: "New person", email: "newperson@company.com", city: "Palms street" });
    req.onsuccess = function (event) {
        // Add succeeded                          
    };
;

In the example you use the event parameter that is passed to the updateneeded handler to get the opened update transaction. Through the transaction you can get the relevant objectStore which will be manipulated. On the objectStore you use the createIndex function to create a new index. The rest of the code is inserting a new person into the objectStore.

Summary

Updating an IndexedDB existing objectStore is done using the updateneeded which is raised when you change the database version.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

*

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>

5 comments

  1. arpanaJanuary 10, 2014 ב 15:19

    Can you please help me to create index on array element.

    Reply
    1. Gil Fink
      Gil FinkJanuary 11, 2014 ב 8:35

      What do you mean index on array element? Arrays include indexes as part of their interface.

      Reply
      1. arpanaJanuary 13, 2014 ב 14:22

        var data = {
        teacher : {
        name : “Mack”,
        subject : “Maths”
        },
        Student : [{
        studname : "Alex",
        stud_id : 1
        },{
        stud_name : "Jack",
        stud_id : 2
        },{
        stud_name : "Mark",
        stud_id : 3
        }]
        };

        In object above data object has Student array, which has multiple objects. And I want to create index on array of object (data.Student.stud_name). Is it possible to create such type of index?

        Reply
        1. Gil Fink
          Gil FinkJanuary 14, 2014 ב 15:22

          In IndexedDB, you will create an object storage in the database to store teachers or students.
          Once you do that you can create indexes on the object storage to retrieve your data.
          Don’t save an array inside an object storage.

          Reply
          1. arpanaJanuary 15, 2014 ב 9:57

            Thank you Gil Fink.