node.jscouchdbcouchdb-nano

Simple field query?


I'm using nano to connect to couchDB through node.js; I have read the basic documentation for couch.db and understand it for the most part, but I didn't see a simple query function anywhere. All I would like to do is (from my server), get the value of a field , from a specific document. How would I do this?

Additionally, while looking for the answer to this, I ran across one site that said an html page can directly send a GET to the DB to get values; I thought the database was secure though, so how is this possible? I guess I'm missing something big here.


Solution

  • The simplest way to get a specific document by id is using the get method. This will return you the document itself:

    var Nano = require('nano'),
        db = new Nano('http://admin:password@localhost:5984').use(yourDatabase),
    
    db.get(yourDocumentId, function (err, yourDocument, headers) {
      return console.log(err || yourDocument);
      });
    

    Re your second question about a webpage requesting data straight from CouchDB: this can be done securely as long as you have a login specifically for your end user in CouchDB. Your end user would log in to CouchDB and CouchDB would return a cookie which your browser would send on each subsequent request to CouchDB as a way of authenticating itself. see http://guide.couchdb.org/editions/1/en/security.html#cookies for more info.

    Personally I would not use this approach as it makes it too easy to couple your UI to your data making future changes harder. I would recommend creating a REST api in express and call that from your UI. It will keep your UI code more focused on UI logic and your api implementation focused on dealing with CouchDB document and view operations.