Skip to content
This repository has been archived by the owner on Nov 5, 2018. It is now read-only.

Commit

Permalink
Pipe Support
Browse files Browse the repository at this point in the history
  • Loading branch information
dscape committed Aug 18, 2011
1 parent 1925a8e commit 08969d7
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
.DS_Store .DS_Store
node_modules/ node_modules/
*tmp.*
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -126,6 +126,13 @@ this is the same as (assuming `alice = nano.use("alice");`):
function (_,_,b) { console.log(b) } function (_,_,b) { console.log(b) }
); );


### pipe

you can pipe in `nano` just like you do in express. this is available in all methods:

alice.attachment.get("sugar", "att", {rev: rev})
.pipe(fs.createWriteStream("/tmp/sugar-for-rabbit"));

# roadmap # roadmap


check [issues][2] check [issues][2]
Expand Down
57 changes: 30 additions & 27 deletions nano.js
Expand Up @@ -77,7 +77,6 @@ module.exports = exports = nano = function database_module(cfg) {
, status_code , status_code
, parsed , parsed
, rh; , rh;
if(!callback) { callback = function () { return; }; } // void callback
if(opts.doc) { if(opts.doc) {
url += "/" + opts.doc; // add the document to the url url += "/" + opts.doc; // add the document to the url
if(opts.att) { url += "/" + opts.att; } // add the attachment to the url if(opts.att) { url += "/" + opts.att; } // add the attachment to the url
Expand All @@ -91,13 +90,14 @@ module.exports = exports = nano = function database_module(cfg) {
} }
if(opts.encoding) { req.encoding = opts.encoding; } if(opts.encoding) { req.encoding = opts.encoding; }
req.uri = url + (_.isEmpty(params) ? "" : "?" + qs.stringify(params)); req.uri = url + (_.isEmpty(params) ? "" : "?" + qs.stringify(params));
if(!callback) { return request(req); } // void callback
request(req, function(e,h,b){ request(req, function(e,h,b){
if(e) { return callback(error.request_err(e,"socket",req,status_code),{},b); } if(e) { return callback(error.request_err(e,"socket",req,status_code),{},b); }
rh = h.headers; rh = h.headers;
status_code = h.statusCode; status_code = h.statusCode;
// did we get json or binary? // did we get json or binary?
try { parsed = JSON.parse(b); } catch (err) { parsed = b; } try { parsed = JSON.parse(b); } catch (err) { parsed = b; }
if (status_code === 200 || status_code === 201 || status_code === 202) { if (status_code >= 200 && status_code < 300) {
callback(null,rh,parsed); callback(null,rh,parsed);
} }
else { // proxy the error directly from couchdb else { // proxy the error directly from couchdb
Expand Down Expand Up @@ -131,7 +131,7 @@ module.exports = exports = nano = function database_module(cfg) {
* @see relax * @see relax
*/ */
function create_db(db_name, callback) { function create_db(db_name, callback) {
relax({db: db_name, method: "PUT"},callback); return relax({db: db_name, method: "PUT"},callback);
} }


/* /*
Expand All @@ -146,7 +146,7 @@ module.exports = exports = nano = function database_module(cfg) {
* @see relax * @see relax
*/ */
function destroy_db(db_name, callback) { function destroy_db(db_name, callback) {
relax({db: db_name, method: "DELETE"},callback); return relax({db: db_name, method: "DELETE"},callback);
} }


/* /*
Expand All @@ -161,7 +161,7 @@ module.exports = exports = nano = function database_module(cfg) {
* @see relax * @see relax
*/ */
function get_db(db_name, callback) { function get_db(db_name, callback) {
relax({db: db_name, method: "GET"},callback); return relax({db: db_name, method: "GET"},callback);
} }


/* /*
Expand All @@ -174,7 +174,7 @@ module.exports = exports = nano = function database_module(cfg) {
* @see relax * @see relax
*/ */
function list_dbs(callback) { function list_dbs(callback) {
relax({db: "_all_dbs", method: "GET"},callback); return relax({db: "_all_dbs", method: "GET"},callback);
} }


/* /*
Expand All @@ -192,7 +192,7 @@ module.exports = exports = nano = function database_module(cfg) {
callback = design_name; callback = design_name;
design_name = null; design_name = null;
} }
relax({db: db_name, doc: "_compact", att: design_name, method: "POST"},callback); return relax({db: db_name, doc: "_compact", att: design_name, method: "POST"},callback);
} }


/* /*
Expand All @@ -212,7 +212,7 @@ module.exports = exports = nano = function database_module(cfg) {
callback = params; callback = params;
params = {}; params = {};
} }
relax({db: db_name, doc: "_changes", params: params, method: "GET"},callback); return relax({db: db_name, doc: "_changes", params: params, method: "GET"},callback);
} }


/* /*
Expand All @@ -233,7 +233,7 @@ module.exports = exports = nano = function database_module(cfg) {
} }
var body = {source: source, target: target}; var body = {source: source, target: target};
if(continuous) { body.continuous = true; } if(continuous) { body.continuous = true; }
relax({db: "_replicate", body: body, method: "POST"},callback); return relax({db: "_replicate", body: body, method: "POST"},callback);
} }


/* /*
Expand All @@ -242,7 +242,7 @@ module.exports = exports = nano = function database_module(cfg) {
* @see relax * @see relax
*/ */
function config(callback) { function config(callback) {
relax({db: "_config", method: "GET"}, function (e,h,r) { return relax({db: "_config", method: "GET"}, function (e,h,r) {
if(e) { callback(e); } if(e) { callback(e); }
callback(null,h,{nano: cfg, couch: r}); callback(null,h,{nano: cfg, couch: r});
}); });
Expand Down Expand Up @@ -274,7 +274,7 @@ module.exports = exports = nano = function database_module(cfg) {
opts.method = "PUT"; opts.method = "PUT";
} }
} }
relax(opts,callback); return relax(opts,callback);
} }


/* /*
Expand All @@ -289,7 +289,7 @@ module.exports = exports = nano = function database_module(cfg) {
*/ */
function update_doc(doc_name,rev,doc,callback) { function update_doc(doc_name,rev,doc,callback) {
doc._rev = rev; doc._rev = rev;
relax({ db: db_name, doc: doc_name, method: "PUT", body: doc},callback); return relax({ db: db_name, doc: doc_name, method: "PUT", body: doc},callback);
} }


/* /*
Expand All @@ -301,7 +301,7 @@ module.exports = exports = nano = function database_module(cfg) {
* @see relax * @see relax
*/ */
function destroy_doc(doc_name,rev,callback) { function destroy_doc(doc_name,rev,callback) {
relax({db: db_name, doc: doc_name, method: "DELETE", params: {rev: rev}}, return relax({db: db_name, doc: doc_name, method: "DELETE", params: {rev: rev}},
callback); callback);
} }


Expand All @@ -323,7 +323,7 @@ module.exports = exports = nano = function database_module(cfg) {
callback = params; callback = params;
params = {}; params = {};
} }
relax({db: db_name, doc: doc_name, method: "GET", params: params},callback); return relax({db: db_name, doc: doc_name, method: "GET", params: params},callback);
} }


/* /*
Expand All @@ -339,7 +339,7 @@ module.exports = exports = nano = function database_module(cfg) {
callback = params; callback = params;
params = {}; params = {};
} }
relax({db: db_name, doc: "_all_docs", method: "GET", params: params},callback); return relax({db: db_name, doc: "_all_docs", method: "GET", params: params},callback);
} }


/* /*
Expand All @@ -352,7 +352,7 @@ module.exports = exports = nano = function database_module(cfg) {
* @see relax * @see relax
*/ */
function bulk_docs(docs,callback) { function bulk_docs(docs,callback) {
relax({db: db_name, doc: "_bulk_docs", body: docs, method: "POST"},callback); return relax({db: db_name, doc: "_bulk_docs", body: docs, method: "POST"},callback);
} }


/************************************************************************** /**************************************************************************
Expand Down Expand Up @@ -385,8 +385,8 @@ module.exports = exports = nano = function database_module(cfg) {
callback = params; callback = params;
params = {}; params = {};
} }
relax({ db: db_name, att: att_name, method: "PUT", content_type: content_type return relax({ db: db_name, att: att_name, method: "PUT", content_type: content_type
, doc: doc_name, params: params, body: att},callback); , doc: doc_name, params: params, body: att},callback);
} }


/* /*
Expand All @@ -403,8 +403,8 @@ module.exports = exports = nano = function database_module(cfg) {
callback = params; callback = params;
params = {}; params = {};
} }
relax({ db: db_name, att: att_name, method: "GET", doc: doc_name return relax({ db: db_name, att: att_name, method: "GET", doc: doc_name
, params: params, encoding: "binary"},callback); , params: params, encoding: "binary"},callback);
} }


/* /*
Expand All @@ -417,28 +417,31 @@ module.exports = exports = nano = function database_module(cfg) {
* @see relax * @see relax
*/ */
function destroy_att(doc_name,att_name,rev,callback) { function destroy_att(doc_name,att_name,rev,callback) {
relax({ db: db_name, att: att_name, method: "DELETE" return relax({ db: db_name, att: att_name, method: "DELETE"
, doc: doc_name, params: {rev: rev}},callback); , doc: doc_name, params: {rev: rev}},callback);
} }


public_functions = { info: function(cb) { get_db(db_name,cb); } public_functions = { info: function(cb) { return get_db(db_name,cb); }
, replicate: function(target,continuous,cb) { , replicate: function(target,continuous,cb) {
if(typeof continuous === "function") { if(typeof continuous === "function") {
cb = continuous; cb = continuous;
continuous = false; continuous = false;
} }
replicate_db(db_name,target,continuous,cb); 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);
} }
, compact: function(cb) { compact_db(db_name,cb); }
, changes: function(params,cb) { changes_db(db_name,params,cb); }
, insert: insert_doc , insert: insert_doc
, update: update_doc , update: update_doc
, get: get_doc , get: get_doc
, destroy: destroy_doc , destroy: destroy_doc
, bulk: bulk_docs , bulk: bulk_docs
, list: list_docs , list: list_docs
, view: { compact: function(design_name,cb) { , view: { compact: function(design_name,cb) {
compact_db(db_name,design_name,cb); } } return compact_db(db_name,design_name,cb); }
}
, attachment: { insert: insert_att , attachment: { insert: insert_att
, get: get_att , get: get_att
, destroy: destroy_att , destroy: destroy_att
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{ "name": "nano" { "name": "nano"
, "description": "minimalistic couchdb driver for node.js" , "description": "minimalistic couchdb driver for node.js"
, "homepage": "http://github.com/dscape/nano" , "homepage": "http://github.com/dscape/nano"
, "version": "0.4.7" , "version": "0.5.0"
, "author": "Nuno Job <nunojobpinto@gmail.com> (http://nunojob.com)" , "author": "Nuno Job <nunojobpinto@gmail.com> (http://nunojob.com)"
, "keywords": ["couchdb", "data", "request", "json", "nosql", "micro", "nano"] , "keywords": ["couchdb", "data", "request", "json", "nosql", "micro", "nano"]
, "dependencies": {"request": "~2.0.4", "underscore": "~1.1.7"} , "dependencies": {"request": "~2.0.4", "underscore": "~1.1.7"}
Expand Down
8 changes: 0 additions & 8 deletions tests/att/get.js
@@ -1,6 +1,5 @@
var vows = require('vows') var vows = require('vows')
, assert = require('assert') , assert = require('assert')
, async = require('async')
, cfg = require('../../cfg/tests.js') , cfg = require('../../cfg/tests.js')
, nano = require('../../nano')(cfg) , nano = require('../../nano')(cfg)
, pixel = "Qk06AAAAAAAAADYAAAAoAAAAAQAAAP////8BABgAAAAAAAAAAAATCwAAEwsAAAAAAAAAAAAAWm2CAA=="; , pixel = "Qk06AAAAAAAAADYAAAAoAAAAAQAAAP////8BABgAAAAAAAAAAAATCwAAEwsAAAAAAAAAAAAAWm2CAA==";
Expand All @@ -11,13 +10,6 @@ function db(i) { return nano.use(db_name(i)); }
/***************************************************************************** /*****************************************************************************
* att_get * * att_get *
*****************************************************************************/ *****************************************************************************/
/*
* Pipe example:
* require('request')
* .get('http://localhost:5984/att_gea/new/att')
* .pipe(require('fs').createWriteStream('/Users/njob/Desktop/temp.bmp'))
*
*/
function att_get(callback) { function att_get(callback) {
var buffer = new Buffer(pixel, 'base64'); var buffer = new Buffer(pixel, 'base64');
nano.db.create(db_name("a"), function () { nano.db.create(db_name("a"), function () {
Expand Down
44 changes: 44 additions & 0 deletions tests/att/pipe.js
@@ -0,0 +1,44 @@
var vows = require('vows')
, fs = require('fs')
, assert = require('assert')
, cfg = require('../../cfg/tests.js')
, nano = require('../../nano')(cfg)
, pixel = "Qk06AAAAAAAAADYAAAAoAAAAAQAAAP////8BABgAAAAAAAAAAAATCwAAEwsAAAAAAAAAAAAAWm2CAA==";

function db_name(i) { return "att_pi" + i; }
function db(i) { return nano.use(db_name(i)); }
function file_name(i) { return __dirname + "/." + i + "-tmp.bmp"; }
function f_s(i) { return fs.createWriteStream(file_name(i)); }

/*****************************************************************************
* att_pipe *
*****************************************************************************/
function att_pipe(callback) {
var buffer = new Buffer(pixel, 'base64')
, file_stream = f_s("a");
file_stream.on("close", function() { callback(); });
nano.db.create(db_name("a"), function () {
db("a").attachment.insert("new", "att", "Hello", "text/plain",
function(e,_,b) {
if(e) { callback(e); }
db("a").attachment.insert("new", "att", buffer, "image/bmp", {rev: b.rev},
function (e2,_,b2) {
if(e2) { callback(e2); }
db("a").attachment.get("new", "att", {rev: b2.rev}).pipe(file_stream);
});
});
});
}

function att_pipe_ok() {
nano.db.destroy(db_name("a"));
console.log(file_name("a"))
assert.equal(fs.readFileSync(file_name("a")).toString("base64"), pixel);
//fs.deleteFileSync(file_name("a"));
}

vows.describe('attachment.pipe').addBatch({
"att_pipe": {
topic: function () { att_pipe(this.callback); }
, "=": att_pipe_ok }
}).exportTo(module);

0 comments on commit 08969d7

Please sign in to comment.