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

Last change on this file since 522 was 522, checked in by hendrikvanantwerpen, 11 years ago
  • Add option to deploy source to Heroku (for debugging).
  • Rewrite xhr so requests are not serialized anymore.
  • Give more sane errors on request errors to Couch (like network errors and such).
File size: 2.8 KB
Line 
1var CouchDB, _ref,
2  __hasProp = {}.hasOwnProperty;
3
4var request = require('./q-request')
5  , _ = require('underscore')
6  , Q = require('q');
7
8function CouchDB(url, db) {
9    this.serverURL = normalizeURL(url);
10    this.db = db;
11    if (this.db) {
12        this.url = "" + this.serverURL + "/" + this.db;
13    } else {
14        this.url = "" + this.serverURL;
15    }
16}
17
18CouchDB.prototype.get = function(id, opts) {
19    var url = "" + this.url + "/" + id;
20    return couchRequest('GET', url, null, opts);
21};
22
23CouchDB.prototype.post = function(doc, opts) {
24    var url = "" + this.url + "/";
25    return couchRequest('POST', url, doc, opts);
26};
27
28CouchDB.prototype.put = function(id, doc, opts) {
29    var url = "" + this.url + "/" + id;
30    return couchRequest('PUT', url, doc, opts);
31};
32
33CouchDB.prototype["delete"] = function(id, opts) {
34    var url = "" + this.url + "/" + id;
35    return couchRequest('DELETE', url, null, opts);
36};
37
38CouchDB.prototype.uuids = function(opts) {
39    var url = "" + this.serverURL + "/_uuids";
40    return couchRequest('GET', url, null, opts);
41};
42
43function normalizeURL(url) {
44    return url.replace(/\/$/, '');
45}
46
47function couchRequest (method, url, body, opts) {
48    var options = {
49        method: method,
50        headers: {
51            'Content-Type': 'application/json; charset=utf-8',
52            'Accept': 'application/json'
53        },
54        body: JSON.stringify(stringifyFunctions(body || {}))
55    };
56    if (opts) {
57        if (opts.query) { options.qs = createQueryObj(opts.query); }
58        if (opts.headers) { _.extend(options.headers, opts.headers); }
59    }
60    var req = request(url, options);
61    var res = req.response
62    .then(function(res) {
63        return req.then(function(res) {
64            return JSON.parse(res);
65        }, function(err) {
66            return Q.reject(JSON.parse(err));
67        });
68    }, function(err) {
69        return Q.reject(err);
70    });
71    res.response = req.response
72    .then(function(res) {
73        return {
74            statusCode: res.statusCode,
75            headers: res.headers,
76            body: JSON.parse(res.body)
77        };
78    }, function(err) {
79        return Q.reject({error:err.message});
80    });
81    return res;
82}
83
84function createQueryObj (obj) {
85    var newObj = {};
86    _.each(obj, function(val, key) {
87        newObj[key] = JSON.stringify(val);
88    });
89    return newObj;
90}
91
92function stringifyFunctions (value) {
93    if (value === null) {
94        return null;
95    } else if (_.isArray(value)) {
96        return value;
97    } else if (_.isFunction(value)) {
98        return value.toString();
99    } else if (_.isObject(value)) {
100        value = _.clone(value);
101        _.each(value, function(propValue, propName) {
102            value[propName] = stringifyFunctions(propValue);
103        });
104        return value;
105    } else {
106        return value;
107    }
108}
109
110exports.CouchDB = CouchDB;
Note: See TracBrowser for help on using the repository browser.