Thursday, July 5, 2007

Client-side SQL database in Javascript


I was thinking today about how a person might go about creating a rich offline web application for devices like the iPhone, where your application may need to run entirely from cache from time to time.

In the extreme example, you could imagine an application that stored its data entirely on the client side, and used cached html and javascript files to manipulate that data. Assuming you could get around the storage issue -- either via cookies, a Flash shared object (not available for the iPhone), Firefox or IEs storage objects, or some trickery with browser form autocompletion -- you'd still run into the task of manipulating that data.

Web developers are used to using an SQL database for the retrieval, joining and sorting of data, so it's convenient that similar functionality exists within Javascript, thanks to a small Javascript library called TrimQuery. After defining a simple table schema, you can use TrimQuery to issue a subset of the standard SELECT syntax. This will let you do something like the following, all within Javascript:

selectStatement = queryLang.parseSQL( "SELECT table1.columna, table2.columnb
FROM table1, table2
WHERE = table2.fkid
ORDER BY table1.columna" );
var results = selectStatement.filter( tabledata );
for (var i = 0; i < results.length; i++) {
var record = results[i];
// ...

TrimQuery Demo - Link

No comments: