Simple Database and Configuration Files with JavaScript
From Zanecorpwiki
With Javascript and Web 2.0, server side responses to dynamic requests represent data rather than content. XML is the recommended format for non-trivial data and for the sake of consistency in non-trivial applications and APIs.
These responses are often generated dynamically by a PHP script, a Java servlet, or the like. However, in many cases one can simply create and maintain the file that is the response and point the request at the response. This is useful in two cases.
First, when the data is fairly static and changes seldom, maintaining an XML file (== the database) by hand is perfectly reasonable. For non-technical users, this is easier than learning SQL syntax and table structure.
Second, generating a file from a database can decrease server load. Unless data is changing continuously, this technique can result in faster load and processing times, especially if the whole database is loaded in one go. Even when the request sends filter criteria, using xpath on a pre-processed XML data structure may yield better results than building the response from the DB, especially if the response contains data resulting from expensive queries.
In the simple case, let's say we have a retail store that sells shirts. The shirts don't change very often and there aren't that many of them, so loading the entire DB in one go is reasonable. First, we create the database file 'tshirts.db':
<tshirtDb> <tshirt name=Foo Logo image=foo.jpg /> <tshirt name=Bar Logo image=bar.jpg /> <tshirt name=Baz Logo image=baz.jpg /> </tshirtDbgt;
The JS code would look something like this:
var call = new Asynchronous();
call.complete(status, statusText, responseText, responseXml) {
//we point right at the XML so assume it's good
var tshirts = responseXml.getElementsByTagName('tshirt');
for (var i = 0; i tshirts.length; i++) insertTeeCell();
}
call.call('tshirts.db');


