1 | var request = require('./request') |
---|
2 | , _ = require('underscore') |
---|
3 | ; |
---|
4 | |
---|
5 | function 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 | |
---|
15 | CouchDB.prototype.get = function(id, opts) { |
---|
16 | var url = "" + this.url + "/" + id; |
---|
17 | return couchRequest('GET', url, null, opts); |
---|
18 | }; |
---|
19 | |
---|
20 | CouchDB.prototype.post = function(doc, opts) { |
---|
21 | var url = "" + this.url + "/"; |
---|
22 | return couchRequest('POST', url, doc, opts); |
---|
23 | }; |
---|
24 | |
---|
25 | CouchDB.prototype.put = function(id, doc, opts) { |
---|
26 | var url = "" + this.url + "/" + id; |
---|
27 | return couchRequest('PUT', url, doc, opts); |
---|
28 | }; |
---|
29 | |
---|
30 | CouchDB.prototype["delete"] = function(id, opts) { |
---|
31 | var url = "" + this.url + "/" + id; |
---|
32 | return couchRequest('DELETE', url, null, opts); |
---|
33 | }; |
---|
34 | |
---|
35 | CouchDB.prototype.uuids = function(opts) { |
---|
36 | var url = "" + this.serverURL + "/_uuids"; |
---|
37 | return couchRequest('GET', url, null, opts); |
---|
38 | }; |
---|
39 | |
---|
40 | function normalizeURL(url) { |
---|
41 | return url.replace(/\/$/, ''); |
---|
42 | } |
---|
43 | |
---|
44 | function 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 | |
---|
64 | function createQueryObj (obj) { |
---|
65 | var newObj = {}; |
---|
66 | _.each(obj, function(val, key) { |
---|
67 | newObj[key] = JSON.stringify(val); |
---|
68 | }); |
---|
69 | return newObj; |
---|
70 | } |
---|
71 | |
---|
72 | function 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 | |
---|
90 | module.exports = CouchDB; |
---|