clerk 0.8.2 → 0.8.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/bin/clerk +62 -0
- package/package.json +32 -30
- package/.npmignore +0 -6
- package/.travis.yml +0 -12
- package/HISTORY.md +0 -132
- package/LICENSE.txt +0 -202
- package/README.md +0 -117
- package/clerk.js +0 -1310
- package/dist/.gitkeep +0 -0
- package/dist/clerk.js +0 -2690
- package/dist/clerk.js.map +0 -1
- package/dist/clerk.min.js +0 -20
- package/dist/clerk.min.js.map +0 -1
- package/index.js +0 -63
- package/jsdoc.json +0 -25
- package/karma.conf.js +0 -35
- package/test/base_test.js +0 -41
- package/test/clerk_test.js +0 -41
- package/test/client_test.js +0 -193
- package/test/database_test.js +0 -510
- package/test/design.js +0 -32
- package/test/shared.js +0 -58
- package/webpack.config.js +0 -20
package/clerk.js
DELETED
|
@@ -1,1310 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
/*!
|
|
4
|
-
|
|
5
|
-
clerk - CouchDB client for node and the browser.
|
|
6
|
-
Copyright 2012-2015 Michael Phan-Ba
|
|
7
|
-
|
|
8
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
9
|
-
you may not use this file except in compliance with the License.
|
|
10
|
-
You may obtain a copy of the License at
|
|
11
|
-
|
|
12
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
13
|
-
|
|
14
|
-
Unless required by applicable law or agreed to in writing, software
|
|
15
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
16
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17
|
-
See the License for the specific language governing permissions and
|
|
18
|
-
limitations under the License.
|
|
19
|
-
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
// Module dependencies.
|
|
23
|
-
var request = require("superagent");
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Copy properties from sources to target.
|
|
27
|
-
*
|
|
28
|
-
* @param {Object} target The target object.
|
|
29
|
-
* @param {...Object} sources The source object.
|
|
30
|
-
* @return {Object} The target object.
|
|
31
|
-
* @private
|
|
32
|
-
*/
|
|
33
|
-
|
|
34
|
-
var extend = function (target /* ...sources */) {
|
|
35
|
-
var source, key, i = 1;
|
|
36
|
-
while (source = arguments[i++]) {
|
|
37
|
-
for (key in source) target[key] = source[key];
|
|
38
|
-
}
|
|
39
|
-
return target;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Stringify value.
|
|
44
|
-
*
|
|
45
|
-
* @param {Object} that That value to stringify.
|
|
46
|
-
* @return {String} The stringifyed value.
|
|
47
|
-
* @private
|
|
48
|
-
*/
|
|
49
|
-
|
|
50
|
-
var asString = function (that) {
|
|
51
|
-
return Object.prototype.toString.call(that);
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Check if value is a string.
|
|
56
|
-
*
|
|
57
|
-
* @param {Object} that That value to check.
|
|
58
|
-
* @return {Boolean} `true` if string, `false` otherwise.
|
|
59
|
-
* @private
|
|
60
|
-
*/
|
|
61
|
-
|
|
62
|
-
var isString = function (that) {
|
|
63
|
-
return asString(that) == "[object String]";
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Check if value is an object.
|
|
68
|
-
*
|
|
69
|
-
* @param {Object} that That value to check.
|
|
70
|
-
* @return {Boolean} `true` if object, `false` otherwise.
|
|
71
|
-
* @private
|
|
72
|
-
*/
|
|
73
|
-
|
|
74
|
-
var isObject = function (that) {
|
|
75
|
-
return asString(that) == "[object Object]";
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Check if value is an array.
|
|
80
|
-
*
|
|
81
|
-
* @param {Object} that That value to check.
|
|
82
|
-
* @return {Boolean} `true` if array, `false` otherwise.
|
|
83
|
-
* @private
|
|
84
|
-
*/
|
|
85
|
-
|
|
86
|
-
var isArray = function (that) {
|
|
87
|
-
return asString(that) == "[object Array]";
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Check if value is a function.
|
|
92
|
-
*
|
|
93
|
-
* @param {Object} that That value to check.
|
|
94
|
-
* @return {Boolean} `true` if function, `false` otherwise.
|
|
95
|
-
* @private
|
|
96
|
-
*/
|
|
97
|
-
|
|
98
|
-
var isFunction = function (that) {
|
|
99
|
-
return asString(that) == "[object Function]";
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Clerk library entry point.
|
|
104
|
-
*
|
|
105
|
-
* @param {String} uri CouchDB server URI.
|
|
106
|
-
* @return {Client|DB} If a URI path is given, returns a `DB`, otherwise
|
|
107
|
-
* returns a `Client`.
|
|
108
|
-
* @see {@link http://docs.couchdb.org|CouchDB Documentation}
|
|
109
|
-
* @see {@link http://guide.couchdb.org/|CouchDB Guide}
|
|
110
|
-
* @see {@link http://wiki.apache.org/couchdb/|CouchDB Wiki}
|
|
111
|
-
*/
|
|
112
|
-
|
|
113
|
-
function clerk (uri) {
|
|
114
|
-
return clerk.make(uri);
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Library version.
|
|
119
|
-
* @type {String}
|
|
120
|
-
*/
|
|
121
|
-
|
|
122
|
-
clerk.version = "0.8.2";
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Default host.
|
|
126
|
-
* @type {String}
|
|
127
|
-
*/
|
|
128
|
-
|
|
129
|
-
clerk.defaultHost = "http://127.0.0.1:5984";
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Create single CouchDB client.
|
|
133
|
-
*
|
|
134
|
-
* @param {String} uri Fully qualified URI.
|
|
135
|
-
* @return {Client|DB} If `uri` has a path, the last segment of the
|
|
136
|
-
* path is used as the database name and a `DB` instance is
|
|
137
|
-
* returned. Otherwise, a `Client` instance is returned.
|
|
138
|
-
*/
|
|
139
|
-
|
|
140
|
-
clerk.make = function (uri) {
|
|
141
|
-
if (!uri) return new Client(this.defaultHost);
|
|
142
|
-
|
|
143
|
-
uri = clerk._parseURI(uri);
|
|
144
|
-
|
|
145
|
-
var db = /\/*([^\/]+)\/*$/.exec(uri.path);
|
|
146
|
-
if (db) {
|
|
147
|
-
uri.path = uri.path.substr(0, db.index);
|
|
148
|
-
db = db[1] && decodeURIComponent(db[1]);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// weird way of doing it, but it's more efficient...
|
|
152
|
-
if (uri.auth) uri.auth = 'Basic ' + clerk.btoa(uri.auth);
|
|
153
|
-
|
|
154
|
-
var client = new clerk.Client(uri.base + uri.path, uri.auth);
|
|
155
|
-
return db ? client.db(db) : client;
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Base64-encode a string.
|
|
160
|
-
*
|
|
161
|
-
* @param {String} str
|
|
162
|
-
* @return {String}
|
|
163
|
-
*/
|
|
164
|
-
|
|
165
|
-
clerk.btoa = typeof Buffer != "undefined" ? function (str) {
|
|
166
|
-
return new Buffer(str).toString("base64");
|
|
167
|
-
} : function (str) {
|
|
168
|
-
return btoa(str);
|
|
169
|
-
};
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* Parse URI.
|
|
173
|
-
*
|
|
174
|
-
* The URI is normalized by removing extra `//` in the path.
|
|
175
|
-
*
|
|
176
|
-
* @param {String} uri Fully qualified URI.
|
|
177
|
-
* @return {String} The normalized URI.
|
|
178
|
-
* @private
|
|
179
|
-
*/
|
|
180
|
-
|
|
181
|
-
clerk._parseURI = function (uri) {
|
|
182
|
-
var match;
|
|
183
|
-
|
|
184
|
-
if (uri) {
|
|
185
|
-
if (match = /^(https?:\/\/)(?:([^\/@]+)@)?([^\/]+)(.*)\/*$/.exec(uri)) {
|
|
186
|
-
return {
|
|
187
|
-
base: match[1] + match[3].replace(/\/+/g, "\/"),
|
|
188
|
-
path: match[4],
|
|
189
|
-
auth: match[2] && decodeURIComponent(match[2])
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
return { base: uri || "", path: "" };
|
|
195
|
-
};
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* Base prototype for `Client` and `DB`.
|
|
199
|
-
* Encapsulates HTTP methods, JSON handling, and response coersion.
|
|
200
|
-
*
|
|
201
|
-
* @constructor
|
|
202
|
-
* @memberof clerk
|
|
203
|
-
*/
|
|
204
|
-
|
|
205
|
-
function Base () {};
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Service request and parse JSON response.
|
|
209
|
-
*
|
|
210
|
-
* @param {String} [method=GET] HTTP method.
|
|
211
|
-
* @param {String} [path=this.uri] HTTP URI.
|
|
212
|
-
* @param {Object} [query] HTTP query options.
|
|
213
|
-
* @param {Object} [body] HTTP body.
|
|
214
|
-
* @param {Object} [headers] HTTP headers.
|
|
215
|
-
* @param {handler} [callback] Callback function.
|
|
216
|
-
* @return {Promise} A Promise, if no callback is provided,
|
|
217
|
-
* otherwise `null`.
|
|
218
|
-
*/
|
|
219
|
-
|
|
220
|
-
Base.prototype.request = function (/* [method], [path], [query], [body], [headers], [callback] */) {
|
|
221
|
-
var args = [].slice.call(arguments);
|
|
222
|
-
var callback = isFunction (args[args.length - 1]) && args.pop();
|
|
223
|
-
|
|
224
|
-
return this._request({
|
|
225
|
-
method: args[0],
|
|
226
|
-
path: args[1],
|
|
227
|
-
query: args[2],
|
|
228
|
-
data: args[3],
|
|
229
|
-
headers: args[4],
|
|
230
|
-
fn: callback
|
|
231
|
-
});
|
|
232
|
-
};
|
|
233
|
-
|
|
234
|
-
/**
|
|
235
|
-
* Internal service request and parse JSON response handler.
|
|
236
|
-
*
|
|
237
|
-
* @param {String} options
|
|
238
|
-
* @param {String} method HTTP method.
|
|
239
|
-
* @param {String} path HTTP URI.
|
|
240
|
-
* @param {Object} query HTTP query options.
|
|
241
|
-
* @param {Object} data HTTP body data.
|
|
242
|
-
* @param {Object} headers HTTP headers.
|
|
243
|
-
* @param {handler} [callback] Callback function.
|
|
244
|
-
* @private
|
|
245
|
-
*/
|
|
246
|
-
|
|
247
|
-
Base.prototype._request = function (options) {
|
|
248
|
-
var self = this;
|
|
249
|
-
|
|
250
|
-
if (options.method == null) options.method = "GET";
|
|
251
|
-
if (options.headers == null) options.headers = {};
|
|
252
|
-
if (options.auth == null) options.auth = this.auth;
|
|
253
|
-
|
|
254
|
-
options.path = options.path ? "/" + options.path : "";
|
|
255
|
-
|
|
256
|
-
// set default headers
|
|
257
|
-
if (options.headers["Content-Type"] == null) {
|
|
258
|
-
options.headers["Content-Type"] = "application/json";
|
|
259
|
-
}
|
|
260
|
-
if (options.headers["Accept"] == null) {
|
|
261
|
-
options.headers["Accept"] = "application/json";
|
|
262
|
-
}
|
|
263
|
-
if (this.auth && options.headers["Authorization"] == null) {
|
|
264
|
-
options.headers["Authorization"] = this.auth;
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
options.uri = this.uri + options.path;
|
|
268
|
-
options.body = options.data && JSON.stringify(options.data,
|
|
269
|
-
/^\/_design/.test(options.path) && this._replacer
|
|
270
|
-
) || "";
|
|
271
|
-
|
|
272
|
-
// create promise if no callback given
|
|
273
|
-
var promise, req;
|
|
274
|
-
if (!options.fn && typeof Promise != "undefined") {
|
|
275
|
-
promise = new Promise(function (resolve, reject) {
|
|
276
|
-
options.fn = function (err, data, status, headers, res) {
|
|
277
|
-
if (err) {
|
|
278
|
-
err.body = data;
|
|
279
|
-
err.status = status;
|
|
280
|
-
err.headers = headers;
|
|
281
|
-
err.res = res;
|
|
282
|
-
reject(err);
|
|
283
|
-
} else {
|
|
284
|
-
if (Object.defineProperties) {
|
|
285
|
-
Object.defineProperties(data, {
|
|
286
|
-
_status: { value: status },
|
|
287
|
-
_headers: { value: headers },
|
|
288
|
-
_response: { value: res },
|
|
289
|
-
});
|
|
290
|
-
}
|
|
291
|
-
resolve(data);
|
|
292
|
-
};
|
|
293
|
-
};
|
|
294
|
-
});
|
|
295
|
-
req = send();
|
|
296
|
-
promise.request = req;
|
|
297
|
-
promise.abort = function () {
|
|
298
|
-
req.abort();
|
|
299
|
-
options.fn(new Error("abort"));
|
|
300
|
-
return promise;
|
|
301
|
-
};
|
|
302
|
-
return promise;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
send();
|
|
306
|
-
|
|
307
|
-
function send () {
|
|
308
|
-
// apply response transforms
|
|
309
|
-
var g = options._;
|
|
310
|
-
var fn = options.fn;
|
|
311
|
-
if (fn) {
|
|
312
|
-
options.fn = g ? function () {
|
|
313
|
-
fn.apply(self, g.apply(self, arguments) || arguments);
|
|
314
|
-
} : fn;
|
|
315
|
-
}
|
|
316
|
-
return self._do(options);
|
|
317
|
-
}
|
|
318
|
-
};
|
|
319
|
-
|
|
320
|
-
/**
|
|
321
|
-
* Provider for servicing requests and parsing JSON responses.
|
|
322
|
-
*
|
|
323
|
-
* @param {String} options
|
|
324
|
-
* @param {String} method HTTP method.
|
|
325
|
-
* @param {String} uri HTTP URI.
|
|
326
|
-
* @param {Object} query HTTP query options.
|
|
327
|
-
* @param {Object} body HTTP body.
|
|
328
|
-
* @param {Object} headers HTTP headers.
|
|
329
|
-
* @param {Object} auth HTTP authentication.
|
|
330
|
-
* @param {handler} [callback] Callback function.
|
|
331
|
-
* @private
|
|
332
|
-
*/
|
|
333
|
-
|
|
334
|
-
Base.prototype._do = function (options) {
|
|
335
|
-
var self = this;
|
|
336
|
-
var key, value;
|
|
337
|
-
var fn = options.fn;
|
|
338
|
-
|
|
339
|
-
// create request
|
|
340
|
-
var req = request(options.method, options.uri);
|
|
341
|
-
|
|
342
|
-
// query string
|
|
343
|
-
if (options.query) {
|
|
344
|
-
// ensure query Array values are JSON encoded
|
|
345
|
-
for (key in options.query) {
|
|
346
|
-
if (typeof(value = options.query[key]) === "object") {
|
|
347
|
-
options.query[key] = JSON.stringify(value);
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
// set query on request
|
|
351
|
-
req.query(options.query);
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
// set headers
|
|
355
|
-
if (options.headers) {
|
|
356
|
-
req.set(options.headers);
|
|
357
|
-
// if authenticating
|
|
358
|
-
if (req.withCredentials && options.headers["Authorization"] != null) {
|
|
359
|
-
req.withCredentials();
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
// send body
|
|
364
|
-
if (options.body) req.send(options.body);
|
|
365
|
-
|
|
366
|
-
// send request
|
|
367
|
-
req.end(function (err, res) {
|
|
368
|
-
var data;
|
|
369
|
-
|
|
370
|
-
if (!err) {
|
|
371
|
-
if (!(data = res.body)) { data = res.text; }
|
|
372
|
-
else if (data.error) err = self._error(data);
|
|
373
|
-
else data = self._response(data);
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
if (err && fn) {
|
|
377
|
-
var response = res || {};
|
|
378
|
-
return fn(err, data, response.status, response.header, res);
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
res.data = data;
|
|
382
|
-
if (fn) fn(err || null, data, res.status, res.header, res);
|
|
383
|
-
});
|
|
384
|
-
|
|
385
|
-
return req;
|
|
386
|
-
};
|
|
387
|
-
|
|
388
|
-
/**
|
|
389
|
-
* Coerce response to normalize access to `_id` and `_rev`.
|
|
390
|
-
*
|
|
391
|
-
* @param {Object} json The response JSON.
|
|
392
|
-
* @return The coerced JSON.
|
|
393
|
-
* @private
|
|
394
|
-
*/
|
|
395
|
-
|
|
396
|
-
Base.prototype._response = function (json) {
|
|
397
|
-
var data = json.rows || json.results || json.uuids || isArray(json) && json;
|
|
398
|
-
var meta = this._meta;
|
|
399
|
-
var i = 0, len, item;
|
|
400
|
-
|
|
401
|
-
if (data) {
|
|
402
|
-
extend(data, json).json = json;
|
|
403
|
-
for (len = data.length; i < len; i++) {
|
|
404
|
-
item = data[i] = meta(data[i]);
|
|
405
|
-
if (item.doc) item.doc = meta(item.doc);
|
|
406
|
-
}
|
|
407
|
-
} else {
|
|
408
|
-
data = meta(json);
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
return data;
|
|
412
|
-
};
|
|
413
|
-
|
|
414
|
-
/**
|
|
415
|
-
* Make an error out of the response.
|
|
416
|
-
*
|
|
417
|
-
* @param {Object} json The response JSON.
|
|
418
|
-
* @return An `Error` object.
|
|
419
|
-
* @private
|
|
420
|
-
*/
|
|
421
|
-
|
|
422
|
-
Base.prototype._error = function (json) {
|
|
423
|
-
var err = new Error(json.reason);
|
|
424
|
-
err.code = json.error;
|
|
425
|
-
return extend(err, json);
|
|
426
|
-
};
|
|
427
|
-
|
|
428
|
-
/**
|
|
429
|
-
* JSON stringify functions. Used for encoding view documents to JSON.
|
|
430
|
-
*
|
|
431
|
-
* @param {String} key The key to stringify.
|
|
432
|
-
* @param {Object} val The value to stringify.
|
|
433
|
-
* @return {Object} The stringified function value or the value.
|
|
434
|
-
* @private
|
|
435
|
-
*/
|
|
436
|
-
|
|
437
|
-
Base.prototype._replacer = function (key, val) {
|
|
438
|
-
return isFunction (val) ? val.toString() : val;
|
|
439
|
-
};
|
|
440
|
-
|
|
441
|
-
/**
|
|
442
|
-
* Coerce documents with prototypical `_id` and `_rev`
|
|
443
|
-
* values.
|
|
444
|
-
*
|
|
445
|
-
* @param {Object} doc The document to coerce.
|
|
446
|
-
* @return {Object} The coerced document.
|
|
447
|
-
* @private
|
|
448
|
-
*/
|
|
449
|
-
|
|
450
|
-
Base.prototype._meta = function (doc) {
|
|
451
|
-
var hasId = !doc._id && doc.id;
|
|
452
|
-
var hasRev = !doc._rev && doc.rev;
|
|
453
|
-
var proto;
|
|
454
|
-
|
|
455
|
-
if (hasId || hasRev) {
|
|
456
|
-
proto = function (){};
|
|
457
|
-
doc = extend(new proto(), doc);
|
|
458
|
-
proto = proto.prototype;
|
|
459
|
-
if (hasId) proto._id = doc.id;
|
|
460
|
-
if (hasRev) proto._rev = doc.rev;
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
return doc;
|
|
464
|
-
};
|
|
465
|
-
|
|
466
|
-
/**
|
|
467
|
-
* Parse arguments.
|
|
468
|
-
*
|
|
469
|
-
* @param {Array} args The arguments.
|
|
470
|
-
* @param {Integer} [start] The index from which to start reading arguments.
|
|
471
|
-
* @param {Boolean} [withBody] Set to `true` if the request body is given as a
|
|
472
|
-
* parameter before HTTP query options.
|
|
473
|
-
* @param {Boolean} [notDoc] The request body is not a document.
|
|
474
|
-
* @return {Promise} A Promise, if no callback is provided, otherwise `null`.
|
|
475
|
-
* @private
|
|
476
|
-
*/
|
|
477
|
-
|
|
478
|
-
Base.prototype._ = function (args, start, withBody, notDoc) {
|
|
479
|
-
var self = this, doc, id, rev;
|
|
480
|
-
|
|
481
|
-
function request(method, path, options) {
|
|
482
|
-
if (!options) options = {};
|
|
483
|
-
return self._request({
|
|
484
|
-
method: method,
|
|
485
|
-
path: path || request.p,
|
|
486
|
-
query: options.q || request.q,
|
|
487
|
-
data: options.b || request.b,
|
|
488
|
-
headers: options.h || request.h,
|
|
489
|
-
fn: options.f || request.f,
|
|
490
|
-
_: options._ || request._
|
|
491
|
-
});
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
// [id], [doc], [query], [header], [callback]
|
|
495
|
-
args = [].slice.call(args, start || 0);
|
|
496
|
-
|
|
497
|
-
request.f = isFunction(args[args.length - 1]) && args.pop();
|
|
498
|
-
request.p = isString(args[0]) && encodeURI(args.shift());
|
|
499
|
-
request.q = args[withBody ? 1 : 0] || {};
|
|
500
|
-
request.h = args[withBody ? 2 : 1] || {};
|
|
501
|
-
|
|
502
|
-
if (withBody) {
|
|
503
|
-
doc = request.b = args[0];
|
|
504
|
-
if (!notDoc) {
|
|
505
|
-
if (id = request.p || doc._id || doc.id) request.p = id;
|
|
506
|
-
if (rev = request.q.rev || doc._rev || doc.rev) request.q.rev = rev;
|
|
507
|
-
}
|
|
508
|
-
}
|
|
509
|
-
|
|
510
|
-
return request;
|
|
511
|
-
};
|
|
512
|
-
|
|
513
|
-
/**
|
|
514
|
-
* Clerk CouchDB client.
|
|
515
|
-
*
|
|
516
|
-
* @param {String} uri Fully qualified URI.
|
|
517
|
-
* @param {String} [auth] Authentication header value.
|
|
518
|
-
* @constructor
|
|
519
|
-
* @memberof clerk
|
|
520
|
-
* @see {@link http://wiki.apache.org/couchdb/Complete_HTTP_API_Reference|CouchDB Wiki}
|
|
521
|
-
*/
|
|
522
|
-
|
|
523
|
-
function Client (uri, auth) {
|
|
524
|
-
this.uri = uri;
|
|
525
|
-
this._db = {};
|
|
526
|
-
this.auth = auth;
|
|
527
|
-
};
|
|
528
|
-
|
|
529
|
-
Client.prototype = new Base();
|
|
530
|
-
|
|
531
|
-
/**
|
|
532
|
-
* Select database to manipulate.
|
|
533
|
-
*
|
|
534
|
-
* @param {String} name DB name.
|
|
535
|
-
* @return {DB} DB object.
|
|
536
|
-
*/
|
|
537
|
-
|
|
538
|
-
Client.prototype.db = function (name) {
|
|
539
|
-
var db = this._db;
|
|
540
|
-
return db[name] || (db[name] = new DB(this, name, this.auth));
|
|
541
|
-
};
|
|
542
|
-
|
|
543
|
-
/**
|
|
544
|
-
* List all databases.
|
|
545
|
-
*
|
|
546
|
-
* @param {Object} [query] HTTP query options.
|
|
547
|
-
* @param {Object} [headers] HTTP headers.
|
|
548
|
-
* @param {handler} [callback] Callback function.
|
|
549
|
-
* @return {Promise} A Promise, if no callback is provided,
|
|
550
|
-
* otherwise `null`.
|
|
551
|
-
* @see {@link http://wiki.apache.org/couchdb/HttpGetAllDbs|CouchDB Wiki}
|
|
552
|
-
*/
|
|
553
|
-
|
|
554
|
-
Client.prototype.dbs = function (/* [query], [headers], [callback] */) {
|
|
555
|
-
return this._(arguments)("GET", "_all_dbs");
|
|
556
|
-
};
|
|
557
|
-
|
|
558
|
-
/**
|
|
559
|
-
* Get UUIDs.
|
|
560
|
-
*
|
|
561
|
-
* @param {Integer} [count=1] Number of UUIDs to get.
|
|
562
|
-
* @param {Object} [query] HTTP query options.
|
|
563
|
-
* @param {Object} [headers] HTTP headers.
|
|
564
|
-
* @param {handler} [callback] Callback function.
|
|
565
|
-
* @return {Promise} A Promise, if no callback is provided,
|
|
566
|
-
* otherwise `null`.
|
|
567
|
-
* @see {@link http://wiki.apache.org/couchdb/HttpGetUuids|CouchDB Wiki}
|
|
568
|
-
*/
|
|
569
|
-
|
|
570
|
-
Client.prototype.uuids = function (count /* [query], [headers], [callback] */) {
|
|
571
|
-
var request = this._(arguments, +count == count ? 1 : 0);
|
|
572
|
-
if (count > 1) request.q.count = count;
|
|
573
|
-
return request("GET", "_uuids");
|
|
574
|
-
};
|
|
575
|
-
|
|
576
|
-
/**
|
|
577
|
-
* Get server information.
|
|
578
|
-
*
|
|
579
|
-
* @param {Object} [query] HTTP query options.
|
|
580
|
-
* @param {Object} [headers] HTTP headers.
|
|
581
|
-
* @param {handler} [callback] Callback function.
|
|
582
|
-
* @return {Promise} A Promise, if no callback is provided,
|
|
583
|
-
* otherwise `null`.
|
|
584
|
-
* @see {@link http://wiki.apache.org/couchdb/HttpGetRoot|CouchDB Wiki}
|
|
585
|
-
*/
|
|
586
|
-
|
|
587
|
-
Client.prototype.info = function (/* [query], [headers], [callback] */) {
|
|
588
|
-
return this._(arguments)("GET");
|
|
589
|
-
};
|
|
590
|
-
|
|
591
|
-
/**
|
|
592
|
-
* Get server stats.
|
|
593
|
-
*
|
|
594
|
-
* @param {Object} [query] HTTP query options.
|
|
595
|
-
* @param {Object} [headers] HTTP headers.
|
|
596
|
-
* @param {handler} [callback] Callback function.
|
|
597
|
-
* @return {Promise} A Promise, if no callback is provided,
|
|
598
|
-
* otherwise `null`.
|
|
599
|
-
* @see {@link http://wiki.apache.org/couchdb/HttpGetLog|CouchDB Wiki}
|
|
600
|
-
*/
|
|
601
|
-
|
|
602
|
-
Client.prototype.stats = function (/* [query], [headers], [callback] */) {
|
|
603
|
-
return this._(arguments)("GET", "_stats");
|
|
604
|
-
};
|
|
605
|
-
|
|
606
|
-
/**
|
|
607
|
-
* Get tail of the server log file.
|
|
608
|
-
*
|
|
609
|
-
* @param {Object} [query] Query parameters.
|
|
610
|
-
* @param {Integer} [query.bytes] Number of bytes to read.
|
|
611
|
-
* @param {Integer} [query.offset] Number of bytes from the end of
|
|
612
|
-
* log file to start reading.
|
|
613
|
-
* @param {Object} [headers] HTTP headers.
|
|
614
|
-
* @param {handler} [callback] Callback function.
|
|
615
|
-
* @return {Promise} A Promise, if no callback is provided,
|
|
616
|
-
* otherwise `null`.
|
|
617
|
-
* @see {@link http://wiki.apache.org/couchdb/HttpGetLog|CouchDB Wiki}
|
|
618
|
-
*/
|
|
619
|
-
|
|
620
|
-
Client.prototype.log = function (/* [query], [headers], [callback] */) {
|
|
621
|
-
return this._(arguments)("GET", "_log");
|
|
622
|
-
};
|
|
623
|
-
|
|
624
|
-
/**
|
|
625
|
-
* List running tasks.
|
|
626
|
-
*
|
|
627
|
-
* @param {Object} [query] HTTP query options.
|
|
628
|
-
* @param {Object} [headers] HTTP headers.
|
|
629
|
-
* @param {handler} [callback] Callback function.
|
|
630
|
-
* @return {Promise} A Promise, if no callback is provided,
|
|
631
|
-
* otherwise `null`.
|
|
632
|
-
* @see {@link http://wiki.apache.org/couchdb/HttpGetActiveTasks|CouchDB Wiki}
|
|
633
|
-
*/
|
|
634
|
-
|
|
635
|
-
Client.prototype.tasks = function (/* [query], [headers], [callback] */) {
|
|
636
|
-
return this._(arguments)("GET", "_active_tasks");
|
|
637
|
-
};
|
|
638
|
-
|
|
639
|
-
/**
|
|
640
|
-
* Get or set configuration values.
|
|
641
|
-
*
|
|
642
|
-
* @param {String} [key] Configuration section or key.
|
|
643
|
-
* @param {String} [value] Configuration value.
|
|
644
|
-
* @param {Object} [query] HTTP query options.
|
|
645
|
-
* @param {Object} [headers] HTTP headers.
|
|
646
|
-
* @param {handler} [callback] Callback function.
|
|
647
|
-
* @return {Promise} A Promise, if no callback is provided,
|
|
648
|
-
* otherwise `null`.
|
|
649
|
-
*/
|
|
650
|
-
|
|
651
|
-
Client.prototype.config = function (/* [key], [value], [query], [headers], [callback] */) {
|
|
652
|
-
var args = [].slice.call(arguments);
|
|
653
|
-
var key = isString(args[0]) && args.shift() || "";
|
|
654
|
-
var value = isString(args[0]) && args.shift();
|
|
655
|
-
var method = isString(value) ? "PUT" : "GET";
|
|
656
|
-
return this._(args)(method, "_config/" + key, { b: value });
|
|
657
|
-
};
|
|
658
|
-
|
|
659
|
-
/**
|
|
660
|
-
* Replicate databases.
|
|
661
|
-
*
|
|
662
|
-
* @param {Object} options Options.
|
|
663
|
-
* @param {String} options.source Source database URL or local name.
|
|
664
|
-
* @param {String} options.target Target database URL or local name.
|
|
665
|
-
* @param {Boolean} [options.cancel] Set to `true` to cancel replication.
|
|
666
|
-
* @param {Boolean} [options.continuous] Set to `true` for continuous
|
|
667
|
-
* replication.
|
|
668
|
-
* @param {Boolean} [options.create_target] Set to `true` to create the
|
|
669
|
-
* target database.
|
|
670
|
-
* @param {String} [options.filter] Filter name for filtered replication.
|
|
671
|
-
* Example: "mydesign/myfilter".
|
|
672
|
-
* @param {Object} [options.query] Query parameters for filter.
|
|
673
|
-
* @param {String[]} [options.doc_ids] Document IDs to replicate.
|
|
674
|
-
* @param {String} [options.proxy] Proxy through which to replicate.
|
|
675
|
-
* @param {Object} [query] HTTP query options.
|
|
676
|
-
* @param {Object} [headers] HTTP headers.
|
|
677
|
-
* @param {handler} [callback] Callback function.
|
|
678
|
-
* @return {Promise} A Promise, if no callback is provided,
|
|
679
|
-
* otherwise `null`.
|
|
680
|
-
* @see {@link http://wiki.apache.org/couchdb/Replication|CouchDB Wiki}
|
|
681
|
-
*/
|
|
682
|
-
|
|
683
|
-
Client.prototype.replicate = function (options /* [query], [headers], [callback] */) {
|
|
684
|
-
return this._(arguments, 1)("POST", "_replicate", { b: options });
|
|
685
|
-
};
|
|
686
|
-
|
|
687
|
-
/**
|
|
688
|
-
* Methods for CouchDB database.
|
|
689
|
-
*
|
|
690
|
-
* @param {Client} client Clerk client.
|
|
691
|
-
* @param {String} name DB name.
|
|
692
|
-
* @param {String} [auth] Authentication header value.
|
|
693
|
-
* @constructor
|
|
694
|
-
* @memberof clerk
|
|
695
|
-
* @return This object for chaining.
|
|
696
|
-
*/
|
|
697
|
-
|
|
698
|
-
function DB (client, name, auth) {
|
|
699
|
-
this.client = client;
|
|
700
|
-
this.name = name;
|
|
701
|
-
this.uri = client.uri + "/" + encodeURIComponent(name);
|
|
702
|
-
this.auth = auth;
|
|
703
|
-
};
|
|
704
|
-
|
|
705
|
-
DB.prototype = new Base();
|
|
706
|
-
|
|
707
|
-
/**
|
|
708
|
-
* Create database.
|
|
709
|
-
*
|
|
710
|
-
* @param {Object} [query] HTTP query options.
|
|
711
|
-
* @param {Object} [headers] HTTP headers.
|
|
712
|
-
* @param {handler} [callback] Callback function.
|
|
713
|
-
* @return {Promise} A Promise, if no callback is provided,
|
|
714
|
-
* otherwise `null`.
|
|
715
|
-
*/
|
|
716
|
-
|
|
717
|
-
DB.prototype.create = function (/* [query], [headers], [callback] */) {
|
|
718
|
-
return this._(arguments)("PUT");
|
|
719
|
-
};
|
|
720
|
-
|
|
721
|
-
/**
|
|
722
|
-
* Destroy database.
|
|
723
|
-
*
|
|
724
|
-
* @param {Object} [query] HTTP query options.
|
|
725
|
-
* @param {Object} [headers] HTTP headers.
|
|
726
|
-
* @param {handler} [callback] Callback function.
|
|
727
|
-
* @return {Promise} A Promise, if no callback is provided,
|
|
728
|
-
* otherwise `null`.
|
|
729
|
-
*/
|
|
730
|
-
|
|
731
|
-
DB.prototype.destroy = function (/* [query], [headers], [callback] */) {
|
|
732
|
-
return this._(arguments)("DELETE");
|
|
733
|
-
};
|
|
734
|
-
|
|
735
|
-
/**
|
|
736
|
-
* Get database info.
|
|
737
|
-
*
|
|
738
|
-
* @param {Object} [query] HTTP query options.
|
|
739
|
-
* @param {Object} [headers] HTTP headers.
|
|
740
|
-
* @param {handler} [callback] Callback function.
|
|
741
|
-
* @return {Promise} A Promise, if no callback is provided,
|
|
742
|
-
* otherwise `null`.
|
|
743
|
-
*/
|
|
744
|
-
|
|
745
|
-
DB.prototype.info = function (/* [query], [headers], callback */) {
|
|
746
|
-
return this._(arguments)("GET");
|
|
747
|
-
};
|
|
748
|
-
|
|
749
|
-
/**
|
|
750
|
-
* Check if database exists.
|
|
751
|
-
*
|
|
752
|
-
* @param {Object} [query] HTTP query options.
|
|
753
|
-
* @param {Object} [headers] HTTP headers.
|
|
754
|
-
* @param {handler} [callback] Callback function.
|
|
755
|
-
* @return {Promise} A Promise, if no callback is provided,
|
|
756
|
-
* otherwise `null`.
|
|
757
|
-
*/
|
|
758
|
-
|
|
759
|
-
DB.prototype.exists = function (/* [query], [headers], callback */) {
|
|
760
|
-
var request = this._(arguments);
|
|
761
|
-
request._ = function (err, body, status, headers, req) {
|
|
762
|
-
return [err, status === 200, status, headers, req];
|
|
763
|
-
};
|
|
764
|
-
return request("HEAD");
|
|
765
|
-
};
|
|
766
|
-
|
|
767
|
-
/**
|
|
768
|
-
* Fetch document.
|
|
769
|
-
*
|
|
770
|
-
* Set `rev` in `query`.
|
|
771
|
-
*
|
|
772
|
-
* @param {String} id Document ID.
|
|
773
|
-
* @param {Object} [query] HTTP query options.
|
|
774
|
-
* @param {Boolean} [query.revs] Fetch list of revisions.
|
|
775
|
-
* @param {Boolean} [query.revs_info] Fetch detailed revision information.
|
|
776
|
-
* @param {Object} [headers] HTTP headers.
|
|
777
|
-
* @param {handler} [callback] Callback function.
|
|
778
|
-
* @return {Promise} A Promise, if no callback is provided,
|
|
779
|
-
* otherwise `null`.
|
|
780
|
-
* @see {@link http://wiki.apache.org/couchdb/HTTP_Document_API#GET|CouchDB Wiki}
|
|
781
|
-
*/
|
|
782
|
-
|
|
783
|
-
DB.prototype.get = function (/* [id], [query], [headers], [callback] */) {
|
|
784
|
-
return this._(arguments)("GET");
|
|
785
|
-
};
|
|
786
|
-
|
|
787
|
-
/**
|
|
788
|
-
* Get document metadata.
|
|
789
|
-
*
|
|
790
|
-
* @param {String} id Document ID.
|
|
791
|
-
* @param {Object} [query] HTTP query options.
|
|
792
|
-
* @param {Object} [headers] HTTP headers.
|
|
793
|
-
* @param {handler} [callback] Callback function.
|
|
794
|
-
* @return {Promise} A Promise, if no callback is provided,
|
|
795
|
-
* otherwise `null`.
|
|
796
|
-
* @see {@link http://wiki.apache.org/couchdb/HTTP_Document_API#HEAD|CouchDB Wiki}
|
|
797
|
-
*/
|
|
798
|
-
|
|
799
|
-
DB.prototype.head = function (/* [id], [query], [headers], callback */) {
|
|
800
|
-
var self = this;
|
|
801
|
-
var request = self._(arguments);
|
|
802
|
-
request._ = function (err, body, status, headers, res) {
|
|
803
|
-
return [err, err ? null : {
|
|
804
|
-
_id: request.p,
|
|
805
|
-
_rev: headers.etag && JSON.parse(headers.etag),
|
|
806
|
-
contentType: headers["content-type"],
|
|
807
|
-
contentLength: headers["content-length"]
|
|
808
|
-
}, status, headers, res];
|
|
809
|
-
};
|
|
810
|
-
return request("HEAD");
|
|
811
|
-
};
|
|
812
|
-
|
|
813
|
-
/**
|
|
814
|
-
* Post document(s) to database.
|
|
815
|
-
*
|
|
816
|
-
* If documents have no ID, a document ID will be automatically generated
|
|
817
|
-
* on the server. Attachments are not currently supported.
|
|
818
|
-
*
|
|
819
|
-
* @param {Object|Object[]} doc Document or array of documents.
|
|
820
|
-
* @param {String} [doc._id] Document ID. If set, uses given document ID.
|
|
821
|
-
* @param {String} [doc._rev] Document revision. If set, allows update to
|
|
822
|
-
* existing document.
|
|
823
|
-
* @param {Object} [doc._attachments] Attachments. If given, must be a
|
|
824
|
-
* map of filenames to attachment properties.
|
|
825
|
-
* @param {String} [doc._attachments[filename]] Attachment filename, as
|
|
826
|
-
* hash key.
|
|
827
|
-
* @param {String} [doc._attachments[filename].contentType] Attachment
|
|
828
|
-
* MIME content type.
|
|
829
|
-
* @param {String|Object} [doc._attachments[filename].data] Attachment
|
|
830
|
-
* data. Will be Base64 encoded.
|
|
831
|
-
* @param {Object} [query] HTTP query options.
|
|
832
|
-
* @param {Boolean} [query.batch] Allow server to write document in
|
|
833
|
-
* batch mode. Documents will not be written to disk immediately,
|
|
834
|
-
* increasing the chances of write failure.
|
|
835
|
-
* @param {Boolean} [query.all_or_nothing] For batch updating of
|
|
836
|
-
* documents, use all-or-nothing semantics.
|
|
837
|
-
* @param {Object} [headers] HTTP headers.
|
|
838
|
-
* @param {handler} [callback] Callback function.
|
|
839
|
-
* @return {Promise} A Promise, if no callback is provided,
|
|
840
|
-
* otherwise `null`.
|
|
841
|
-
* @see {@link http://wiki.apache.org/couchdb/HTTP_Document_API#POST|CouchDB Wiki}
|
|
842
|
-
* @see {@link http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API|CouchDB Wiki}
|
|
843
|
-
*/
|
|
844
|
-
|
|
845
|
-
DB.prototype.post = function (docs /* [query], [headers], [callback] */) {
|
|
846
|
-
var request = this._(arguments, 1);
|
|
847
|
-
if (isArray(docs)) {
|
|
848
|
-
request.p = "_bulk_docs";
|
|
849
|
-
request.b = extend({ docs: docs }, request.q);
|
|
850
|
-
request.q = null
|
|
851
|
-
} else {
|
|
852
|
-
request.b = docs;
|
|
853
|
-
}
|
|
854
|
-
return request("POST");
|
|
855
|
-
};
|
|
856
|
-
|
|
857
|
-
/**
|
|
858
|
-
* Put document in database.
|
|
859
|
-
*
|
|
860
|
-
* @param {Object} doc Document data. Requires `_id` and `_rev`.
|
|
861
|
-
* @param {String} [options] Options.
|
|
862
|
-
* @param {Object} [query] HTTP query options.
|
|
863
|
-
* @param {Object} [headers] HTTP headers.
|
|
864
|
-
* @param {handler} [callback] Callback function.
|
|
865
|
-
* @return {Promise} A Promise, if no callback is provided,
|
|
866
|
-
* otherwise `null`.
|
|
867
|
-
* @see {@link http://wiki.apache.org/couchdb/HTTP_Document_API#PUT|CouchDB Wiki}
|
|
868
|
-
*/
|
|
869
|
-
|
|
870
|
-
DB.prototype.put = function (/* [id], [doc], [query], [headers], [callback] */) {
|
|
871
|
-
var request = this._(arguments, 0, 1);
|
|
872
|
-
// prevent acidentally creating database
|
|
873
|
-
if (!request.p) request.p = request.b._id || request.b.id;
|
|
874
|
-
if (!request.p) throw new Error("missing id");
|
|
875
|
-
return request("PUT");
|
|
876
|
-
};
|
|
877
|
-
|
|
878
|
-
/**
|
|
879
|
-
* Delete document(s).
|
|
880
|
-
*
|
|
881
|
-
* @param {Object|Object[]} docs Document or array of documents.
|
|
882
|
-
* @param {Object} [query] HTTP query options.
|
|
883
|
-
* @param {Object} [headers] HTTP headers.
|
|
884
|
-
* @param {handler} [callback] Callback function.
|
|
885
|
-
* @return {Promise} A Promise, if no callback is provided,
|
|
886
|
-
* otherwise `null`.
|
|
887
|
-
* @see {@link http://wiki.apache.org/couchdb/HTTP_Document_API#DELETE|CouchDB Wiki}
|
|
888
|
-
* @see {@link http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API|CouchDB Wiki}
|
|
889
|
-
*/
|
|
890
|
-
|
|
891
|
-
DB.prototype.del = function (docs /* [query], [headers], [callback] */) {
|
|
892
|
-
if (isArray(docs)) {
|
|
893
|
-
var i = 0, len = docs.length, doc;
|
|
894
|
-
for (; i < len; i++) {
|
|
895
|
-
doc = docs[i], docs[i] = {
|
|
896
|
-
_id: doc._id || doc.id,
|
|
897
|
-
_rev: doc._rev || doc.rev,
|
|
898
|
-
_deleted: true
|
|
899
|
-
};
|
|
900
|
-
}
|
|
901
|
-
return this.post.apply(this, arguments);
|
|
902
|
-
} else {
|
|
903
|
-
var request = this._(arguments, 0, 1);
|
|
904
|
-
// prevent acidentally deleting database
|
|
905
|
-
if (!request.p) throw new Error("missing id");
|
|
906
|
-
return request("DELETE");
|
|
907
|
-
}
|
|
908
|
-
};
|
|
909
|
-
|
|
910
|
-
/**
|
|
911
|
-
* Copy document.
|
|
912
|
-
*
|
|
913
|
-
* @param {Object} source Source document.
|
|
914
|
-
* @param {String} source.id Source document ID.
|
|
915
|
-
* @param {String} [source.rev] Source document revision.
|
|
916
|
-
* @param {String} [source._id] Source document ID. Alternate key for
|
|
917
|
-
* `source.id`.
|
|
918
|
-
* @param {String} [source._rev] Source document revision. Alternate key
|
|
919
|
-
* for `source.id`.
|
|
920
|
-
* @param {Object} target Target document.
|
|
921
|
-
* @param {String} target.id Target document ID.
|
|
922
|
-
* @param {String} [target.rev] Target document revision.
|
|
923
|
-
* @param {String} [target._id] Target document ID. Alternate key for
|
|
924
|
-
* `target.id`.
|
|
925
|
-
* @param {String} [target._rev] Target document revision. Alternate key
|
|
926
|
-
* for `target.id`.
|
|
927
|
-
* @param {Object} [query] HTTP query options.
|
|
928
|
-
* @param {Object} [headers] HTTP headers.
|
|
929
|
-
* @param {handler} [callback] Callback function.
|
|
930
|
-
* @return {Promise} A Promise, if no callback is provided,
|
|
931
|
-
* otherwise `null`.
|
|
932
|
-
* @see {@link http://wiki.apache.org/couchdb/HTTP_Document_API#COPY|CouchDB Wiki}
|
|
933
|
-
*/
|
|
934
|
-
|
|
935
|
-
DB.prototype.copy = function (source, target /* [query], [headers], [callback] */) {
|
|
936
|
-
var request = this._(arguments, 2);
|
|
937
|
-
var sourcePath = encodeURIComponent(source.id || source._id || source);
|
|
938
|
-
var targetPath = encodeURIComponent(target.id || target._id || target);
|
|
939
|
-
var sourceRev = source.rev || source._rev;
|
|
940
|
-
var targetRev = target.rev || target._rev;
|
|
941
|
-
|
|
942
|
-
if (sourceRev) request.q.rev = sourceRev;
|
|
943
|
-
if (targetRev) targetPath += "?rev=" + encodeURIComponent(targetRev);
|
|
944
|
-
|
|
945
|
-
request.h.Destination = targetPath;
|
|
946
|
-
|
|
947
|
-
return request("COPY", sourcePath);
|
|
948
|
-
};
|
|
949
|
-
|
|
950
|
-
/**
|
|
951
|
-
* Query all documents by ID.
|
|
952
|
-
*
|
|
953
|
-
* @param {Object} [query] HTTP query options.
|
|
954
|
-
* @param {JSON} [query.startkey] Start returning results from this
|
|
955
|
-
* document ID.
|
|
956
|
-
* @param {JSON} [query.endkey] Stop returning results at this document
|
|
957
|
-
* ID.
|
|
958
|
-
* @param {Integer} [query.limit] Limit number of results returned.
|
|
959
|
-
* @param {Boolean} [query.descending=false] Lookup results in reverse
|
|
960
|
-
* order by key, returning documents in descending order by key.
|
|
961
|
-
* @param {Integer} [query.skip] Skip this many records before
|
|
962
|
-
* returning results.
|
|
963
|
-
* @param {Boolean} [query.include_docs=false] Include document source for
|
|
964
|
-
* each result.
|
|
965
|
-
* @param {Boolean} [query.include_end=true] Include `query.endkey`
|
|
966
|
-
* in results.
|
|
967
|
-
* @param {Boolean} [query.update_seq=false] Include sequence value
|
|
968
|
-
* of the database corresponding to the view.
|
|
969
|
-
* @param {Object} [headers] HTTP headers.
|
|
970
|
-
* @param {handler} [callback] Callback function.
|
|
971
|
-
* @return {Promise} A Promise, if no callback is provided,
|
|
972
|
-
* otherwise `null`.
|
|
973
|
-
* @see {@link http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API|CouchDB Wiki}
|
|
974
|
-
*/
|
|
975
|
-
|
|
976
|
-
DB.prototype.all = function (/* [query], [headers], [callback] */) {
|
|
977
|
-
var request = this._(arguments);
|
|
978
|
-
var body = this._viewOptions(request.q);
|
|
979
|
-
return request(body ? "POST" : "GET", "_all_docs", { b: body });
|
|
980
|
-
};
|
|
981
|
-
|
|
982
|
-
/**
|
|
983
|
-
* Query a view.
|
|
984
|
-
*
|
|
985
|
-
* @param {String|Object} view View name (e.g. mydesign/myview) or
|
|
986
|
-
* temporary view definition. Using a temporary view is strongly not
|
|
987
|
-
* recommended for production use.
|
|
988
|
-
* @param {Object} [query] HTTP query options.
|
|
989
|
-
* @param {JSON} [query.key] Key to lookup.
|
|
990
|
-
* @param {JSON} [query.startkey] Start returning results from this key.
|
|
991
|
-
* @param {String} [query.startkey_docid] Start returning results
|
|
992
|
-
* from this document ID. Allows pagination with duplicate keys.
|
|
993
|
-
* @param {JSON} [query.endkey] Stop returning results at this key.
|
|
994
|
-
* @param {String} [query.endkey_docid] Stop returning results at
|
|
995
|
-
* this document ID. Allows pagination with duplicate keys.
|
|
996
|
-
* @param {Integer} [query.limit] Limit number of results returned.
|
|
997
|
-
* @param {Boolean|String} [query.stale] Do not refresh view even if
|
|
998
|
-
* stale. For CouchDB versions `1.1.0` and up, set to `update_after` to
|
|
999
|
-
* update view after results are returned.
|
|
1000
|
-
* @param {Boolean} [query.descending=false] Lookup results in reverse
|
|
1001
|
-
* order by key, returning documents in descending order by key.
|
|
1002
|
-
* @param {Integer} [query.skip] Skip this many records before
|
|
1003
|
-
* returning results.
|
|
1004
|
-
* @param {Boolean|Integer} [query.group=false] Use the reduce function
|
|
1005
|
-
* to group results by key. Set to an integer specify `group_level`.
|
|
1006
|
-
* @param {Boolean|Integer} [query.reduce=true] Use the reduce function.
|
|
1007
|
-
* @param {Boolean} [query.fetch=false] Include document source for
|
|
1008
|
-
* each result.
|
|
1009
|
-
* @param {Boolean} [query.include_end=true] Include `query.endkey`
|
|
1010
|
-
* in results.
|
|
1011
|
-
* @param {Boolean} [query.update_seq=false] Include sequence value
|
|
1012
|
-
* of the database corresponding to the view.
|
|
1013
|
-
* @param {Object} [headers] HTTP headers.
|
|
1014
|
-
* @param {handler} [callback] Callback function.
|
|
1015
|
-
* @return {Promise} A Promise, if no callback is provided,
|
|
1016
|
-
* otherwise `null`.
|
|
1017
|
-
* @see {@link http://wiki.apache.org/couchdb/HTTP_view_API|CouchDB Wiki}
|
|
1018
|
-
*/
|
|
1019
|
-
|
|
1020
|
-
DB.prototype.find = function (view /* [query], [headers], [callback] */) {
|
|
1021
|
-
var request = this._(arguments, 1), path, body;
|
|
1022
|
-
|
|
1023
|
-
if (isString(view)) {
|
|
1024
|
-
path = view.split("/", 2);
|
|
1025
|
-
path = "_design/" + encodeURIComponent(path[0]) +
|
|
1026
|
-
"/_view/" + encodeURIComponent(path[1]);
|
|
1027
|
-
} else {
|
|
1028
|
-
path = "_temp_view";
|
|
1029
|
-
body = view;
|
|
1030
|
-
}
|
|
1031
|
-
|
|
1032
|
-
body = this._viewOptions(request.q, body);
|
|
1033
|
-
return request(body ? "POST" : "GET", path, { b: body });
|
|
1034
|
-
};
|
|
1035
|
-
|
|
1036
|
-
/**
|
|
1037
|
-
* Get database changes.
|
|
1038
|
-
*
|
|
1039
|
-
* The `feed` option determines how the callback is called:
|
|
1040
|
-
*
|
|
1041
|
-
* - `normal` calls the callback once.
|
|
1042
|
-
* - `longpoll` waits for a response, then calls the callback once.
|
|
1043
|
-
* - `continuous` calls the callback each time an update is received.
|
|
1044
|
-
* Implemented as the `database#follow()` method.
|
|
1045
|
-
*
|
|
1046
|
-
* @param {Object} [query] HTTP query options.
|
|
1047
|
-
* @param {String} [query.feed="normal"] Type of feed. See comments
|
|
1048
|
-
* above.
|
|
1049
|
-
* @param {String} [query.filter] Filter updates using this filter.
|
|
1050
|
-
* @param {Integer} [query.limit] Maximum number of rows to return.
|
|
1051
|
-
* @param {Integer} [query.since=0] Start results from this sequence
|
|
1052
|
-
* number.
|
|
1053
|
-
* @param {Boolean} [query.include_docs=false] Include documents with
|
|
1054
|
-
* results.
|
|
1055
|
-
* @param {Integer} [query.timeout=1000] Maximum period in milliseconds
|
|
1056
|
-
* to wait for a change before sending a response, even if there are no
|
|
1057
|
-
* results.
|
|
1058
|
-
* @param {Integer} [query.heartbeat=1000] Period in milliseconds after
|
|
1059
|
-
* which an empty line is sent. Applicable only to feed types
|
|
1060
|
-
* `longpoll` and `continuous`. Overrides `query.timeout` to keep the
|
|
1061
|
-
* feed alive indefinitely.
|
|
1062
|
-
* @param {Object} [headers] HTTP headers.
|
|
1063
|
-
* @param {handler} [callback] Callback function.
|
|
1064
|
-
* @return {Promise} A Promise, if no callback is provided,
|
|
1065
|
-
* otherwise `null`.
|
|
1066
|
-
* @see {@link http://wiki.apache.org/couchdb/HTTP_database_API#Changes|CouchDB Wiki}
|
|
1067
|
-
*/
|
|
1068
|
-
|
|
1069
|
-
DB.prototype.changes = function (/* [query], [headers], [callback] */) {
|
|
1070
|
-
var request = this._(arguments);
|
|
1071
|
-
if (request.q.feed != "longpoll") delete request.q.feed;
|
|
1072
|
-
return this._changes(request);
|
|
1073
|
-
};
|
|
1074
|
-
|
|
1075
|
-
/**
|
|
1076
|
-
* Follow database changes.
|
|
1077
|
-
*
|
|
1078
|
-
* @see `#changes()`.
|
|
1079
|
-
*/
|
|
1080
|
-
|
|
1081
|
-
DB.prototype.follow = function (/* [query], [headers], callback */) {
|
|
1082
|
-
var self = this;
|
|
1083
|
-
var request = this._(arguments);
|
|
1084
|
-
var fn = request.f;
|
|
1085
|
-
|
|
1086
|
-
if (!fn) return this;
|
|
1087
|
-
|
|
1088
|
-
request.q.feed = "longpoll";
|
|
1089
|
-
request.f = function (err, body) {
|
|
1090
|
-
var args = [].slice.call(arguments);
|
|
1091
|
-
var done, i;
|
|
1092
|
-
for (i = 0; i < body.length; i++) {
|
|
1093
|
-
args[1] = body[i];
|
|
1094
|
-
if (done = fn.apply(self, args) === false || err) break;
|
|
1095
|
-
}
|
|
1096
|
-
if (!done) self._changes(request);
|
|
1097
|
-
};
|
|
1098
|
-
|
|
1099
|
-
return this._changes(request);
|
|
1100
|
-
};
|
|
1101
|
-
|
|
1102
|
-
/**
|
|
1103
|
-
* Service a changes request.
|
|
1104
|
-
*
|
|
1105
|
-
* @private
|
|
1106
|
-
*/
|
|
1107
|
-
|
|
1108
|
-
DB.prototype._changes = function (request) {
|
|
1109
|
-
return request("GET", "_changes");
|
|
1110
|
-
};
|
|
1111
|
-
|
|
1112
|
-
/**
|
|
1113
|
-
* Update document using server-side handler.
|
|
1114
|
-
*
|
|
1115
|
-
* @param {String} handler Update handler. Example: mydesign/myhandler
|
|
1116
|
-
* @param {String} [id] Document ID.
|
|
1117
|
-
* @param {any} data Data.
|
|
1118
|
-
* @param {Object} [query] HTTP query options.
|
|
1119
|
-
* @param {Object} [headers] Headers.
|
|
1120
|
-
* @param {handler} [callback] Callback function.
|
|
1121
|
-
* @return {Promise} A Promise, if no callback is provided,
|
|
1122
|
-
* otherwise `null`.
|
|
1123
|
-
* @see {@link http://wiki.apache.org/couchdb/Document_Update_Handlers|CouchDB Wiki}
|
|
1124
|
-
*/
|
|
1125
|
-
|
|
1126
|
-
DB.prototype.update = function (handler /* [id], [data], [query], [headers], [callback] */) {
|
|
1127
|
-
var request = this._(arguments, 1, 1, 1);
|
|
1128
|
-
var path = handler.split("/", 2);
|
|
1129
|
-
|
|
1130
|
-
path = "_design/" + encodeURIComponent(path[0]) +
|
|
1131
|
-
"/_update/" + encodeURIComponent(path[1]);
|
|
1132
|
-
|
|
1133
|
-
if (request.p) path += "/" + request.p;
|
|
1134
|
-
|
|
1135
|
-
return request("POST", path);
|
|
1136
|
-
};
|
|
1137
|
-
|
|
1138
|
-
/**
|
|
1139
|
-
* Download attachment from document.
|
|
1140
|
-
*
|
|
1141
|
-
* @param {Object|String} docOrId Document or document ID.
|
|
1142
|
-
* @param {String} attachmentName Attachment name.
|
|
1143
|
-
* @param {Object} [query] HTTP query options.
|
|
1144
|
-
* @param {Object} [headers] HTTP headers.
|
|
1145
|
-
* @param {handler} [callback] Callback function.
|
|
1146
|
-
* @return {Promise} A Promise, if no callback is provided,
|
|
1147
|
-
* otherwise `null`.
|
|
1148
|
-
*/
|
|
1149
|
-
|
|
1150
|
-
DB.prototype.attachment = function (doc, attachmentName /* [query], [headers], [callback] */) {
|
|
1151
|
-
var request = this._(arguments, 2);
|
|
1152
|
-
var path = encodeURIComponent(doc._id || doc.id || doc) + "/" +
|
|
1153
|
-
encodeURIComponent(attachmentName);
|
|
1154
|
-
return request("GET", path, options);
|
|
1155
|
-
};
|
|
1156
|
-
|
|
1157
|
-
/**
|
|
1158
|
-
* Upload attachment to document.
|
|
1159
|
-
*
|
|
1160
|
-
* Set the `Content-Type` header.
|
|
1161
|
-
*
|
|
1162
|
-
* @param {Object} [doc] Document. Requires `id`. `rev` can be specified
|
|
1163
|
-
* here or in `query`.
|
|
1164
|
-
* @param {String} attachmentName Attachment name.
|
|
1165
|
-
* @param {Object} data Data.
|
|
1166
|
-
* @param {Object} [query] HTTP query options.
|
|
1167
|
-
* @param {Object} [headers] HTTP headers.
|
|
1168
|
-
* @param {handler} [callback] Callback function.
|
|
1169
|
-
* @return {Promise} A Promise, if no callback is provided,
|
|
1170
|
-
* otherwise `null`.
|
|
1171
|
-
*/
|
|
1172
|
-
|
|
1173
|
-
DB.prototype.attach = function (doc, attachmentName, data /* [query], [headers], [callback] */) {
|
|
1174
|
-
var request = this._(arguments, 3);
|
|
1175
|
-
request.p = encodeURIComponent(doc._id || doc.id) + "/" +
|
|
1176
|
-
encodeURIComponent(attachmentName);
|
|
1177
|
-
if (!request.q.rev) request.q.rev = doc._rev || doc.rev;
|
|
1178
|
-
request.q.body = data;
|
|
1179
|
-
return request("PUT", path);
|
|
1180
|
-
};
|
|
1181
|
-
|
|
1182
|
-
/**
|
|
1183
|
-
* Replicate database.
|
|
1184
|
-
*
|
|
1185
|
-
* This convenience function sets `options.source` and `options.target` to
|
|
1186
|
-
* the selected database name. Either `options.source` or `options.target`
|
|
1187
|
-
* must be overridden for a successful replication request.
|
|
1188
|
-
*
|
|
1189
|
-
* @param {Options} options Options. Accepts all options from
|
|
1190
|
-
* `Client.replicate()`.
|
|
1191
|
-
* @param {String} [options.source=this.name] Source database URL or
|
|
1192
|
-
* local name. Defaults to the selected database name if not given.
|
|
1193
|
-
* @param {String} [options.target=this.name] Target database URL or
|
|
1194
|
-
* local name. Defaults to the selected database name if not given.
|
|
1195
|
-
* @param {Object} [query] HTTP query options.
|
|
1196
|
-
* @param {Object} [headers] HTTP headers.
|
|
1197
|
-
* @param {handler} [callback] Callback function.
|
|
1198
|
-
* @return {Promise} A Promise, if no callback is provided,
|
|
1199
|
-
* otherwise `null`.
|
|
1200
|
-
*/
|
|
1201
|
-
|
|
1202
|
-
DB.prototype.replicate = function (options /* [query], [headers], [callback] */) {
|
|
1203
|
-
if (!options.source) options.source = this.name;
|
|
1204
|
-
if (!options.target) options.target = this.name;
|
|
1205
|
-
return this.client.replicate.apply(this.client, arguments);
|
|
1206
|
-
};
|
|
1207
|
-
|
|
1208
|
-
/**
|
|
1209
|
-
* Ensure recent changes are committed to disk.
|
|
1210
|
-
*
|
|
1211
|
-
* @param {Object} [query] HTTP query options.
|
|
1212
|
-
* @param {Object} [headers] HTTP headers.
|
|
1213
|
-
* @param {handler} [callback] Callback function.
|
|
1214
|
-
* @return {Promise} A Promise, if no callback is provided,
|
|
1215
|
-
* otherwise `null`.
|
|
1216
|
-
*/
|
|
1217
|
-
|
|
1218
|
-
DB.prototype.commit = function (/* [query], [headers], [callback] */) {
|
|
1219
|
-
return this._(arguments)("POST", "_ensure_full_commit");
|
|
1220
|
-
};
|
|
1221
|
-
|
|
1222
|
-
/**
|
|
1223
|
-
* Purge deleted documents from database.
|
|
1224
|
-
*
|
|
1225
|
-
* @param {Object} revs Map of document IDs to revisions to be purged.
|
|
1226
|
-
* @param {Object} [query] HTTP query options.
|
|
1227
|
-
* @param {Object} [headers] HTTP headers.
|
|
1228
|
-
* @param {handler} [callback] Callback function.
|
|
1229
|
-
* @return {Promise} A Promise, if no callback is provided,
|
|
1230
|
-
* otherwise `null`.
|
|
1231
|
-
*/
|
|
1232
|
-
|
|
1233
|
-
DB.prototype.purge = function (revs /* [query], [headers], [callback] */) {
|
|
1234
|
-
return this._(arguments, 1)("POST", "_purge", { b: revs });
|
|
1235
|
-
};
|
|
1236
|
-
|
|
1237
|
-
/**
|
|
1238
|
-
* Compact database or design.
|
|
1239
|
-
*
|
|
1240
|
-
* @param {String} [design] Design name if compacting design indexes.
|
|
1241
|
-
* @param {Object} [query] HTTP query options.
|
|
1242
|
-
* @param {Object} [headers] HTTP headers.
|
|
1243
|
-
* @param {handler} [callback] Callback function.
|
|
1244
|
-
* @return {Promise} A Promise, if no callback is provided,
|
|
1245
|
-
* otherwise `null`.
|
|
1246
|
-
* @see {@link http://wiki.apache.org/couchdb/Compaction|CouchDB Wiki}
|
|
1247
|
-
*/
|
|
1248
|
-
|
|
1249
|
-
DB.prototype.compact = function (/* [design], [query], [headers], [callback] */) {
|
|
1250
|
-
var request = this._(arguments);
|
|
1251
|
-
return request("POST", "_compact/" + (request.p || ""));
|
|
1252
|
-
};
|
|
1253
|
-
|
|
1254
|
-
/**
|
|
1255
|
-
* Remove unused views.
|
|
1256
|
-
*
|
|
1257
|
-
* @param {Object} [query] HTTP query options.
|
|
1258
|
-
* @param {Object} [headers] HTTP headers.
|
|
1259
|
-
* @param {handler} [callback] Callback function.
|
|
1260
|
-
* @return {Promise} A Promise, if no callback is provided,
|
|
1261
|
-
* otherwise `null`.
|
|
1262
|
-
* @see {@link http://wiki.apache.org/couchdb/Compaction|CouchDB Wiki}
|
|
1263
|
-
*/
|
|
1264
|
-
|
|
1265
|
-
DB.prototype.vacuum = function (/* [query], [headers], [callback] */) {
|
|
1266
|
-
return this._(arguments)("POST", "_view_cleanup");
|
|
1267
|
-
};
|
|
1268
|
-
|
|
1269
|
-
/**
|
|
1270
|
-
* Parse view options.
|
|
1271
|
-
*
|
|
1272
|
-
* @param {Object} query The HTTP query options.
|
|
1273
|
-
* @param {Object} body The body payload.
|
|
1274
|
-
* @param {handler} [callback] Callback function.
|
|
1275
|
-
* @return {Object} The body payload.
|
|
1276
|
-
* @private
|
|
1277
|
-
*/
|
|
1278
|
-
|
|
1279
|
-
DB.prototype._viewOptions = function (q, body) {
|
|
1280
|
-
if (q) {
|
|
1281
|
-
if (q.key) q.key = JSON.stringify(q.key);
|
|
1282
|
-
if (q.startkey) q.startkey = JSON.stringify(q.startkey);
|
|
1283
|
-
if (q.endkey) q.endkey = JSON.stringify(q.endkey);
|
|
1284
|
-
if (q.stale && q.stale != "update_after") q.stale = "ok";
|
|
1285
|
-
if (q.keys) {
|
|
1286
|
-
if (!body) body = {};
|
|
1287
|
-
body.keys = q.keys;
|
|
1288
|
-
delete q.keys;
|
|
1289
|
-
}
|
|
1290
|
-
}
|
|
1291
|
-
return body;
|
|
1292
|
-
};
|
|
1293
|
-
|
|
1294
|
-
/**
|
|
1295
|
-
* Handle a clerk response.
|
|
1296
|
-
*
|
|
1297
|
-
* @callback handler
|
|
1298
|
-
* @param {Error|null} error Error or `null` on success.
|
|
1299
|
-
* @param {Object} data Response data.
|
|
1300
|
-
* @param {Integer} status Response status code.
|
|
1301
|
-
* @param {Object} headers Response headers.
|
|
1302
|
-
* @param {superagent.Response} res Superagent response object.
|
|
1303
|
-
*/
|
|
1304
|
-
|
|
1305
|
-
clerk.Base = Base;
|
|
1306
|
-
clerk.Client = Client;
|
|
1307
|
-
clerk.DB = DB;
|
|
1308
|
-
|
|
1309
|
-
// Export clerk.
|
|
1310
|
-
module.exports = clerk;
|