Aaron Yodaiken
Aaron Yodaiken

Reputation: 19551

JavaScript DB API design

I'm building a database that can be accessed by JavaScript, and I'd like to be able to represent queries within JavaScript which will then be translated into something else.

Currently, I have an API that works like this:

var db; /* don't worry about the definition */
var is; /* db functions, don't worry about definition */

/* variables from HTTP session,  */
var $session; /* $session == {"user": 12345} */

var results = [];

db.put("posts").
        where("date", is.between("January 10 2010", "December 10 2010")).
        where("author", is("John Doe")).
        where("user", is($session.user).
        into(results);  

The above code would get all the posts from the database with dates between January 10 2010 and December 10 2010 etc and put them as array elements in results, which could then be accessed like normal objects, eg:

var i;
for(i = 0; i < results.length; i++) {
    alert("Result #" + i + ": " + results[i].title);
}

The database is not SQL, but holds "typed" JSON objectes (if this is important, I can elaborate further).

Is this API a good one? Or should I do something else? I'd like to avoid strings as the only method ala MySQL.


My other idea was to somehow mimic the DOM (something like JQuery), but I'm not sure what the best way of doing that would be.

Upvotes: 0

Views: 816

Answers (2)

Greg Guida
Greg Guida

Reputation: 7512

Check out SQLike, its a DB written in JavaScript that uses a SQL "like" syntax. might be exactly what you're looking for.

Blog Post: http://www.thomasfrank.se/sqlike.html <- you can get the un-minified source here

Demo: http://www.thomasfrank.se/SQLike/

Upvotes: 1

tbombach
tbombach

Reputation: 164

It seems good - it's very similar to other query building syntax structures (see http://framework.zend.com/manual/en/zend.db.select.html for an example of how a PHP library does it similarly).

Upvotes: 0

Related Questions