incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nathan Vander Wilt <nate-li...@calftrail.com>
Subject ANN: Fermata, a "native" JavaScript library for CouchDB (and everything else)
Date Sat, 13 Aug 2011 04:29:16 GMT
I was given some encouragement at CouchConf to announce my little REST library that's been
working beautifully for CouchDB clients (and more):
https://github.com/andyet/fermata (single <script> include with no compile, and/or `npm
install fermata`)


Why did I bother building yet another JavaScipt HTTP library? I grew tired of building URL
strings like `var url = "http://" + myServer + '/' + myApp + "_view/by_date?reduce=false&startkey="
+ encodeURIComponent(JSON.stringify([2001,1])) + "&limit=" + myLimit` — and then this
spring I finally noticed that JavaScript's object/method syntax matches REST so much better
than passing strings/dictionaries to functions like `$.ajax()`.

With Fermata it's really easy to access CouchDB from a browser:

    var photoDB = fermata.api({url:"http://localhost:5984"})('photos');
    var photosIndexed = photoDB(['_design/shutterstem', '_view']);
    
    photosIndexed('by_date')({group_level:3}).get(function (err,data) {
        console.log(data.rows);
        // ...
    });
    
    photosIndexed('by_date')({reduce:false, limit:500, include_docs:true}).get(function (e,d)
{
        var thumbs = d3.select('#content').selectAll('.frame').data(d.rows);
        thumbs.enter().append('div').classed('frame', true).append('img').classed('photo',
true)
            .attr('src', function (d) { return photoDB([d.id, 'thumbnail/64.jpg'])(); });
    });

In node.js the syntax is even cleaner, thanks to Harmony Proxy objects:
    
    var myCouch = fermata.api({url:"http://localhost:5984"});
    var indexedPhotos = myCouch.photos[appName]._view;
    
    indexedPhotos.by_date({reduce:false, $startkey=[2011,1], $endkey=[2012]}).get(function
(e,d) { console.log(d); });

Of course the "broswer" syntax works in node.js server code too, but it's pretty fun to pretend
that any REST API is just a local JavaScript object. (The dot syntax does actually work in
Firefox and soon Chrome, but for clientside or mixed code I recommend sticking to parentheses
for compatibility.) My teammates at @andyet keep encouraging me that Fermata's subtle differences
really are a big deal in practice: we can assign, extend and access URLs throughout our code
as if they were (virtual) nested JavaScript objects.

I'm currently working on plugin support (for sending XML, OAuth signatures, etc.) but that's
getting off-topic for this list: Fermata already works great with CouchDB and I hope some
of you will find it as useful as we have. Would love to hear your feedback!

regards,
-natevw

p.s. are many people using CouchDB from Python? ;-)
Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message