forked from mikanda/nanu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
285 lines (275 loc) · 7.51 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*jslint nomen: true, node: true, devel: true, maxlen: 79 */
'use strict';
var _request = require('request');
exports._request = _request;
_request = module.exports._request;
function request(options, callback) {
var __request = _request;
if (options._request) {
__request = options._request;
delete options._request;
}
options.headers = options.headers || {};
options.headers.accept = 'application/json';
if (callback) {
return __request(options, function (error, res) {
var e;
if (error) {
callback(error, res);
} else {
if (
res.headers['content-type'] === 'application/json'
&& typeof res.body === 'string'
) {
res.body = JSON.parse(res.body);
}
if (
res.statusCode >= 200
&& res.statusCode <= 202
) {
if (res.headers.etag) {
res.headers.etag = JSON.parse(res.headers.etag);
}
callback(null, res.body, res.headers);
} else {
e = new Error(res.body.error + ': '
+ res.body.reason);
e.error = res.body.error;
e.reason = res.body.reason;
e.statusCode = res.statusCode;
callback(e, res, res.headers);
}
}
});
} else {
return __request(options);
}
}
function Nanu(database, host) {
this._host = host || 'http://localhost:5984';
this._database = database;
};
function Doc(parent, doc) {
this._parent = parent;
this._doc = doc;
}
function DesignDoc(parent, designdoc) {
this._parent = parent;
this._design = designdoc;
}
Nanu.prototype._buildUrl = function (args) {
var url;
url = [this._host, this._database].concat(args);
return url.join('/');
};
Nanu.prototype.doc = function (doc) {
return new Doc(this, doc);
};
Nanu.prototype.design = function (designName) {
return new DesignDoc(this, designName);
};
Nanu.prototype.get = function (id, options, callback) {
options = options || {};
if (typeof options === 'function') {
callback = options;
options = {};
}
options.url = options.uri = this._host + '/' + this._database
+ '/' + id;
return request(options, callback);
};
/**
* Performs a simple document get request but with head as method. This
* encourages couchdb to only send meta data about the document.
*
* @param {String} id The id of the desired document.
* @param {Object} options Passed to `Nanu.get()`
* @param {Function} callback
*/
Nanu.prototype.head = function (id, options, callback) {
options = options || {};
if (typeof options === 'function') {
callback = options;
options = {};
}
options.method = 'HEAD';
return this.get(id, options, callback);
};
Nanu.prototype.insert = function (doc, options, callback) {
options = options || {};
if (typeof options === 'function') {
callback = options;
options = {};
} else if (options === undefined) {
options = {};
}
options.json = true;
options.body = doc;
options.url = this._host + '/' + this._database;
if (doc._id) {
options.url += '/' + doc._id;
options.method = 'PUT';
} else {
options.method = 'POST';
}
return request(options, callback);
};
Nanu.prototype.bulk = function (options, callback) {
var host = this._host,
database = this._database,
url = null;
options = options || {};
options.url = options.uri = url;
options.json = true;
options.body = options.body || {};
options.method = 'POST';
if (options.docs) {
options.body.docs = options.docs;
delete options.docs;
} else if (options.keys) {
url = [host, database, '_all_docs'].join('/');
options.body.keys = options.keys;
delete options.keys;
}
if (options.include_docs) {
options.qs = options.qs || {};
options.qs.include_docs = JSON.stringify(options.include_docs);
delete options.include_docs;
}
if (!url) {
url = [host, database, '_bulk_docs'].join('/');
}
options.url = options.uri = url;
return request(options, callback);
};
Doc.prototype._buildUrl = function () {
var args,
i;
args = [this._doc];
for (i = 0; i < arguments.length; ++i) {
args.push(arguments[i]);
}
return this._parent._buildUrl.call(this._parent, args);
};
/**
* Appends an attachment to the document. This function accepts the following
* options.
*
* * `contentType`: This gives the content type of the attachment.
* * `batch`: A truthy value enables the couchdb batch feature.
*
* @param {String} name
* @param {Object} [options]
* @param {Function} [callback]
*/
Doc.prototype.insert = function (name, options, callback) {
var url;
options = options || {};
if (options.rev) {
options.qs = options.qs || {};
options.qs.rev = options.rev;
delete options.rev;
}
if (typeof options === 'function') {
callback = options;
options = {};
}
options.method = 'PUT';
if (options.contentType) {
options.headers = options.headers || {};
options.headers['Content-Type'] = options.contentType;
delete options.contentType;
}
if (options.batch) {
options.qs = options.qs || {};
options.qs.batch = 'ok';
delete options.batch;
}
url = this._buildUrl(name);
options.uri = options.url = url;
return request(options, callback);
};
Doc.prototype.get = function (name, options, callback) {
options = options || {};
if (typeof options === 'function') {
callback = options;
options = {};
}
options.url = options.uri = this._buildUrl(name);
return request(options, callback);
};
/** Executes the given view and returns the result. */
DesignDoc.prototype.view = function (view, options, callback) {
var host = this._parent._host,
database = this._parent._database,
key;
options = options || {};
if (typeof options === 'function') {
callback = options;
options = {};
}
if (options.key) {
options.method = 'GET';
options.qs = options.qs || {};
options.qs.key = JSON.stringify(options.key);
delete options.key;
} else if (options.keys) {
options.method = 'POST';
options.json = true;
options.body = options.body || {};
options.body.keys = options.keys;
delete options.keys;
}
[
'startkey',
'endkey',
'limit',
'reduce',
'group',
'group_level',
'include_docs'
].forEach(
function (k) {
if (options.hasOwnProperty(k)) {
options.method = 'GET';
options.qs = options.qs || {};
options.qs[k] = JSON.stringify(options[k]);
delete options[k];
}
}
);
options.url = options.uri = host + '/' + database
+ '/_design/' + this._design + '/_view/' + view;
return request(options, callback);
};
DesignDoc.prototype.update = function (handler, options, id, callback) {
/* The options and the id parameter are optional so we have to figure out
* if and at which position a callback was given. */
var host = this._parent.host,
database = this._parent.database;
options = options || {};
if (typeof options === 'string') {
id = options;
options = {};
} else if (typeof options === 'function') {
callback = options;
}
if (typeof id === 'function') {
callback = id;
id = undefined;
}
var url = host + '/' + database
+ '/_design/' + this._design + '/_update/' + handler;
/* Then we construct the url and the request method dependent on the fact
* that an id was given or not. */
if (id) {
url += '/' + id;
options.method = 'PUT';
} else {
options.method = 'POST';
}
options.url = options.uri = url;
return request(options, callback);
};
exports.Nanu = Nanu;
exports.DesignDoc = DesignDoc;