This repository has been archived by the owner on Nov 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 157
/
nano.js
497 lines (474 loc) · 15.9 KB
/
nano.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
/* minimal couch in node
*
* copyright 2011 nuno job <nunojob.com> (oO)--',--
*
* licensed under the apache license, version 2.0 (the "license");
* you may not use this file except in compliance with the license.
* you may obtain a copy of the license at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* unless required by applicable law or agreed to in writing, software
* distributed under the license is distributed on an "as is" basis,
* without warranties or conditions of any kind, either express or implied.
* see the license for the specific language governing permissions and
* limitations under the license.
*/
var request = require('request')
, fs = require('fs')
, qs = require('querystring')
, _ = require('underscore')
, u = require('url')
, error = require('./error')
, default_url = "http://localhost:5984"
, nano;
/*
* nano is a library that helps you building requests to couchdb
* that is built on top of mikeals/request
*
* no more, no less
* be creative. be silly. have fun! relax (and don't forget to compact).
*
* dinosaurs spaceships!
*/
module.exports = exports = nano = function database_module(cfg) {
var public_functions = {}, path, db;
if(typeof cfg === "string") {
if(/^https?:/.test(cfg)) { cfg = {url: cfg}; } // url
else {
try { cfg = require(cfg); } // file path
catch(e) { console.error("bad cfg: couldn't load file"); }
}
}
if(!cfg) {
console.error("bad cfg: you passed undefined");
cfg = {};
}
if(cfg.proxy) {
request = request.defaults({proxy: cfg.proxy}); // proxy support
}
if(!cfg.url) {
console.error("bad cfg: using default=" + default_url);
cfg = {url: default_url}; // if everything else fails, use default
}
path = u.parse(cfg.url);
/****************************************************************************
* relax *
****************************************************************************/
/*
* relax
*
* base for all request using nano
* this function assumes familiarity with the couchdb api
*
* e.g.
* nano.request( { db: "alice"
* , doc: "rabbit"
* , method: "GET"
* , params: { rev: "1-967a00dff5e02add41819138abb3284d"}
* },
* function (_,_,b) { console.log(b) });
*
* @error {request:socket} problem connecting to couchdb
* @error {couch:*} an error proxied from couchdb
*
* @param {opts:object} request options; e.g. {db: "test", method: "GET"}
* {opts.db:string} database name
* {opts.method:string:optional} http method, defaults to "GET"
* {opts.path:string:optional} a full path, override `doc` and `att`
* {opts.doc:string:optional} document name
* {opts.att:string:optional} attachment name
* {opts.content_type:string:optional} content type, default to json
* {opts.body:object|string|binary:optional} document or attachment body
* {opts.encoding:string:optional} encoding for attachments
* @param {callback:function:optional} function to call back
*/
function relax(opts,callback) {
var url = cfg.url + "/" + opts.db
, headers = { "content-type": "application/json"
, "accept": "application/json"
}
, req = { method: (opts.method || "GET"), headers: headers }
, params = opts.params
, status_code
, parsed
, rh;
if(opts.path) {
url += "/" + opts.path;
}
else if(opts.doc) {
url += "/" + encodeURIComponent(opts.doc); // add the document to the url
if(opts.att) { url += "/" + opts.att; } // add the attachment to the url
}
if(opts.encoding && callback) {
req.encoding = opts.encoding;
delete req.headers["content-type"];
delete req.headers.accept;
}
if(opts.content_type) {
req.headers["content-type"] = opts.content_type;
delete req.headers.accept; // undo headers set
}
req.uri = url + (_.isEmpty(params) ? "" : "?" + qs.stringify(params));
if(!callback) { return request(req); } // void callback, pipe
if(opts.body) {
if (Buffer.isBuffer(opts.body)) {
req.body = opts.body; // raw data
}
else { req.body = JSON.stringify(opts.body); } // json data
}
request(req, function(e,h,b){
rh = (h && h.headers || {});
rh['status-code'] = status_code = (h && h.statusCode || 500);
if(e) { return callback(error.request(e,"socket",req,status_code),rh,b); }
delete rh.server; // prevent security vunerabilities related to couchdb
delete rh["content-length"]; // prevent problems with trims and stalled responses
try { parsed = JSON.parse(b); } catch (err) { parsed = b; } // did we get json or binary?
if (status_code >= 200 && status_code < 300) {
callback(null,rh,parsed);
}
else { // proxy the error directly from couchdb
callback(error.couch(parsed.reason,parsed.error,req,status_code),rh,parsed);
}
});
}
/****************************************************************************
* db *
****************************************************************************/
/*
* creates a couchdb database
* http://wiki.apache.org/couchdb/HTTP_database_API
*
* e.g. function recursive_retries_create_db(tried,callback) {
* nano.db.create(db_name, function (e,b) {
* if(tried.tried === tried.max_retries) {
* callback("Retries work");
* return;
* }
* else {
* tried.tried += 1;
* recursive_retries_create_db(tried,callback);
* }
* });
* }
*
* @param {db_name:string} database name
*
* @see relax
*/
function create_db(db_name, callback) {
return relax({db: db_name, method: "PUT"},callback);
}
/*
* annihilates a couchdb database
*
* e.g. nano.db.delete(db_name);
*
* even though this examples looks sync it is an async function
*
* @param {db_name:string} database name
*
* @see relax
*/
function destroy_db(db_name, callback) {
return relax({db: db_name, method: "DELETE"},callback);
}
/*
* gets information about a couchdb database
*
* e.g. nano.db.get(db_name, function(e,b) {
* console.log(b);
* });
*
* @param {db_name:string} database name
*
* @see relax
*/
function get_db(db_name, callback) {
return relax({db: db_name, method: "GET"},callback);
}
/*
* lists all the databases in couchdb
*
* e.g. nano.db.list(function(e,b) {
* console.log(b);
* });
*
* @see relax
*/
function list_dbs(callback) {
return relax({db: "_all_dbs", method: "GET"},callback);
}
/*
* compacts a couchdb database
*
* e.g. nano.db.compact(db_name);
*
* @param {db_name:string} database name
* @param {design_name:string:optional} design document name
*
* @see relax
*/
function compact_db(db_name, design_name, callback) {
if(typeof design_name === "function") {
callback = design_name;
design_name = null;
}
return relax({db: db_name, path: ("_compact" + design_name), method: "POST"},callback);
}
/*
* couchdb database _changes feed
*
* e.g. nano.db.changes(db_name, {since: 2}, function (e,h,r) {
* console.log(r);
* });
*
* @param {db_name:string} database name
* @param {params:object:optional} additions to the querystring
*
* @see relax
*/
function changes_db(db_name, params, callback) {
if(typeof params === "function") {
callback = params;
params = {};
}
return relax({db: db_name, path: "_changes", params: params, method: "GET"},callback);
}
/*
* replicates a couchdb database
*
* e.g. nano.db.replicate(db_1, db_2);
*
* @param {source:string} name of the source database
* @param {target:string} name of the target database
* @param {continuous:bool:optional} continuous replication on?
*
* @see relax
*/
function replicate_db(source, target, continuous, callback) {
if(typeof continuous === "function") {
callback = continuous;
continuous = false;
}
var body = {source: source, target: target};
if(continuous) { body.continuous = true; }
return relax({db: "_replicate", body: body, method: "POST"},callback);
}
/****************************************************************************
* doc *
****************************************************************************/
function document_module(db_name) {
var public_functions = {};
/*
* inserts a document in a couchdb database
* http://wiki.apache.org/couchdb/HTTP_Document_API
*
* @param {doc:object|string} document body
* @param {doc_name:string:optional} document name
*
* @see relax
*/
function insert_doc(doc,doc_name,callback) {
var opts = {db: db_name, body: doc, method: "POST"};
if(doc_name) {
if(typeof doc_name === "function") {
callback = doc_name;
}
else {
opts.doc = doc_name;
opts.method = "PUT";
}
}
return relax(opts,callback);
}
/*
* destroy a document from a couchdb database
*
* @param {doc_name:string} document name
* @param {rev:string} previous document revision
*
* @see relax
*/
function destroy_doc(doc_name,rev,callback) {
return relax({db: db_name, doc: doc_name, method: "DELETE", params: {rev: rev}},
callback);
}
/*
* get a document from a couchdb database
*
* e.g. db2.get("foo", {revs_info: true}, function (e,h,b) {
* console.log(e,h,b);
* return;
* });
*
* @param {doc_name:string} document name
* @param {params:object:optional} additions to the querystring
*
* @see relax
*/
function get_doc(doc_name,params,callback) {
if(typeof params === "function") {
callback = params;
params = {};
}
return relax({db: db_name, doc: doc_name, method: "GET", params: params},callback);
}
/*
* lists all the documents in a couchdb database
*
* @param {params:object:optional} additions to the querystring
*
* @see get_doc
* @see relax
*/
function list_docs(params,callback) {
if(typeof params === "function") {
callback = params;
params = {};
}
return relax({db: db_name, path: "_all_docs", method: "GET", params: params},callback);
}
/*
* bulk update/delete/insert functionality
* [1]: http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API
*
* @param {docs:object} documents as per the couchdb api[1]
*
* @see get_doc
* @see relax
*/
function bulk_docs(docs,callback) {
return relax({db: db_name, path: "_bulk_docs", body: docs, method: "POST"},callback);
}
/**************************************************************************
* attachment *
**************************************************************************/
/*
* inserting an attachment
* [2]: http://wiki.apache.org/couchdb/HTTP_Document_API
*
* e.g.
* db.attachment.insert("new", "att", buffer, "image/bmp", {rev: b.rev},
* function(_,_,response) {
* console.log(response);
* });
*
* don't forget that params.rev is required in most cases. only exception
* is when creating a new document with a new attachment. consult [2] for
* details
*
* @param {doc_name:string} document name
* @param {att_name:string} attachment name
* @param {att:buffer} attachment data
* @param {content_type:string} attachment content-type
* @param {params:object:optional} additions to the querystring
*
* @see relax
*/
function insert_att(doc_name,att_name,att,content_type,params,callback) {
if(typeof params === "function") {
callback = params;
params = {};
}
return relax({ db: db_name, att: att_name, method: "PUT", content_type: content_type
, doc: doc_name, params: params, body: att},callback);
}
/*
* get an attachment
*
* @param {doc_name:string} document name
* @param {att_name:string} attachment name
* @param {params:object:optional} additions to the querystring
*
* @see relax
*/
function get_att(doc_name,att_name,params,callback) {
if(typeof params === "function") {
callback = params;
params = {};
}
return relax({ db: db_name, att: att_name, method: "GET", doc: doc_name
, params: params, encoding: "binary"},callback);
}
/*
* destroy an attachment
*
* @param {doc_name:string} document name
* @param {att_name:string} attachment name
* @param {rev:string} previous document revision
*
* @see relax
*/
function destroy_att(doc_name,att_name,rev,callback) {
return relax({ db: db_name, att: att_name, method: "DELETE"
, doc: doc_name, params: {rev: rev}},callback);
}
public_functions = { info: function(cb) { return get_db(db_name,cb); }
, replicate: function(target,continuous,cb) {
if(typeof continuous === "function") {
cb = continuous;
continuous = false;
}
return replicate_db(db_name,target,continuous,cb);
}
, compact: function(cb) { return compact_db(db_name,cb); }
, changes: function(params,cb) {
return changes_db(db_name,params,cb);
}
, insert: insert_doc
, get: get_doc
, destroy: destroy_doc
, bulk: bulk_docs
, list: list_docs
, view: { compact: function(design_name,cb) {
return compact_db(db_name,design_name,cb); }
}
, attachment: { insert: insert_att
, get: get_att
, destroy: destroy_att
}
};
return public_functions;
}
public_functions = { db: { create: create_db
, get: get_db
, destroy: destroy_db
, list: list_dbs
, use: document_module // alias
, scope: document_module // alias
, compact: compact_db
, replicate: replicate_db
, changes: changes_db
}
, use: document_module
, scope: document_module // alias
, request: relax
, config: cfg
, relax: relax // alias
, dinosaur: relax // alias
};
// does the user want a database, or nano?
if(!_.isEmpty(path.pathname.split('/')[1])) {
db = path.pathname.split('/')[1];
cfg.url = path.protocol + '//' + path.host; // reset url
return document_module(db);
}
else { return public_functions; }
};
/*
* and now an ascii dinosaur
* _
* / _) ROAR! i'm a vegan!
* .-^^^-/ /
* __/ /
* /__.|_|-|_|
*
* thanks for visiting! come again!
*
* LH1059-A321
* LH1178-A321
*/
nano.version = JSON.parse(
fs.readFileSync(__dirname + "/package.json")).version;
nano.path = __dirname;