12

I have seen multiple JavaScript examples of using createIndex to define an ObjectStore index directly after the ObjectStore has been created like this:

var objectStore = ixDb.createObjectStore(osName, { keyPath: pkName, autoIncrement: autoIncrement });

objectStore.createIndex("name", "name", { unique: false }); 

Can anyone show me how you would use createIndex on a pre-existing table without calling createObjectStore? I guess the real question here is how to get a reference to the objectStore without using createObjectStore?

I tried several variations of the following with no luck:

var objectStore = window.IDBTransaction.objectStore(ObjectStoreName);
var index = objectStore.createIndex(ixName, fieldName, { unique: unique, multiEntry: multiEntry });
SetFreeByTruth
  • 819
  • 8
  • 23
Jake Drew
  • 2,230
  • 23
  • 29
  • 3
    request.onupgradeneeded = function (evt) { var objectStore = evt.currentTarget.transaction.objectStore("people"); objectStore.createIndex("city", "city", { unique: false }); };//Quick answer – Garry Apr 01 '16 at 00:03

2 Answers2

4

You do it during onupgradeneeded, which should be the same place you are making the object store in the first place. In there, you can do whatever appropriate action is needed to upgrade it, such as creating a new index. Here is an example.

dumbmatter
  • 9,351
  • 7
  • 41
  • 80
  • onupgradeneeded does not even fire in Chrome yet. It is part of the newest W3 spec. It would be nice if I could figure out how to get at an objectstore reference using something other than events? – Jake Drew Jul 18 '12 at 19:48
  • And none of it works at all in IE or Opera or Safari, so using IndexedDB at all implies that you're sacrificing some cross-browser compatibility. Chrome will hopefully soon support `onupgradeneeded`. I think I read that they're targeting Chrome 22 for this, so maybe this will all be working in Chrome in a few months. Until then, you can hack around it by doing something like [this](http://blog.nparashuram.com/2012/05/indexeddb-setversion-vs-onupgradeneeded.html). – dumbmatter Jul 18 '12 at 19:54
  • And since I wasn't explicit enough earlier, I suppose... you can only mess with your object store when you're upgrading the database. There is no other solution. [`onupgradeneeded` "is very important, because it is the only place in your code that you can create object stores and indices."](https://developer.mozilla.org/en/IndexedDB/Using_IndexedDB) – dumbmatter Jul 18 '12 at 19:56
  • I will def give you credit since your link helped me figure out a workaround for both the old and new spec. I will also post my workaround below. – Jake Drew Jul 18 '12 at 20:23
  • It seems like this code is not working anymore, because transaction property inside IDBOpenDBRequest is undefined. – Dmitrii Sorin Oct 21 '14 at 05:59
3

Chrome does not currently support the onupgradeneeded event. If you wanted to get a reference to an ObjectStore using the old or new W3 specs, this is one possible workaround via the onsuccess event using either the old setVersion command or via the new onupgradeneeded event:

var ixDb; 
var ixDbRequest; 
var ixDbVersionTansaction;

//Check to see if we have a browser that supports IndexedDB
if (window.indexedDB) {

ixDbRequest = window.indexedDB.open(dbName, dbVersion);

//For browsers like chrome that support the old set version method
ixDbRequest.onsuccess = function (e) {

    ixDb = ixDbRequest.result || e.result;

    if (typeof ixDb.setVersion === "function") {

        //Put your version checking logic here 

        if (oldVersion < newVersion) {
            var verRequest = ixDb.setVersion(newVersion);

            verRequest.onerror = function (e) {
                //handling error logic here
            }

            verRequest.onsuccess = function (e) {
                //Get a reference to the version transaction 
                //from the old setVersion method.
                ixDbVersionTansaction = verRequest.result;
                //Create database using function provided by the user. 
                UserFunction();
            }
        }
    }
}; 

ixDbRequest.onupgradeneeded = function (e) {
    //FF uses this event to fire the transaction for upgrades.  
    //All browsers will eventually use this method. Per - W3C Working Draft 24 May 2012
    ixDb = ixDbRequest.result || e.currentTarget.result;

    //Get a reference to the version transaction via the onupgradeneeded event (e)
    ixDbVersionTansaction = e.currentTarget.transaction;

    //Create database using function provided by the user. 
    UserFunction();
};

UserFunction(){
    //ObjectStore is accessed via ixDbVersionTansaction variable 
    // in either instance (transaction..objectStore("ObjectStoreName"))
    var ObjectStore = ixDbVersionTansaction.objectStore("ObjectStoreName");
    var index = ObjectStore.createIndex("ixName", "fieldName");
}
Jake Drew
  • 2,230
  • 23
  • 29