Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create multiple object stores in IndexedDB

I don't know if I'm right or wrong. But as I know I can't create a version change transaction manually. The only way to invoke this is by changing the version number when opening the indexed DB connection. If this is correct, in example1 and example2 new objectStore will never be created?

Example1

function createObjectStore(name){
    var request2 = indexedDB.open("existingDB");    
    request2.onupgradeneeded = function() {
        var db = request2.result;   
        var store = db.createObjectStore(name); 
    };
}

Example2

function createObjectStore(name){
    var request2 = indexedDB.open("existingDB");    
    request2.onsuccess = function() {
        var db = request2.result;   
        var store = db.createObjectStore(name); 
    };
 }

Example3 - This should work:

function createObjectStore(name){
    var request2 = indexedDB.open("existingDB", 2);     
    request2.onupgradeneeded = function() {
        var db = request2.result;   
        var store = db.createObjectStore(name); 
    };
}

If I want to create multiple objectStore's in one database how can I get/fetch database version before opening the database?? So is there a way to automate this process of getting database version number??

Is there any other way to create objectStore other than that using onupgradeneeded event handler.

Please help. Thanks a lot.

Edit:

Here is same problem that I have: https://groups.google.com/a/chromium.org/forum/#!topic/chromium-html5/0rfvwVdSlAs

like image 487
user1598696 Avatar asked Nov 20 '13 13:11

user1598696


People also ask

Can IndexedDB store objects?

Structuring the database. Now to structure the database. IndexedDB uses object stores rather than tables, and a single database can contain any number of object stores. Whenever a value is stored in an object store, it is associated with a key.

How can you insert a new record collection of data in IndexedDB?

add() adds a new record to the store. IDBCursor. update() updates the record at the current position of the cursor.

Can we store images in IndexedDB?

Not everything can be stored in IndexedDB on all platforms # If you are storing large, user-generated files such as images or videos, then you may try to store them as File or Blob objects. This will work on some platforms but fail on others. Safari on iOS, in particular, cannot store Blob s in IndexedDB.

What is keyPath in IndexedDB?

The keyPath read-only property of the IDBObjectStore interface returns the key path of this object store. If this property is null, the application must provide a key for each modification operation.


1 Answers

You need to open the database to check it's current version and open it again with version + 1 to trigger the upgrade.

Here is the sample code:

function CreateObjectStore(dbName, storeName) {
    var request = indexedDB.open(dbName);
    request.onsuccess = function (e){
        var database = e.target.result;
        var version =  parseInt(database.version);
        database.close();
        var secondRequest = indexedDB.open(dbName, version+1);
        secondRequest.onupgradeneeded = function (e) {
            var database = e.target.result;
            var objectStore = database.createObjectStore(storeName, {
                keyPath: 'id'
            });
        };
        secondRequest.onsuccess = function (e) {
            e.target.result.close();
        }
    }
}
like image 118
Deni Spasovski Avatar answered Sep 21 '22 09:09

Deni Spasovski