1 | var CouchDB, _ref, |
---|
2 | __hasProp = {}.hasOwnProperty; |
---|
3 | |
---|
4 | var request = require('./q-request') |
---|
5 | , _ = require('underscore') |
---|
6 | , Q = require('q'); |
---|
7 | |
---|
8 | function 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 | |
---|
18 | CouchDB.prototype.get = function(id, opts) { |
---|
19 | var url = "" + this.url + "/" + id; |
---|
20 | return couchRequest('GET', url, null, opts); |
---|
21 | }; |
---|
22 | |
---|
23 | CouchDB.prototype.post = function(doc, opts) { |
---|
24 | var url = "" + this.url + "/"; |
---|
25 | return couchRequest('POST', url, doc, opts); |
---|
26 | }; |
---|
27 | |
---|
28 | CouchDB.prototype.put = function(id, doc, opts) { |
---|
29 | var url = "" + this.url + "/" + id; |
---|
30 | return couchRequest('PUT', url, doc, opts); |
---|
31 | }; |
---|
32 | |
---|
33 | CouchDB.prototype["delete"] = function(id, opts) { |
---|
34 | var url = "" + this.url + "/" + id; |
---|
35 | return couchRequest('DELETE', url, null, opts); |
---|
36 | }; |
---|
37 | |
---|
38 | CouchDB.prototype.uuids = function(opts) { |
---|
39 | var url = "" + this.serverURL + "/_uuids"; |
---|
40 | return couchRequest('GET', url, null, opts); |
---|
41 | }; |
---|
42 | |
---|
43 | function normalizeURL(url) { |
---|
44 | return url.replace(/\/$/, ''); |
---|
45 | } |
---|
46 | |
---|
47 | function 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 | |
---|
84 | function createQueryObj (obj) { |
---|
85 | var newObj = {}; |
---|
86 | _.each(obj, function(val, key) { |
---|
87 | newObj[key] = JSON.stringify(val); |
---|
88 | }); |
---|
89 | return newObj; |
---|
90 | } |
---|
91 | |
---|
92 | function 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 | |
---|
110 | exports.CouchDB = CouchDB; |
---|