Temporary CouchDB View using Node module Nano

829 Views Asked by At

Nano doesn't provide documentation for temporary views, is there any undocumented method? Failing that, how would you advise somebody execute a temporary view using a nano-like syntax. Currently I am attempting to create the view as _view/guid, query it, return the results, and then delete it from the collection:

function generateToken() {
    return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}
var db = nano.use('db'),
    fn = 'function(doc){ emit(doc); }',
    token = generateToken(),
    id = '_design/' + token;
db.insert({ views: { view: { map: fn } } }, id, function(){
    db.view(token, 'view', function (err, results) {
        db.get(id, function (err, view) {
            console.log(results);
            db.destroy(id, view._rev);
        });
    });
});

I assume this is inoptimal with the temporary view functionality built into couch core.

I'm aware of the temporary-view caveats, however I do believe I have a genuine usage case.

3

There are 3 best solutions below

0
On BEST ANSWER

The temporary view API is described in the official CouchDB documentation: http://docs.couchdb.org/en/latest/api/database/temp-views.html#post--db-_temp_view

0
On

Open up futon and see what calls it does to the couchDB api ?

Edit: went and did the above

Futon does a post to SVRNAME/DBNAME/_temp_view?limit=11&descending=true request payload {language: "javascript" map: function(doc) { emit(null, doc.id);} and you must be logged in as an admin.

Hope that helps

0
On

I think you could do this through Nano using nano.request() (or nano.dinosaur()). https://github.com/dscape/nano#nanorequestopts-callback