Monday, August 25, 2008

OR-Mapper for the HTML5 database and the Google Gears database

Last week a guy on the Joose mailing list asked whether the example object relational mapper included with Joose could be used to build iPhone web application using the webkit implementation of the HTML5 database API. The immediate answer was that it wasn't possible because the OR mapper was designed to work with the Google Gears database which is currently not compantible with HTML5.

So I did two thinks

I implemented a wrapper for the Gears API that makes it look like the HTML5 database API (There was already one linked from Ajaxian but all the download links were dead)

I ported the existing OR mapper to use the new API

This means that the OR mapper can be used with the Gears db and the HTML5 db without any changes.

The HTML5 database API in inherently asynchronous. The reason being that databases requests should not block script execution and thus decrease the responsiveness of the GUI.I, personally, don't like the API because thinks start to look a lot like degenerated LISP once you get down to the 16th callback level, but, I guess, performance was more important than beauty for the API designers.

The primary change with respect to the synchronous Gears version is that now all operations that talk to the database like Entity.save() or Entity.newFromId() take an extra parameter that is a callback function which gets executed as soon as the db operation is completed.

Here are some examples from the test suite of the OR-Mapper.

Declaring an entity looks like this (A Person that has a mother and many cars):