Daniel Gerson
Daniel Gerson

Reputation: 2209

How do I get hold of both the old and new versions of the IndexedDB database, while catering for both setVersion and onUpgradeNeeded?

There are already questions and answers like this

Add Index to Pre-Existing ObjectStore In IndexedDB Using Javascript

but I can't see how that code caters for incremental versions for BOTH the setVersion and onUpgradeNeeded methods. Something like the following pseudoCode..


if (oldVersion < 1)
createObjectStore
if (oldVersion < 2)
createNewIndex
etc etc etc...

I.e. I know how to get the oldVersion for the setVersion method (Check if db.serVersion exists and then query the value of db.version), but I don't know how to get the old version for the newer onUpgradeNeeded method.

It wasn't obvious from http://dvcs.w3.org/hg/IndexedDB/raw-file/tip/Overview.html#request-api either :-(

THanx.

Upvotes: 2

Views: 1340

Answers (1)

Kristof Degrave
Kristof Degrave

Reputation: 4180

Well there are several ways. First of all the new version of the database, is the version number you provide when opening the db.

var version = 2;
var request = indexeddb.open("name", version)

so if you use a variable, you can do that. But the onupgradeneeded event also provides eventdata

request.onupgradeneeded = function (e) {
     var transaction = request.result;
     var oldVersion = e.oldVersion;
     var newVersion = e.newVersion;
};

As you see the eventdata is passed as an argument to the onupgradeneeded callback

Upvotes: 3

Related Questions