Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Index to Pre-Existing ObjectStore In IndexedDB Using Javascript

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 });
like image 403
Jake Drew Avatar asked Jul 18 '12 01:07

Jake Drew


People also ask

Can I store object in IndexedDB?

It lets you store just about anything in the user's browser. In addition to the usual search, get, and put actions, IndexedDB also supports transactions. Here is the definition of IndexedDB on MDN: "IndexedDB is a low-level API for client-side storage of significant amounts of structured data, including files/blobs.

Is IndexedDB domain specific?

IndexedDB follows a same-origin policy. So while you can access stored data within a domain, you cannot access data across different domains.


2 Answers

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.

like image 110
dumbmatter Avatar answered Oct 11 '22 10:10

dumbmatter


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");
}
like image 30
Jake Drew Avatar answered Oct 11 '22 10:10

Jake Drew