Search code examples
node.jscouchdbcouchdb-nano

Node.js nano library for couchdb: timeout needed


I've been working with the nano library, and found myself with a need for having timeouts for my couchdb requests.

I'm using db.search/db.get/db.destroy/db.insert and as far as I could tell from the documentation there's no easy way to add a timeout.

These are async functions that pass a callback as a parameter. Ideally I would prefer not to modify the callbacks, but i'm open to suggestions.


Solution

  • When using nano you can provide a object that is passed to the request object:

    var db = require('nano')({"requestDefaults" : { "proxy" : "http://someproxy" }});
    

    For example, that sets a proxy to http://someproxy.

    To change the timeout, you can use the timeout property

    This code should work:

    var db = require('nano')({
      "uri": "http://localhost:5984/mydb",
      "requestDefaults" : { "timeout" : "100" } // in miliseconds
    });
    

    The default timeout in linux is about 20000ms, 20 seconds.