node.jscouchdbcradle

Retrieving all Documents from couchdb using Node.js


I am writing a simple test app to experiment with the functionality of node.js and couchdb, so far i am loving it, but i ran in a snag. i have looked for and wide but can't seem to find an answer. My test server(a simple address book) does 2 things:

  1. if the user goes to localhost:8000/{id} then my app returns the name and address of the user with that id.
  2. if the user goes to localhost:8000/ then my app needs to return a list a names that are hyperlinks and takes them to the page localhost:8000/{id}.

I was able to get the first requirement working. i cant not seem to find how to retrieve a list of all names from my couchdb. that is what i need help with. here is my code:

var http = require('http');
var cradle = require('cradle');
var conn = new(cradle.Connection)();
var db = conn.database('users');

function getUserByID(id) {
    var rv = "";

    db.get(id, function(err,doc) {
        rv = doc.name;
    rv += " lives at " + doc.Address;
});

return rv;
}

function GetAllUsers() {
var rv = ""
return rv;
}

var server =  http.createServer(function(req,res) {
res.writeHead(200, {'Content-Type':'text/plain'});
var rv = "" ;
var id = req.url.substr(1);

    if (id != "")
    rv = getUserByID(id);
else
    rv = GetAllUsers();

    res.end(rv);


});

server.listen(8000);
console.log("server is runnig");

As you can see, I need to fill in the GetAllUsers() function. Any help would be appreciated. Thanks in advance.


Solution

  • You can create a CouchDB view which will list the users. Here are several resources on CouchDB views which you should read in order to get a bigger picture on this topic:

    So let's say you have documents structured like this:

    {
        "_id": generated by CouchDB,
        "_rev": generated by CouchDB,
        "type": "user",
        "name": "Johny Bravo",
        "isHyperlink": true
    }
    

    Then you can create a CouchDB view (the map part) which would look like this:

    // view map function definition
    function(doc) {
        // first check if the doc has type and isHyperlink fields
        if(doc.type && doc.isHyperlink) {
            // now check if the type is user and isHyperlink is true (this can also inclided in the statement above)
            if((doc.type === "user") && (doc.isHyperlink === true)) {
                // if the above statements are correct then emit name as it's key and document as value (you can change what is emitted to whatever you want, this is just for example)
                emit(doc.name, doc);
            }
        }
    }
    

    When a view is created you can query it from your node.js application:

    // query a view
    db.view('location of your view', function (err, res) {
        // loop through each row returned by the view
        res.forEach(function (row) {
            // print out to console it's name and isHyperlink flag
            console.log(row.name + " - " + row.isHyperlink);
        });
    });
    

    This is just an example. First I would recommend to go through the resources above and learn the basics of CouchDB views and it's capabilities.