Pivotal Labs

Now I understand what they mean by tabular data (or: building a relational database using jQuery and <TABLE> tags)

edit Posted by Nick Kallen on Tuesday April 08, 2008 at 07:26AM

Today I was thinking aloud about Tree Regular Expressions and how they might make a nice query language for document databases like CouchDB. Someone pointed out that CSS3 selectors might make a great concrete syntax for this. One thing lead to another and I thought, why not build a relational database in HTML? So I did. I even got inner joins working.

Let's start with a few tables:

<table class="users">
  <tr>
    <td class="id">1</td>
    <td class="first_name">amy</td>
    <td class="last_name">bobamy</td>
  </tr> 
  ...
</table>
<table class="photos">
  <tr>
    <td class="id">1</td>
    <td class="user_id">1</td>
    <td class="url">http://www.example.com/foo.png</td>
  </tr> 
</table>

Now we can express some queries:

$('.users')
  .where('.id:eq(1)')
  .select('*')

This is equivalent to SELECT * FROM users WHERE id = 1

$('.users')
  .where('.id:eq(1)')
  .select('.id, .name')

This is equivalent to SELECT id, name FROM users WHERE id = 1. Here is something slightly more complicated:

$('.users')
  .where('.name:contains(a)')
    .and('.name:contains(c)')
  .select('*')

But here is the crowning glory, the inner join:

$('.users')
  .join('.photos')
  .where('.photos.user_id:eq(.users.id)')
    .and('.users.id:eq(1)')
  .select('.photos.url')

This is equivalent to:

SELECT photos.url FROM users, photos
WHERE photos.user_id = users.id
  AND users.id = 1

Download the fun at Github.

Comments

  1. Dr Nic Dr Nic on April 08, 2008 at 01:01PM

    Cute.

  2. Tim Connor Tim Connor on April 08, 2008 at 03:34PM

    Hahahahahha, now I have to find an excuse....

  3. Aman Gupta Aman Gupta on April 08, 2008 at 06:38PM

    No benchmarks? =)

  4. Jack Danger  Canty Jack Danger Canty on April 08, 2008 at 06:48PM

    We're not in 2002 anymore. Seriously, relational database in html? I gotta go play with this.

  5. Daniel Fischer Daniel Fischer on April 08, 2008 at 07:32PM

    Haha, that's great. Rock on man.

  6. Alex Chaffee Alex Chaffee on April 09, 2008 at 01:24AM

    Hey, and with the right stylesheet, your data dumps are going to look real pretty...

    Soon you can even use [CSS3 to style tables](http://www.sitepoint.com/blogs/2008/02/28/table-based-layout-is-the-next-big-thing/ )... But darn, that would totally violate model-view separation :-)

  7. Visualdensity Visualdensity on April 09, 2008 at 06:13AM

    Geeez... this is seriously cool and fun. But dude, you have too much free time man.

  8. Vijay Chakravarthy Vijay Chakravarthy on April 09, 2008 at 07:04PM

    On my side, I ended up writing a CSS3 engine which used a relational database on the server side. The two worlds MUST NEVER COLLIDE!!

  9. seb seb on April 09, 2008 at 07:45PM

    this might actually be quite usefull for teaching database queries to students. like some sort of interactive web-based tutorial.

  10. Peter Cooper Peter Cooper on April 10, 2008 at 01:59AM

    That's it, I'm sold! I'm with Yegge, Resig and the rest. JavaScript is the only language that'll matter in a few years' time!

  11. Myles Eftos Myles Eftos on April 10, 2008 at 04:07AM

    That's awesome. Really awesome... Couple that with things like JS based table sort, you could have a hot little data search engine there...

  12. SFdude SFdude on April 10, 2008 at 08:43PM

    Very nice, using the output to Firebug Console.

    But...from a practical angle, what's the code to display the 3 query results in the web page itself, not in the Firebug Console?

    (3 results displayed in the page containing the 2 tables, "users" and "photos"...).

    Thanks! SFdude

  13. Dan Brickley Dan Brickley on April 24, 2008 at 12:45PM

    Very cool :)

    For similar fun in a different syntax, you can play with Jan Grant's 'javascript prolog'; eg. I made http://www.w3.org/1999/11/11-WWWProposal/rdfqdemo.html using http://ioctl.org/logic/

Add a Comment (MarkDown available)