Google Gears Database API on the Server
As soon as I started to play with Aptana Jaxer, I saw an interesting opportunity to port the Google Gears Database API (note the Gears in the logo!)
If I could use the same API for both client and server side database access, then I can be enabled to do things like:
- Use one API, and have the system do a sync from local to remote databases
- If the user has JavaScript, use a local database, else do the work remotely
- Share higher level database libraries and ORMs such as Gears DBLib for use on server side data too
I quickly built a prototype to see if this would all work.
The Jaxer shim of the Gears API was born, and to test it out I took the database example from Gears itself and made it work.
To do so, I only had to make a few changes:
Run code on the server
I changed the main library to run on the server via:
<script type="text/javascript" src="gears_init.js" runat="server"></script>
I wrapped database access in proxy objects, such as:
function addPhrase(phrase, currTime) { getDB().execute('insert into Demo values (?, ?)', [phrase, currTime]); } addPhrase.proxy = true;
This now allows me to run the addPhrase code from the browser, and it will be proxied up to the server to actually execute that INSERT statement.
This forced me to separate the server side code from the client side code, which is a better practice anyway, but it does make you think about what goes where. In a pure Gears solution I can put everything in one place since it all runs on the client.
Create the new gears_init.js
A new gears_init.js acts as the shim itself. Instead of doing the typical Gears logic, it implements the Gears database contract. This wasn’t that tough, although there are differences between the Gears way, and the Jaxer.DB way. The main difference is to do with the ResultSet implementation, where Gears goes for a rs.next()/rs.field(1) type model versus the Jaxer.DB rs.rows[x] model.
I actually much prefer Gears DBLib as it hides all of that, and just gives the programmer what he wants… the rows to work on.
oncallback magic
In the current Jaxer beta, I ran into an issue where I wanted the Gears library to just “be there” for any proxy requests.
You have to think about the lifecycle of a Jaxer application, and the documentation tells you what you need to know to work around the issue.
In this case, I wrapped the code in:
function oncallback() { // create the wrapper here }
This is less than idea, and Aptana is playing with nice scoping which would enable you to just say “hey, load this library once and keep it around for the lifetime of the server | application | session | page”. That will be very nice indeed.
You can do a little bit of this by opening up your jaxer_prefs file and adding the resource for your file:
// This option sets up an html document that will be loaded // everytime a callback is processed. This has to be a local file. // If not specified, an empty document will be loaded. // pref("Jaxer.dev.LoadDocForCallback", "resource:///framework/callback.html");
Future…
This is just the beginning. As I mentioned at the beginning, I am interested to see where you can take this to handle clients who do not support JavaScript, and also to deal with synchronization with minimal code (sync from local to remote with exactly the same SQL API).
February 5th, 2008 at 8:37 am
This is just so cool! I wondered how long it would take to set up Gears on the server side of Jaxer and then you did it :)
Kind of gets you thinking what else could fit on the server side..
Cheers,
PS
September 11th, 2008 at 11:31 am
Pretty cool stuff..