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 });
1
  • 3
    request.onupgradeneeded = function (evt) { var objectStore = evt.currentTarget.transaction.objectStore("people"); objectStore.createIndex("city", "city", { unique: false }); };//Quick answer Commented Apr 1, 2016 at 0:03

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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?
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.
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."
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.
It seems like this code is not working anymore, because transaction property inside IDBOpenDBRequest is undefined.
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");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.