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??
1 Answer
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.
}
1 Comment
buley
Right on. In your database is already open, as it sounds like it might be, for the
version variable, @Manikandan, you can pass indexedDB.open the current IDBDatabase.version property and + 1 and always trigger an upgradeneeded event.