2

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.

Community
  • 1
  • 1
Daniel Gerson
  • 2,159
  • 1
  • 19
  • 29

1 Answers1

3

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

Kristof Degrave
  • 4,142
  • 22
  • 32
  • Thanx, I'm going to check it out before I mark as correct. I come from a strongly typed background, so navigating documentation for API I find a real pain (compared to auto-completion). – Daniel Gerson Sep 21 '12 at 13:29
  • I know the feeling :). In RL I work as a .NET developer, so I know the pain. But JS also has its advantages, it's very extensible for instance. If you need more info about the API, I have written some blogpost about the API on http://www.kristofdegrave.be – Kristof Degrave Sep 21 '12 at 13:52
  • Extensibility is a cop-out for not negotiating adequate interfaces and hooks for your clients and vendors, and going around them instead :-P Marking your answer as correct, and will be visiting your site. Already having problems loading data from multiple object stores in an async way for something that needs to happen procedurally :-( – Daniel Gerson Sep 21 '12 at 14:25
  • did you try working with the yield keyword? An other way to handle the async calls easily is by using promises – Kristof Degrave Sep 21 '12 at 15:54
  • Thanx for the advice. Clearly I'm not that familiar with these strategies... Couldn't yield leave transactions open, leaking them? I guess when the generator is GC'd then everything else would be. Although I don't quite see how you connect your onsuccess from your cursor request to your generator. Also seems like Promises just shift the boilerplate to promise object, so doesn't that really only add value if you use the same promise again and again? I guess all this really deserves another Stack question... – Daniel Gerson Sep 27 '12 at 14:24