Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nano

Package Overview
Dependencies
Maintainers
1
Versions
155
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nano - npm Package Compare versions

Comparing version 0.4.6 to 0.5.3

tests/att/.a-tmp.bmp

11

cfg/couch.example.js

@@ -9,9 +9,14 @@ var cfg = { host: "mydb.iriscouch.com"

cfg.credentials = function credentials() {
if (cfg.user && cfg.pass) {
return cfg.user + ":" + cfg.pass + "@";
}
else { return ""; }
}();
cfg.url = function () {
return "http" + (cfg.ssl ? "s" : "") + "://" + cfg.http_credentials + cfg.host +
return "http" + (cfg.ssl ? "s" : "") + "://" + cfg.credentials + cfg.host +
":" + cfg.port;
}();
cfg.database = function(name) { return cfg.url + "/" + name; };
module.exports = exports = cfg;

@@ -33,11 +33,2 @@ /* Config file used during tests

/*
* Database
*
* @param {name} The database name
*
* @return The full URL of the database
*/
cfg.database = function(name) { return cfg.url + "/" + name; };
module.exports = exports = cfg;
/*
* Generic Error
* generic error
*

@@ -25,17 +25,15 @@ * e.g. missing rev information:

*
* Extension on Error to support more complex logic.
* extension on error to support more complex logic.
*
* @param {error} Either an Error, or a string that will be casted into new
* error.
* @param {code} The recognizable error code
* @param {http_code} The HTTP code from CouchDB
* @param {error:error|string} the error or a reason for the error
* @param {code:string} the recognizable error code
* @param {http_code:integer:optional} the http code from couchdb
* @param {request:object} the request that was made to couch
* @param {type:string} a namespace for the error, e.g. couch
*
* @return An error augmented an driver specific code
* @return an augmented error that helps you know more than the stack trace
*/
function gen_err(error,code,request,http_code,type) {
if(typeof error === 'string') { error = new Error(error); }
if(!type) {
type = http_code;
http_code = 500;
}
if(!type) { type = http_code; http_code = 500; }
error.error = code;

@@ -48,3 +46,3 @@ error.http_code = http_code;

exports.request_err = function (e,c,r,h) { return gen_err(e,c,r,h,"request"); };
exports.couch_err = function (e,c,r,h) { return gen_err(e,c,r,h,"couch"); };
exports.request_err = function (e,c,r,h) { return gen_err(e,c,r,h,"request");};
exports.couch_err = function (e,c,r,h) { return gen_err(e,c,r,h,"couch"); };

@@ -1,16 +0,16 @@

/* Minimal Couch In Node
/* minimal couch in node
*
* Copyright 2011 Nuno Job <nunojob.com> (oO)--',--
* 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
* 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.
* 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.
*/

@@ -26,14 +26,9 @@ var request = require('request')

/*
* Nano is a library that helps you building requests on top of mikeals/request
* No more, no less.
* 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).
*
* - As simple as possible, but no simpler than that
* - It is not an ORM for CouchDB
* - It is not all fancy and RoR like
* - It is not meant to prevent you from doing stupid things.
* Be creative. Be silly. Do stupid things. I won't thow exceptions back at you.
*
* Have fun! Relax, and don't forget to compact.
* Dinosaurs spaceships ftw!
* dinosaurs spaceships!
*/

@@ -43,40 +38,41 @@ module.exports = exports = nano = function database_module(cfg) {

if(typeof cfg === "string") {
cfg = require(cfg); // No CFG? Maybe it's a file path?
cfg = require(cfg); // no cfg? maybe it's a file path?
}
if(cfg.proxy) { // Proxy support
request = request.defaults({proxy: cfg.proxy});
if(cfg.proxy) {
request = request.defaults({proxy: cfg.proxy}); // proxy support
}
/****************************************************************************
* aux *
* relax *
****************************************************************************/
/*
* Request DB
* relax
*
* An auxiliary function to do the request to CouchDB
* base for all request using nano
* this function assumes familiarity with the couchdb api
*
* Important Announcement: _airplane_voice_
* This function is public but does not try to prevent you from shooting
* yourself in the foot. Use only if you know _exactly_ what you are trying
* to do. Plus if you are using it directly and it's not part of the interface
* please consider emailing me about that, or telling me what it is, or
* doing a pull request!
* e.g.
* nano.request( { db: "alice"
* , doc: "rabbit"
* , method: "GET"
* , params: { rev: "1-967a00dff5e02add41819138abb3284d"}
* },
* function (_,_,b) { console.log(b) });
*
* @error {request:socket} There was a problem connecting to CouchDB
* @error {couch:*} Any error that CouchDB returns when creating a DB
* @error {request:socket} problem connecting to couchdb
* @error {couch:*} an error proxied from couchdb
*
* @param {opts:object} The request options; e.g. {db: "test", method: "GET"}
* {opts.db:string} The database name
* {opts.method:string} The http Method
* {opts.doc:string:optional} The document name
* {opts.att:string:optional} The attachment name
* {opts.content_type:string:optional} The content type, else json will be used
* {opts.body:object|string|binary:optional} The JSON body
* @param {callback:function:optional} The function to callback
*
* @return Execution of the code in your callback. Hopefully you are handling
* @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.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.database(opts.db)
, req = { method: opts.method, headers: headers }
var url = cfg.url + "/" + opts.db
, req = { method: (opts.method || "GET"), headers: headers }
, params = opts.params

@@ -86,6 +82,5 @@ , status_code

, rh;
if(!callback) { callback = function () { return; }; } // Void Callback
if(opts.doc) {
url += "/" + opts.doc; // Add the document to the URL
if(opts.att) { url += "/" + opts.att; } // Add the attachment to the URL
url += "/" + opts.doc; // add the document to the url
if(opts.att) { url += "/" + opts.att; } // add the attachment to the url
}

@@ -95,20 +90,19 @@ if(opts.content_type) { req.headers["content-type"] = opts.content_type; }

if (Buffer.isBuffer(opts.body)) {
req.body = opts.body; // Raw data
req.body = opts.body; // raw data
}
else { req.body = JSON.stringify(opts.body); } // JSON
else { req.body = JSON.stringify(opts.body); } // json data
}
if(opts.encoding) { req.encoding = opts.encoding; }
req.uri = url + (_.isEmpty(params) ? "" : "?" + qs.stringify(params));
if(!callback) { return request(req); } // void callback
request(req, function(e,h,b){
if(e) {
callback(error.request_err(e,"socket",req,status_code),{},b);
return;
}
if(e) { return callback(error.request_err(e,"socket",req,status_code),{},b); }
rh = h.headers;
status_code = h.statusCode;
// Most likely its JSON but sometimes we get a binary attachment
// did we get json or binary?
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);
}
else { // Proxy the error
else { // proxy the error directly from couchdb
callback(error.couch_err(parsed.reason,parsed.error,req,status_code),rh,parsed);

@@ -123,3 +117,3 @@ }

/*
* Creates a CouchDB Database
* creates a couchdb database
* http://wiki.apache.org/couchdb/HTTP_database_API

@@ -140,3 +134,3 @@ *

*
* @param {db_name:string} The name of the database
* @param {db_name:string} database name
*

@@ -146,14 +140,13 @@ * @see relax

function create_db(db_name, callback) {
relax({db: db_name, method: "PUT"},callback);
return relax({db: db_name, method: "PUT"},callback);
}
/*
* Annihilates a CouchDB Database
* annihilates a couchdb database
*
* e.g. nano.db.delete(db_name);
*
* Even though this looks sync it is an async function
* and therefor order is not guaranteed
* even though this examples looks sync it is an async function
*
* @param {db_name:string} The name of the database
* @param {db_name:string} database name
*

@@ -163,7 +156,7 @@ * @see relax

function destroy_db(db_name, callback) {
relax({db: db_name, method: "DELETE"},callback);
return relax({db: db_name, method: "DELETE"},callback);
}
/*
* Gets information about a CouchDB Database
* gets information about a couchdb database
*

@@ -174,3 +167,3 @@ * e.g. nano.db.get(db_name, function(e,b) {

*
* @param {db_name:string} The name of the database
* @param {db_name:string} database name
*

@@ -180,7 +173,7 @@ * @see relax

function get_db(db_name, callback) {
relax({db: db_name, method: "GET"},callback);
return relax({db: db_name, method: "GET"},callback);
}
/*
* Lists all the databases in CouchDB
* lists all the databases in couchdb
*

@@ -194,12 +187,12 @@ * e.g. nano.db.list(function(e,b) {

function list_dbs(callback) {
relax({db: "_all_dbs", method: "GET"},callback);
return relax({db: "_all_dbs", method: "GET"},callback);
}
/*
* Compacts a CouchDB Database
* compacts a couchdb database
*
* e.g. nano.db.compact(db_name);
*
* @param {db_name:string} The name of the database
* @param {design_name:string:optional} The name of the design document
* @param {db_name:string} database name
* @param {design_name:string:optional} design document name
*

@@ -213,7 +206,7 @@ * @see relax

}
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);
}
/*
* CouchDB Database _changes feed
* couchdb database _changes feed
*

@@ -224,4 +217,4 @@ * e.g. nano.db.changes(db_name, {since: 2}, function (e,h,r) {

*
* @param {db_name:string} The name of the database
* @param {params:object:optional} Additions to the querystring
* @param {db_name:string} database name
* @param {params:object:optional} additions to the querystring
*

@@ -235,13 +228,13 @@ * @see relax

}
relax({db: db_name, doc: "_changes", params: params, method: "GET"},callback);
return relax({db: db_name, doc: "_changes", params: params, method: "GET"},callback);
}
/*
* Replicates a CouchDB Database
* replicates a couchdb database
*
* e.g. nano.db.replicate(db_1, db_2);
*
* @param {source:string} The name of the source database
* @param {target:string} The name of the target database
* @param {continuous:bool:optional} Turn on continuous replication
* @param {source:string} name of the source database
* @param {target:string} name of the target database
* @param {continuous:bool:optional} continuous replication on?
*

@@ -257,7 +250,7 @@ * @see relax

if(continuous) { body.continuous = true; }
relax({db: "_replicate", body: body, method: "POST"},callback);
return relax({db: "_replicate", body: body, method: "POST"},callback);
}
/*
* Returns the CouchDB + Nano Configuration
* returns couchdb + nano configuration
*

@@ -267,3 +260,3 @@ * @see relax

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); }

@@ -281,7 +274,7 @@ callback(null,h,{nano: cfg, couch: r});

/*
* Inserts a document in a CouchDB Database
* inserts a document in a couchdb database
* http://wiki.apache.org/couchdb/HTTP_Document_API
*
* @param {doc:object|string} The document
* @param {doc_name:string:optional} The name of the document
* @param {doc:object|string} document body
* @param {doc_name:string:optional} document name
*

@@ -301,30 +294,15 @@ * @see relax

}
relax(opts,callback);
return relax(opts,callback);
}
/*
* Updates a document in a CouchDB Database
* destroy a document from a couchdb database
*
* @param {doc_name:string} document name
* @param {rev:string} previous document revision
*
* @param {doc_name:string} The name of the document
* @param {rev:string} The previous document revision
* @param {doc:object|string} The document
*
* @see relax
*/
function update_doc(doc_name,rev,doc,callback) {
doc._rev = rev;
relax({ db: db_name, doc: doc_name, method: "PUT", body: doc},callback);
}
/*
* Destroy a document from CouchDB Database
*
* @param {doc_name:string} The name of the document
* @param {rev:string} The previous document revision
*
* @see relax
*/
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);

@@ -334,3 +312,3 @@ }

/*
* Get's a document from a CouchDB Database
* get a document from a couchdb database
*

@@ -342,4 +320,4 @@ * e.g. db2.get("foo", {revs_info: true}, function (e,h,b) {

*
* @param {doc_name:string} The name of the document
* @param {params:object:optional} Additions to the querystring
* @param {doc_name:string} document name
* @param {params:object:optional} additions to the querystring
*

@@ -353,9 +331,9 @@ * @see relax

}
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);
}
/*
* Lists all the documents in a CouchDB Database
* lists all the documents in a couchdb database
*
* @param {params:object:optional} Additions to the querystring
* @param {params:object:optional} additions to the querystring
*

@@ -370,10 +348,10 @@ * @see get_doc

}
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);
}
/*
* Bulk update/delete/insert functionality
* http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API
* bulk update/delete/insert functionality
* [1]: http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API
*
* @param {docs:object} The documents as per the CouchDB API (check link)
* @param {docs:object} documents as per the couchdb api[1]
*

@@ -384,3 +362,3 @@ * @see get_doc

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);
}

@@ -392,4 +370,4 @@

/*
* Inserting an attachment
* http://wiki.apache.org/couchdb/HTTP_Document_API
* inserting an attachment
* [2]: http://wiki.apache.org/couchdb/HTTP_Document_API
*

@@ -402,10 +380,11 @@ * e.g.

*
* Don't forget that params.rev is required in all cases except when
* creating a new document with a new attachment via this method
* 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} The name of the document
* @param {att_name:string} The name of the attachment
* @param {att:buffer} The attachment data
* @param {content_type:string} The attachment content type
* @param {params:object:optional} Additions to the querystring
* @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
*

@@ -419,12 +398,12 @@ * @see relax

}
relax({ db: db_name, att: att_name, method: "PUT", content_type: content_type
, doc: doc_name, params: params, body: att},callback);
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
* get an attachment
*
* @param {doc_name:string} The name of the document
* @param {att_name:string} The name of the attachment
* @param {params:object:optional} Additions to the querystring
* @param {doc_name:string} document name
* @param {att_name:string} attachment name
* @param {params:object:optional} additions to the querystring
*

@@ -438,12 +417,12 @@ * @see relax

}
relax({ db: db_name, att: att_name, method: "GET"
, doc: doc_name, params: params},callback);
return relax({ db: db_name, att: att_name, method: "GET", doc: doc_name
, params: params, encoding: "binary"},callback);
}
/*
* Destroy an attachment
* destroy an attachment
*
* @param {doc_name:string} The name of the document
* @param {att_name:string} The name of the attachment
* @param {rev:string} The document last revision
* @param {doc_name:string} document name
* @param {att_name:string} attachment name
* @param {rev:string} previous document revision
*

@@ -453,7 +432,7 @@ * @see relax

function destroy_att(doc_name,att_name,rev,callback) {
relax({ db: db_name, att: att_name, method: "DELETE"
, doc: doc_name, params: {rev: rev}},callback);
return relax({ db: db_name, att: att_name, method: "DELETE"
, 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) {

@@ -464,8 +443,9 @@ if(typeof continuous === "function") {

}
replicate_db(db_name,target,continuous,cb);
return replicate_db(db_name,target,continuous,cb);
}
, compact: function(cb) { compact_db(db_name,cb); }
, changes: function(params,cb) { changes_db(db_name,params,cb); }
, compact: function(cb) { return compact_db(db_name,cb); }
, changes: function(params,cb) {
return changes_db(db_name,params,cb);
}
, insert: insert_doc
, update: update_doc
, get: get_doc

@@ -476,3 +456,4 @@ , destroy: destroy_doc

, 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

@@ -490,4 +471,4 @@ , get: get_att

, list: list_dbs
, use: document_module // Alias
, scope: document_module // Alias
, use: document_module // alias
, scope: document_module // alias
, compact: compact_db

@@ -498,7 +479,7 @@ , replicate: replicate_db

, use: document_module
, scope: document_module // Alias
, scope: document_module // alias
, request: relax
, config: config
, relax: relax // Alias
, dinosaur: relax // Alias
, relax: relax // alias
, dinosaur: relax // alias
};

@@ -509,5 +490,5 @@ return public_functions;

/*
* And now an ASCII Dinosaur
* and now an ascii dinosaur
* _
* / _) ROAR! I'm a vegan!
* / _) ROAR! i'm a vegan!
* .-^^^-/ /

@@ -517,3 +498,6 @@ * __/ /

*
* Thanks for visiting! Come Again!
* thanks for visiting! come again!
*
* LH1059-A321
* LH1178-A321
*/

@@ -520,0 +504,0 @@

{ "name": "nano"
, "description": "NanoCouch is a minimalistic driver for CouchDB built on mikeals/request"
, "description": "minimalistic couchdb driver for node.js"
, "homepage": "http://github.com/dscape/nano"
, "version": "0.4.6"
, "version": "0.5.3"
, "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"}

@@ -8,0 +8,0 @@ , "devDependencies": { "async": "~0.1.9", "vows": "~0.5.10" }

# nano
`nano` (short for nanocouch) is a minimalistic `couchdb` driver for `node.js`
`nano` (short for `nanocouch`) is a minimalistic `couchdb` driver for `node.js`

@@ -89,3 +89,2 @@ # instalation

`doc.insert(doc,doc_name*,callback*)`
`doc.update(doc_name,rev,doc,callback*)`
`doc.destroy(doc_name,rev,callback*)`

@@ -130,2 +129,9 @@ `doc.get(doc_name,params*,callback*)`

### 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

@@ -132,0 +138,0 @@

var vows = require('vows')
, assert = require('assert')
, async = require('async')
, cfg = require('../../cfg/tests.js')

@@ -30,5 +29,5 @@ , nano = require('../../nano')(cfg)

function att_get_ok(e,h,b) {
assert.isNull(e);
nano.db.destroy(db_name("a"));
var from_buffer = new Buffer(b, 'binary').toString('base64');
assert.isNull(e);
var from_buffer = new Buffer(b, "binary").toString("base64");
assert.equal(from_buffer, pixel);

@@ -35,0 +34,0 @@ }

@@ -13,4 +13,4 @@ var vows = require('vows')

nano.db.create(db_name, function () {
db.insert("foo", {foo: "bar"}, function (_,_,b) {
db.update("foo", b.rev, {foo: "baz"}, function (e,_,b) {
db.insert({foo: "bar"}, "foo", function (_,_,b) {
db.insert({"_rev": b.rev, foo: "baz"}, "foo", function (e,_,b) {
callback(e,b);

@@ -17,0 +17,0 @@ return;

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc