elarson’s posterous

 
« Back to blog

Javascript DB Revelation

I finally realized why having a database for client side Javascript is such a great idea. It took a while for me see a use case that really exposed why it is such a helpful concept. I always thought to myself that if you have an application pushing enough data to the client where you need to do SQL queries, then something just isn't designed correctly. I'm all for pushing work to the client when it helps speed up the response, but pushing large batches of data to the client can only hurt. It has never made sense then why toolkits like Gears provide a database feature.

Like so much in software, there is always a consideration of state. What does the world look like at a particular point in time. One of the more difficult aspects of Javascript is that the state can be difficult to determine because there are times where taking a look at some value might be misleading. A great example is if you check the status of an input. That input might change by some other event. The easy answer is to avoid randomly attaching events. The problem is things can get complicated when events start bubbling and they can be fired multiple times. I'm sure smarter folks than me have developed excellent skills working with the Javascript event models (and I hope they all contribute to jQuery), but for me, it is still very much a challenge to keep straight why I might see an event a few times when it seems like once would be enough (with bubbling considered).

Having a client side database takes much of this confusion out of the picture because you have a place to keep your current state that can be locked. Sessions are a great example where multiple requests can be considered in their own scope even though different threads may handle them. One common means for storing sessions is via a database. The general idea is that you have a single canonical resource for making a decision. Even though the browser might be in the middle of making changes, you have a means of saving and retriveing critical details about the state of the applicaiton without trusting the asynchronous nature of Javascript events.

I might still be misunderstanding the thinking behind a Javascript database, but assuming it gives me a place outside the constant changing of the browser DOM for keeping important state information, then I think it makes a lot more sense.

Loading mentions Retweet