Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Updating an Existing IndexedDB objectStore

, 24 Feb 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
How to update an existing IndexedDB objectStore.

Introduction

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Gil Fink
Technical Lead sparXys
Israel Israel
Gil Fink is a web development expert and ASP.Net/IIS Microsoft MVP. He is the founder and owner of sparXys. He is currently consulting for various enterprises and companies, where he helps to develop Web and RIA-based solutions. He conducts lectures and workshops for individuals and enterprises who want to specialize in infrastructure and web development. He is also co-author of several Microsoft Official Courses (MOCs) and training kits, co-author of "Pro Single Page Application Development" book (Apress) and the founder of Front-End.IL Meetup. You can read his publications at his website: http://www.gilfink.net
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411022.1 | Last Updated 24 Feb 2012
Article Copyright 2012 by Gil Fink
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid