Thursday, September 30, 2010

A Document Database for your Browser - proton-db

Excited about the new Web Storage browser standard (the alternative to cookies) but not excited about the low-level, string-only implementation? The new open source proton-db framework gives you a high-level document database right in your browser. Killer idea, IMO, and nicely implemented.

How to use it:

var things = proton.db('things');
things.save({foo:'foo', foot:11, bar:[1, 2, 3]});
var found = things.find({foo:'foo'});
var found_again = things.find({foot:11});


As you can see, it is very much inspired by mongodb's intuitive interface. Check out sample-queries.txt for more usage examples.

How to get up and running:

Currently it is necessary to check the project out from the proton-db project page on google code and run an ant task (ant build) to concatenate all the js files together into a "proton.js" file. But, it's only one step. Then you can include it in your page and start using it. No dependencies required

The framework's author, Guy Royse, hinted he might be moving the project to github, and also might consider adding a feature that will make proton-db fall back to cookie storage for older browsers.

Implementation:

Under the hood, the code iterates through each record to check for matches, rather than maintaining indexes like mongodb and other document databases. However, since you'll likely be only storing on the order of hundreds of records max per user for most applications, it's probably fast enough for you. I did a test that inserted 400 records and read one of them. It took several seconds to insert all of them, but once they were there, the .find() call was almost instantaneous.

Web Storage

Proton-db uses the new Web Storage standard to actually store the data, so that it is persisted between sessions. For some simple applications, you may choose to read and write to Web Storage yourself. See the html source of web_storage_example.html to see how it works (it essentially just exposes an associative array named localStorage that is persisted across windows and sessions for a given user).

Review

Click this link to review the main points of this blog post, and help commit them to memory:



Also check out memorize.com/web-storage for a review of the basics of how to access Web Storage directly.

No comments:

Post a Comment