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

Last change on this file since 525 was 525, checked in by hendrikvanantwerpen, 11 years ago
  • Allow empty subcodes.
  • Use HTTPResult exclusively on server (no more q).
  • Set readonly & disabled on ourselves as well in _ComplexValueMixin
  • Split server into several modules.
  • Check codes on the variable level, not question level.
  • We can add modules in design documents now.
File size: 2.3 KB
Line 
1var request = require('./request')
2  , _ = require('underscore')
3  ;
4
5function CouchDB(url, db) {
6    this.serverURL = normalizeURL(url);
7    this.db = db;
8    if (this.db) {
9        this.url = "" + this.serverURL + "/" + this.db;
10    } else {
11        this.url = "" + this.serverURL;
12    }
13}
14
15CouchDB.prototype.get = function(id, opts) {
16    var url = "" + this.url + "/" + id;
17    return couchRequest('GET', url, null, opts);
18};
19
20CouchDB.prototype.post = function(doc, opts) {
21    var url = "" + this.url + "/";
22    return couchRequest('POST', url, doc, opts);
23};
24
25CouchDB.prototype.put = function(id, doc, opts) {
26    var url = "" + this.url + "/" + id;
27    return couchRequest('PUT', url, doc, opts);
28};
29
30CouchDB.prototype["delete"] = function(id, opts) {
31    var url = "" + this.url + "/" + id;
32    return couchRequest('DELETE', url, null, opts);
33};
34
35CouchDB.prototype.uuids = function(opts) {
36    var url = "" + this.serverURL + "/_uuids";
37    return couchRequest('GET', url, null, opts);
38};
39
40function normalizeURL(url) {
41    return url.replace(/\/$/, '');
42}
43
44function couchRequest (method, url, body, opts) {
45    var options = {
46        method: method,
47        headers: {
48            'Content-Type': 'application/json; charset=utf-8',
49            'Accept': 'application/json'
50        },
51        body: JSON.stringify(stringifyFunctions(body || {}))
52    };
53    if (opts) {
54        if (opts.query) { options.qs = createQueryObj(opts.query); }
55        if (opts.headers) { _.extend(options.headers, opts.headers); }
56    }
57    return request(url, options)
58        .handle({
59            '-1': _.identity,
60            default: function(status,result) { return JSON.parse(result); }
61        });
62}
63
64function createQueryObj (obj) {
65    var newObj = {};
66    _.each(obj, function(val, key) {
67        newObj[key] = JSON.stringify(val);
68    });
69    return newObj;
70}
71
72function stringifyFunctions (value) {
73    if (value === null) {
74        return null;
75    } else if (_.isArray(value)) {
76        return value;
77    } else if (_.isFunction(value)) {
78        return value.toString();
79    } else if (_.isObject(value)) {
80        value = _.clone(value);
81        _.each(value, function(propValue, propName) {
82            value[propName] = stringifyFunctions(propValue);
83        });
84        return value;
85    } else {
86        return value;
87    }
88}
89
90module.exports = CouchDB;
Note: See TracBrowser for help on using the repository browser.