13

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

Deni Spasovski
  • 4,004
  • 3
  • 35
  • 52
user1598696
  • 550
  • 1
  • 4
  • 22

2 Answers2

24

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();
        }
    }
}
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
Deni Spasovski
  • 4,004
  • 3
  • 35
  • 52
  • 3
    Thanks for reply. This could work. But it seems "stupid" to me and not programmatic way to open database twice :/ – user1598696 Nov 21 '13 at 08:51
  • Maybe there is really no other way to do it...I'll wait a day and see. IndexedDB seems still very unfinished and some things are done pretty bad. – user1598696 Nov 21 '13 at 08:58
  • I don't understand why you want to know the version of the db upfront? you know in wich version you need to open the database. if the version doesn't match the version on the you provided it wil give you a way to upgrade your database. In that event you will know from which version you need to upgrade. – Kristof Degrave Nov 21 '13 at 13:16
  • 2
    Knowing the version upfront doesn't work every time, especially if the database can update often (new indexes, tables), but reading the database version on the first opening and storing it in cache could work whenever upgrade is needed, that way you can avoid the extra db open for reading the version. – Deni Spasovski Nov 21 '13 at 18:24
  • I know scheme's can change, but as developer of the application you choose which indexes and tables you want, not the user that is using the application. Another solution is like you shown, but with this in mind that depending on the client the indexeddb will deffer – Kristof Degrave Nov 22 '13 at 10:49
5

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.

Yusuf Adeyemo
  • 389
  • 3
  • 12
Kristof Degrave
  • 4,142
  • 22
  • 32
  • Thanks for your response.It is all clear to me what you wrote, but where will I store that constant? If I close browser and open it again then that constant is lost, Am I'm right?!? And then, when I want to create new object how will I know which version of database I have? Thanks. – user1598696 Nov 20 '13 at 18:48
  • 1
    No if you just define var constant=3; it will stay the same everytime you open the file. When you need a newer version you just set it to 4 for example – Kristof Degrave Nov 21 '13 at 09:39
  • 1
    I have small app, and every time i upload a file new objectStore needs to be created. I'm a bit confused now, can you please explain further or give small example. If i have constant var version = 2; and then I have automatic upload of 100+ files. So that means 100+ objectStores needs to be created. What is then a proper way to do it? – user1598696 Nov 21 '13 at 16:24
  • if you have var const = 3; lets say X files are uploaded 1 by 1, and lets say number version is now X. Close browser, open browser again, You have const =3; and how will You now database version? It seems to me that Deni Mf is right, Caching is needed. But that is not permanent storage either. – user1598696 Nov 21 '13 at 20:12
  • Why would tou need a new object store for a file? you can stor multiple files in only 1 object store. For adding data you don't need to change the scheme – Kristof Degrave Nov 22 '13 at 10:47
  • Because I'm building a torrent client. Each file name is object store and file is split in blobs and stored inside object store. If you think there is some better way to do it please inform me. Thanks. – user1598696 Nov 22 '13 at 18:16
  • 2
    Ok, now I follow. I think Deni's solution is the way To Go. Opening the db bydefault without a Version and reopen It with An incremented Version for ever file – Kristof Degrave Nov 23 '13 at 05:54