简体   繁体   中英

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

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();
        }
    }
}

The only way you can create an object store is in the onupgradeneeded event. You need a version_change transaction to be able to change the schema. And the only way of getting a version_change transaction is through a onupgradeneeded event.

The only way to trigger the onupgradeneeded event is by opening the database in a higher version than the current version of the database. The best way to do this is keeping a constant with the current version of the database you need to work with. Every time you need to change the schema of the database you increase this number. Then in the onupgradeneeded event, you can retrieve the current version of the database. With this, you can decide which upgrade path you need to follow to get to the latest database schema.

I hope this answers your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM