-
Notifications
You must be signed in to change notification settings - Fork 2
/
supercouch.js
900 lines (789 loc) · 22.2 KB
/
supercouch.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
var supercouch = function (req) {
var module = {};
/*!
* SuperCouch
* Copyright (c) 2012 Jake Luer <jake@qualiancy.com>
* MIT Licensed
*/
/*!
* Context loader
*/
module.exports = function (agent) {
/*!
* Main Export (factory)
*/
var exports = function (address) {
return new Couch(address);
};
/*!
* SupeCouch version
*/
exports.version = '0.2.4';
/*!
* toString utility
*
* Provided as variable for easier browser minification.
*
* @param {Object} object to test
* @api private
*/
var toString = Object.prototype.toString;
/*!
* isFn utility
*
* @param {Function} fn
* @api private
*/
function isFn (fn) {
return '[object Function]' === toString.call(fn);
}
/*!
* isObj utility
*
* @param {Object} fn
* @api private
*/
function isObj (obj) {
return obj === Object(obj);
}
/*!
* isArray utility
*
* @param {Array} fn
* @api private
*/
var isArray = Array.isArray || function (arr) {
return '[object Array]' === toString.call(arr);
};
/*!
* indexOf utility
*
* IE8 doesn't have indexOf. Defaults to "native"
* Array.prototype.indexOf if it exists.
*
* @ctx {Array}
* @param {Mixed} item to look for
* @param {Number} start index
*/
var indexOf = Array.prototype.indexOf || function (obj, start) {
for (var i = (start || 0), j = this.length; i < j; i++) {
if (this[i] === obj) return i;
}
return -1;
}
/*!
* merge utility
*
* @param {Object} a
* @param {Object} b
* @api private
*/
function merge (a, b){
if (a && b) {
for (var key in b) {
a[key] = b[key];
}
}
return a;
}
/*!
* defaults utility
*
* @param {Object} a
* @param {Object} b
* @api private
*/
function defaults (a, b) {
if (a && b) {
for (var key in b) {
if ('undefined' == typeof a[key]) a[key] = b[key];
}
}
return a;
}
/**
* ## Request API
*
* Requests are the last part of any of the chainable
* methods. Providing API access to the Request API
* allows for a way to directly manipulate and initialize
* the request.
*
* Further API documentation will note when a method
* **returns a Request**. You can also initialize custom
* requests using the `.request` method of the Database API.
*
* @header Request API
*/
function Request (opts) {
/*!
* @param {Object} options
* @api public
*/
opts = opts || {};
this.reqOpts = opts;
}
/**
* ### .send (obj[, value])
*
* Modify key/values to the JSON body being sent
* during `PUT` and `POST` based operations. This
* is usually the case of `insert` or `update` commands
* from supercouch.
*
* var req = couch.request('post', '/mydb/abc');
*
* Can be used to update key/value pairs...
*
* req.send('hello', 'world');
*
* Or, as an object to be shallow-merged with the
* current parameters.
*
* req.send({ hello: 'universe', scope: 'ubiquitous' });
*
* @param {String|Object} string as key or object to merge
* @param {Mixed} value to use if previous was a key
* @api public
* @name send
*/
Request.prototype.send = function (opts, value) {
var body = this.reqOpts.body || (this.reqOpts.body = {});
if (isObj(opts)) {
this.reqOpts.body = merge(opts, body);
} else {
this.reqOpts.body[opts] = value;
}
return this;
};
/**
* ### .query (obj[, value])
*
* Modify key/values to the querystring being appended
* to the url for a request.
*
* var req = couch.request('get', '/mydb/_changes');
*
* Can be used to update key/value pairs...
*
* req.send('since', 123);
*
* Or, as an object to be shallow-merged with the
* current parameters.
*
* req.send({ since: 123, feed: 'continuous' });
*
* @param {String|Object} string as key or object to merge
* @param {Mixed} value to use if previous was a key
* @api public
* @name query
*/
Request.prototype.query = function (opts, value) {
var qs = this.reqOpts.qs || (this.reqOpts.qs = {});
if (isObj(opts)) {
this.reqOpts.qs = merge(opts, qs);
} else {
this.reqOpts.qs[opts] = value;
}
return this;
};
/**
* ### .end (callback);
*
* Interprets all the given parameters into a request,
* sends reqest to superagent, parses results, and sends
* appropriate values back to callback.
*
* If an error occurs on the CouchDB side of an operation,
* the results will be constructed into a `supercouch.CouchError`,
* which is an instance of a javascript `Error`.
*
* req.end(function (err, res) {
* if (err) throw err; // likely instanceof CouchError
* console.log(res); // json of CouchDB response
* });
*
* @param {Function} callback
* @api public
* @name end
*/
Request.prototype.end = function (cb) {
var self = this
, opts = this.reqOpts
, url = buildUrl.call(this)
, method = opts.method.toUpperCase()
, req;
if ('GET' === method) req = agent.get(url);
else if ('POST' === method) req = agent.post(url);
else if ('PUT' === method) req = agent.put(url);
else if ('DELETE' === method) req = agent.del(url);
else if ('HEAD' === method) req = agent.head(url);
else return cb(new Error('Unsuppored request method'));
// add in querystrings
if (opts.qs) req.query(opts.qs);
// convert map/reduce functions to strings
if (opts.body) {
var map = opts.body.map
, reduce = opts.body.reduce;
if (isFn(map)) opts.body.map = '' + map;
if (isFn(reduce)) opts.body.reduce = '' + reduce;
req.send(opts.body);
}
req.end(function makeRequest (res) {
var json
, resErr = null;
if (method === 'HEAD') {
var val = isFn(opts.validate)
? opts.validate(res)
: res.status !== 404;
return cb(null, val);
}
try { json = JSON.parse(res.text); }
catch (ex) { resErr = ex; }
if (!resErr && json.error) resErr = new CouchError(json);
if (resErr) return cb(resErr);
var val = isFn(opts.validate)
? opts.validate(json)
: json;
cb(null, val);
});
};
/*!
* buildUrl
*
* Takes all parameters in a constructed
* Request and builds the appropriate url.
*
* @context {Request}
* @api private
*/
function buildUrl () {
var opts = this.reqOpts
opts.path
.join('/')
.split('/')
.filter(function (part) {
return !!!part.trim.length;
});
var path = (opts.path.length === 1 && opts.path[0] == '/')
? ''
: opts.path.join('/');
return opts.base + '/' + path;
}
/**
* ## Database API
*
* After you have specified your CouchDB url, the
* next level in the chainable API is the `Database
* API`. It exposes methods to interact with
* server and database level events. Note that some
* methods will construct Requests, while others will
* construct the next level chainable objects.
*
* @header Database API
*/
function Couch (address, opts) {
/*!
* @param {String} address
* @param {Object} options
* @api private
*/
if ('object' === typeof address) {
opts = address;
address = 'http://localhost:5984';
}
opts = opts || {};
this.reqOpts = {
base: address || 'http://localhost:5984'
}
};
/**
* ### .request (method, url[, callback])
*
* Helper method to directly create Requests
* to the server. Useful for any urls that might
* not be included in the chainable api.
*
* Providing a callback will immediately execute
* the request. **Returns a Request**.
*
* @param {String} method
* @param {String} url
* @param {Function} callback (optional)
* @returns {Request} constructed request
* @api public
* @name request
*/
Couch.prototype.request = function (method, _url, fn) {
var opts = merge({
method: method
, path: [ _url ]
}, this.reqOpts);
var req = new Request(opts);
if (isFn(fn)) req.end(fn);
return req;
};
/**
* ### .action (name[, body[, callback]])
*
* Constructs a request that performs an CouchDb action
* on the server. If the name of the action is not valid
* an error will be thrown.
*
* couch
* .action('all dbs')
* .end(db);
*
* Proving a callback will immediately execute the
* request. **Returns a Request**.
*
* ##### Actions
*
* - `all dbs` - get a list of all databases
* - `active tasks` - get a list of currenly running tasks
* - `uuids` - get a list of all uuids generated on the server
* - `stats` - get all couch server statistics
* - `log` - get a tail of ther server's log file (requires admin priv)
* - `replicate` - post a replicate command to the couch server
* - `restart` - port a restart command to the server
*
* @param {String} command name
* @param {Object} body for post commands
* @param {Function} optional callback
* @api public
* @name action
* @see http://wiki.apache.org/couchdb/Complete_HTTP_API_Reference CouchDB API Guide
*/
Couch.prototype.action = function (name, body, fn) {
var actions = {
'all dbs' : 'GET'
, 'active tasks' : 'GET'
, 'uuids' : 'GET'
, 'stats' : 'GET'
, 'log' : 'GET'
, 'replicate' : 'POST'
, 'restart' : 'POST'
};
if (!actions[name])
throw new Error('Couch action `' + name + '` not valid');
if (isFn(body)) fn = body, data = {};
var method = actions[name]
, path = '_' + name.split(' ').join('_')
, opts = merge({
method: method
, path: [ path ]
, body: body
}, this.reqOpts);
var req = new Request(opts);
if (isFn(fn)) req.end(fn);
return req;
};
/**
* ### .dbAdd (name[, fn])
*
* Constructs a request that will add a database
* to the CouchDb server.
*
* couch
* .dbAdd('my_app')
* .end(cb);
*
* Providing a callback will immediately execute
* the request. **Returns a Request**.
*
* @param {String} db name
* @param {Function} callback (optional)
* @returns {Request} constructed request
* @api public
* @name dbAdd
*/
Couch.prototype.dbAdd = function (name, fn) {
var opts = merge({
method: 'PUT'
, path: [ name ]
}, this.reqOpts);
var req = new Request(opts);
if (isFn(fn)) req.end(fn);
return req;
};
/**
* ### .dbInfo (name[, fn])
*
* Constructs a request that will get the information
* about a database in the CouchDb server.
*
* couch
* .dbInfo('my_app')
* .end(cb);
*
* Providing a callback will immediately execute
* the request. **Returns a Request**.
*
* @param {String} db name
* @param {Function} callback (optional)
* @returns {Request} constructed request
* @api public
* @name dbInfo
*/
Couch.prototype.dbInfo = function (name, fn) {
var opts = merge({
method: 'GET'
, path: [ name ]
}, this.reqOpts);
var req = new Request(opts);
if (isFn(fn)) req.end(fn);
return req;
};
/**
* ### .dbExists (name[, fn])
*
* Constructs a request that will check if
* database exists.
*
* couch
* .dbExists('my_app')
* .end(cb);
*
* Providing a callback will immediately execute
* the request. **Returns a Request**.
*
* @param {String} db name
* @param {Function} callback (optional)
* @returns {Request} constructed request
* @api public
* @name dbExists
*/
Couch.prototype.dbExists = function (name, fn) {
var opts = merge({
method: 'GET'
, path: [ '_all_dbs' ]
, validate: function (res) {
return indexOf.call(res, name) > -1
}
}, this.reqOpts);
var req = new Request(opts);
if (isFn(fn)) req.end(fn);
return req;
};
/**
* ### .dbDel (name[, fn])
*
* Constructs a request that will remove a database
* to the CouchDb server.
*
* couch
* .dbDel('my_app')
* .end(cb);
*
* Providing a callback will immediately execute
* the request. **Returns a Request**.
*
* @param {String} db name
* @param {Function} callback (optional)
* @returns {Request} constructed request
* @api public
* @name dbDel
*/
Couch.prototype.dbDel = function (name, fn) {
var opts = merge({
method: 'DELETE'
, path: [ name ]
}, this.reqOpts);
var req = new Request(opts);
if (isFn(fn)) req.end(fn);
return req;
};
/**
* ### .db (name)
*
* Contructs a Db interface for chaining actions
* on a specific database. See the `Document API`.
*
* var mydb = couch.db('mydb');
*
* @param {String} db name
* @returns {Db} constructed db chain api
* @api public
* @name db
*/
Couch.prototype.db = function (name) {
var opts = merge({
path: [ name ]
}, this.reqOpts);
var db = new Db(opts);
return db;
};
/**
* ## Document API
*
* Provides chainable API for requests to a specific db.
* Constructed when `.db('name')` method is performed on
* a database instance.
*
* @header Document API
*/
function Db (opts) {
/*!
* @param {Object} current request options
* @api private
*/
opts = opts || {};
this.reqOpts = opts;
}
/**
* ### .action (name[, body[, callback]])
*
* Constructs a request that performs a CouchDb action
* on the currently selected database.
*
* If the name of the action is not valid an error will
* be thrown.
*
* Proving a callback will immediately execute the
* request to the server. **Returns a Request**.
*
* ##### Actions
*
* - `changes` - gets changes for the database
* - `compact` - post instructing the db to compact
* - `view cleanup` - post instructing the db to perform view cleanup
* - `temp view` - post instructing the db to execute view function (admin privileges)
* - `ensure full commit` - post instructing the db commit all changes to disk
* - `purge` - post instructing the db to purge history docs from db history
*
* Here is very simple `map` temporary view example.
*
* couch
* .db('mydb')
* .action('temp view')
* .send({
* map: function (doc) {
* emit(doc._id, doc);
* }
* })
* .end(cb);
*
* @param {String} command name
* @param {Object} body for post commands
* @param {Function} optional callback
* @api public
* @name action
* @see http://wiki.apache.org/couchdb/Complete_HTTP_API_Reference CouchDB API Guide
*/
Db.prototype.action = function (name, body, fn) {
var actions = {
'changes' : 'GET'
, 'compact' : 'POST'
, 'view cleanup' : 'POST'
, 'temp view' : 'POST'
, 'ensure full commit' : 'POST'
, 'purge' : 'POST'
};
if (!actions[name])
throw new Error('Couch action `' + name + '` not valid');
if (isFn(body)) fn = body, data = {};
var method = actions[name]
, path = '_' + name.split(' ').join('_')
, opts = merge({
method: method
, body: body
}, this.reqOpts);
opts.path.push(path);
var req = new Request(opts);
if (isFn(fn)) req.end(fn);
return req;
};
/**
* ### .insert (doc[, callback])
*
* Inserts a new document to the currently
* selected database.
*
* couch
* .db('my_app')
* .insert(docObj)
* .end(cb);
*
* Document is optional but should be provided
* using the requests `send` command in the chainable api.
*
* couch
* .db('my_app')
* .insert()
* .send(docObj)
* .end(cb);
*
* If a callback is provided will execute the
* insert request immediately. **Returns a Request**.
*
* @param {Object} document
* @param {Function} optional callback
* @api public
* @name insert
*/
Db.prototype.insert = function (body, fn) {
if (isFn(body)) fn = body, body = {};
var opts = merge({
method: (body._id) ? 'PUT' : 'POST'
, body: body
}, this.reqOpts);
if (body._id) opts.path.push(body._id);
var req = new Request(opts);
if (isFn(fn)) req.end(fn);
return req;
};
/**
* ### .get (id[, rev[, callback])
*
* Retrieves a document, optionally at a specific
* revision, from the currently selected database.
*
* couch
* .db('my_app')
* .get('123', 'rev123')
* .end(cb);
*
* Revision is optional, or can also be provided
* using the requests `qs` method.
*
* couch
* .db('my_app')
* .get('123')
* .qs('rev', 'rev123')
* .end(cb);
*
* If a callback is provided will execute the
* insert request immediately. **Returns a Request**.
*
* @param {Mixed} document id
* @param {String} document revision
* @param {Function} optional callback
* @api public
* @name get
*/
Db.prototype.get = function (id, rev, fn) {
if (isFn(rev)) fn = rev, rev = null;
var opts = merge({
method: 'GET'
}, this.reqOpts);
opts.path.push(id);
var req = new Request(opts);
if (rev) req.query('rev', rev);
if (isFn(fn)) req.end(fn);
return req;
};
/**
* ### .update ([id[, rev[, doc[, callback]]]])
*
* Updates a document, optionally at a specific
* revision, to the provided document object,
* from the currently selected database.
*
* couch
* .db('my_app')
* .update(123, docObj)
* .end(cb);
*
* Revision is optional. Document is also optional
* but should be provided using the requests `send`
* command in the chainable api.
*
* couch
* .db('my_app')
* .update(123)
* .send(docObj)
* .end(cb);
*
* When updating the document, if you wish to
* use an aleady existing document object, you
* can provide that directly without specifing
* the `id` parameter. This does _not_ work with
* the requests `send` method.
*
* couch
* .db('my_app')
* .update({
* _id: 123
* , _rev: 'rev123'
* , name: 'Arthur Dent'
* })
* .end(cb);
*
* If a callback is provided will execute the
* insert request immediately. **Returns a Request**.
*
* @param {Mixed} document id
* @param {String} document revision
* @param {Function} optional callback
* @api public
* @name update
*/
Db.prototype.update = function (id, rev, obj, fn) {
if (isObj(rev)) fn = obj, obj = rev, rev = null;
var opts = merge({
method: 'PUT'
, body: obj || {}
}, this.reqOpts);
opts.path.push(id);
opts.body._id = id;
if (rev) opts.body._rev = rev;
var req = new Request(opts);
if (isFn(fn)) req.end(fn);
return req;
};
/**
* ### .remove (id, rev[, callback])
*
* Remove a document from the selected database.
* A revision is required per CouchDB specifications.
*
* If a callback is provided will execute the
* insert request immediately. **Returns a Request**.
*
* @param {Mixed} document id
* @param {String} document revision
* @param {Function} optional callback
* @name remove
* @api public
*/
Db.prototype.remove = function (id, rev, fn) {
if (isFn(rev)) fn = rev, rev = null;
var opts = merge({
method: 'DELETE'
}, this.reqOpts);
opts.path.push(id);
var req = new Request(opts);
if (rev) req.query('rev', rev);
if (isFn(fn)) req.end(fn);
return req;
};
/*!
* CouchError (opts)
*
* Takes a JSON error response from CouchDB and constructes
* a javascript compatible Error Object.
*
* @param {Object} options
* @returns Error
* @api public-ish
*/
function CouchError (opts) {
opts = opts || {};
this.message = opts.reason || opts.message;
this.code = 'E' + (opts.error.toUpperCase() || 'COUCHERR');
this._raw = opts;
if (Error.captureStackTrace) {
Error.captureStackTrace(this, arguments.callee);
}
}
// Ensure inheritance from javascript Error
CouchError.prototype = Object.create(Error.prototype);
CouchError.prototype.name = 'CouchError';
CouchError.prototype.constructor = CouchError;
// provide as part of export for instanceof checking
exports.CouchError = CouchError;
// return exports factory and helpers
return exports;
};
return module.exports(req);
}(superagent);