0

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

1 Answer 1

3

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.
}
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.