[525] | 1 | var request = require('./request') |
---|
[494] | 2 | , _ = require('underscore') |
---|
[527] | 3 | , HTTPResult = require('./http-result') |
---|
[525] | 4 | ; |
---|
[494] | 5 | |
---|
| 6 | function 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 | |
---|
| 16 | CouchDB.prototype.get = function(id, opts) { |
---|
| 17 | var url = "" + this.url + "/" + id; |
---|
| 18 | return couchRequest('GET', url, null, opts); |
---|
| 19 | }; |
---|
| 20 | |
---|
| 21 | CouchDB.prototype.post = function(doc, opts) { |
---|
| 22 | var url = "" + this.url + "/"; |
---|
| 23 | return couchRequest('POST', url, doc, opts); |
---|
| 24 | }; |
---|
| 25 | |
---|
| 26 | CouchDB.prototype.put = function(id, doc, opts) { |
---|
| 27 | var url = "" + this.url + "/" + id; |
---|
| 28 | return couchRequest('PUT', url, doc, opts); |
---|
| 29 | }; |
---|
| 30 | |
---|
| 31 | CouchDB.prototype["delete"] = function(id, opts) { |
---|
| 32 | var url = "" + this.url + "/" + id; |
---|
| 33 | return couchRequest('DELETE', url, null, opts); |
---|
| 34 | }; |
---|
| 35 | |
---|
| 36 | CouchDB.prototype.uuids = function(opts) { |
---|
| 37 | var url = "" + this.serverURL + "/_uuids"; |
---|
| 38 | return couchRequest('GET', url, null, opts); |
---|
| 39 | }; |
---|
| 40 | |
---|
| 41 | function normalizeURL(url) { |
---|
| 42 | return url.replace(/\/$/, ''); |
---|
| 43 | } |
---|
| 44 | |
---|
[527] | 45 | function couchRequest (method, url, body, opts, retries) { |
---|
| 46 | body = body || {}; |
---|
| 47 | retries = _.isNumber(retries) ? retries : 3; |
---|
[494] | 48 | var options = { |
---|
| 49 | method: method, |
---|
[527] | 50 | headers: {}, |
---|
| 51 | body: stringifyFunctions(body), |
---|
| 52 | json: true, |
---|
| 53 | timeout: 5000 |
---|
[494] | 54 | }; |
---|
| 55 | if (opts) { |
---|
| 56 | if (opts.query) { options.qs = createQueryObj(opts.query); } |
---|
| 57 | if (opts.headers) { _.extend(options.headers, opts.headers); } |
---|
| 58 | } |
---|
[525] | 59 | return request(url, options) |
---|
[527] | 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 | }); |
---|
[494] | 71 | } |
---|
| 72 | |
---|
| 73 | function createQueryObj (obj) { |
---|
| 74 | var newObj = {}; |
---|
| 75 | _.each(obj, function(val, key) { |
---|
| 76 | newObj[key] = JSON.stringify(val); |
---|
| 77 | }); |
---|
| 78 | return newObj; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | function 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 | |
---|
[525] | 99 | module.exports = CouchDB; |
---|