source: Dev/trunk/src/server/util/couch.js @ 527

Last change on this file since 527 was 527, checked in by hendrikvanantwerpen, 11 years ago
  • Dropped request module for a simple one we wrote ourselves using the native Node modules. Hopefully this one will not suffer from the problem that sometimes empty bodies are returned even when the statuscode and content-length of the request are ok & present.
  • Handle exceptions better in HTTPResult chain, they were hoisted in unknown responses before. Should we not include them in default processing, because they are special?
File size: 2.6 KB
Line 
1var request = require('./request')
2  , _ = require('underscore')
3  , HTTPResult = require('./http-result')
4  ;
5
6function CouchDB(url, db) {
7    this.serverURL = normalizeURL(url);
8    this.db = db;
9    if (this.db) {
10        this.url = "" + this.serverURL + "/" + this.db;
11    } else {
12        this.url = "" + this.serverURL;
13    }
14}
15
16CouchDB.prototype.get = function(id, opts) {
17    var url = "" + this.url + "/" + id;
18    return couchRequest('GET', url, null, opts);
19};
20
21CouchDB.prototype.post = function(doc, opts) {
22    var url = "" + this.url + "/";
23    return couchRequest('POST', url, doc, opts);
24};
25
26CouchDB.prototype.put = function(id, doc, opts) {
27    var url = "" + this.url + "/" + id;
28    return couchRequest('PUT', url, doc, opts);
29};
30
31CouchDB.prototype["delete"] = function(id, opts) {
32    var url = "" + this.url + "/" + id;
33    return couchRequest('DELETE', url, null, opts);
34};
35
36CouchDB.prototype.uuids = function(opts) {
37    var url = "" + this.serverURL + "/_uuids";
38    return couchRequest('GET', url, null, opts);
39};
40
41function normalizeURL(url) {
42    return url.replace(/\/$/, '');
43}
44
45function couchRequest (method, url, body, opts, retries) {
46    body = body || {};
47    retries = _.isNumber(retries) ? retries : 3;
48    var options = {
49        method: method,
50        headers: {},
51        body: stringifyFunctions(body),
52        json: true,
53        timeout: 5000
54    };
55    if (opts) {
56        if (opts.query) { options.qs = createQueryObj(opts.query); }
57        if (opts.headers) { _.extend(options.headers, opts.headers); }
58    }
59    return request(url, options)
60    .handle(function(status,body){
61        if ( _.isUndefined(body) ) {
62            if ( retries > 0) {
63                return couchRequest(method,url,body,opts,retries-1);
64            } else {
65                return new HTTPResult(-1,new Error("No response after several retries."));
66            }
67        } else {
68            return body;
69        }
70    });
71}
72
73function createQueryObj (obj) {
74    var newObj = {};
75    _.each(obj, function(val, key) {
76        newObj[key] = JSON.stringify(val);
77    });
78    return newObj;
79}
80
81function stringifyFunctions (value) {
82    if (value === null) {
83        return null;
84    } else if (_.isArray(value)) {
85        return value;
86    } else if (_.isFunction(value)) {
87        return value.toString();
88    } else if (_.isObject(value)) {
89        value = _.clone(value);
90        _.each(value, function(propValue, propName) {
91            value[propName] = stringifyFunctions(propValue);
92        });
93        return value;
94    } else {
95        return value;
96    }
97}
98
99module.exports = CouchDB;
Note: See TracBrowser for help on using the repository browser.