Question

How to upgrade the IndexedDB version in a program... I have a button, If I click, the IDB version should be automatically upgraded to next version and onupgradeneeded event should be called and also if I open it the next time, it has to open with the newly upgraded version. How can I do like this??

Was it helpful?

Solution

Open the database with a higher version number.

This wil trigger the onupgradeneeded event. Once this is handle the onsuccess will return ths IDB connection in the latest version.

var dbrequest = indexedDB.open("name", version);

dbrequest.onupgradeneeded = function (){
   // Upgrade db code
}

dbrequest.onsuccess = function(){
   // db opened in the provided version.
}

If you just want to open a connection to the latest version, you can call the open method without providing a version.

var dbrequest = indexedDB.open("name");

dbrequest.onsuccess = function(){
   // db opened in the latest version.
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top