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.0.3 to 0.1.5

.gitignore

4

error.js

@@ -13,4 +13,4 @@ /*

*/
function gen_err(reason,code,request,http_code,type) {
if(typeof reason === 'string') { error = new Error(reason); }
function gen_err(error,code,request,http_code,type) {
if(typeof error === 'string') { error = new Error(error); }
if(!type) {

@@ -17,0 +17,0 @@ type = http_code;

@@ -1,11 +0,39 @@

/* Minimalistic Couch In Node */
/* 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')
, error = require('./error')
, headers = { "content-type": "application/json"
, "accept": "application/json"
}
, headers = { "content-type": "application/json", "accept": "application/json" }
, nano;
module.exports = exports = nano = function nano_module(cfg) {
/*
* Nano is a library that helps you building requests on top of mikeals/request
* No more, no less.
*
*
* - 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 take the trash out. (compact)
*/
module.exports = exports = nano = function database_module(cfg) {
var public_functions = {};

@@ -16,2 +44,5 @@ if(typeof cfg === "string") {

/****************************************************************************
* aux *
****************************************************************************/
/*

@@ -22,7 +53,17 @@ * Request DB

*
* @error {request:connect_db} There was a problem connecting to CouchDB
* 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!
*
* @error {request:socket} There was a problem connecting to CouchDB
* @error {couch:*} Any error that CouchDB returns when creating a DB
*
* @param {name} The database name
* @param {method} A valid HTTP verb (e.g. GET)
* @param {opts} The request options; e.g. {db: "test", method: "GET"}
* {opts.db} REQUIRED The database name
* {opts.method} REQUIRED The HTTP Method
* {opts.doc} The document URI, if any
* {opts.body} The body, if any
* @param {callback} The function to callback

@@ -32,22 +73,29 @@ *

*/
function request_db(name,method,callback) {
if(!callback) { // Then ignore callback
callback = function () { return; };
function relax(opts,callback) {
var url = cfg.database(opts.db)
, req = { method: opts.method, headers: headers }
, params = opts.params
, status_code
, parsed
, rh;
if(!callback) { callback = function () { return; }; } // Void Callback
if(opts.doc) { url += "/" + opts.doc; } // Add the document to the URL
if(opts.body) {
if(typeof opts.body === "object") { req.body = JSON.stringify(opts.body); }
else { req.body = opts.body; }
}
var req =
{ uri: cfg.database(name)
, method: method
, headers: headers
};
req.uri = url + (params ? "?" + qs.stringify(params) : "");
request(req, function(e,h,b){
var status_code = h.statusCode
, parsed;
rh = h.headers;
status_code = h.statusCode;
if(e) {
callback(error.request_err(e,"connect_db",req,status_code));
callback(error.request_err(e,"socket",req,status_code),rh,b);
return;
}
parsed = JSON.parse(b);
if (status_code === 200 || status_code === 201) { callback(null,parsed); }
if (status_code === 200 || status_code === 201 || status_code === 202) {
callback(null,rh,parsed);
}
else { // Proxy the error
callback(error.couch_err(parsed.reason,parsed.error,req,status_code));
callback(error.couch_err(parsed.reason,parsed.error,req,status_code),rh,parsed);
}

@@ -57,20 +105,25 @@ });

/****************************************************************************
* db *
****************************************************************************/
/*
* Creates a CouchDB Database
*
* e.g. 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);
* }
* });
* 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);
* }
* });
* }
*
* @see request_db
* @see relax
*/
function create_db(name, callback) {
request_db(name,"PUT",callback);
function create_db(db_name, callback) {
relax({db: db_name, method: "PUT"},callback);
}

@@ -86,6 +139,6 @@

*
* @see request_db
* @see relax
*/
function destroy_db(name, callback) {
request_db(name,"DELETE",callback);
function destroy_db(db_name, callback) {
relax({db: db_name, method: "DELETE"},callback);
}

@@ -100,6 +153,6 @@

*
* @see request_db
* @see relax
*/
function get_db(name, callback) {
request_db(name,"GET",callback);
function get_db(db_name, callback) {
relax({db: db_name, method: "GET"},callback);
}

@@ -110,12 +163,116 @@

*
* e.g. nano.db.get(db_name, function(e,b) {
* e.g. nano.db.list(function(e,b) {
* console.log(b);
* });
*
* @see request_db
* @see relax
*/
function list_dbs(callback) {
request_db("_all_dbs","GET",callback);
relax({db: "_all_dbs", method: "GET"},callback);
}
/*
* Compacts a CouchDB Database
*
* e.g. nano.db.compact(db_name);
*
* @see relax
*/
function compact_db(db_name, callback) {
relax({db: db_name, doc: "_compact", method: "POST"},callback);
}
/*
* Replicates a CouchDB Database
*
* e.g. nano.db.replicate(db_1, db_2);
*
* @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; }
relax({db: "_replicate", doc: "_compact", body: body, method: "POST"},callback);
}
/****************************************************************************
* doc *
****************************************************************************/
function document_module(db_name) {
var public_functions = {};
/*
* Inserts a document in a CouchDB Database
*
* @see relax
*/
function insert_doc(doc_name,doc,callback) {
var opts = {db: db_name};
if(typeof doc === "function") {
callback = doc;
opts.body = doc_name;
opts.method = "POST";
}
else {
opts.doc = doc_name;
opts.body = doc;
opts.method = "PUT";
}
relax(opts,callback);
}
/*
* Destroy a document from CouchDB Database
*
* @see relax
*/
function destroy_doc(doc_name,rev,callback) {
relax({db: db_name, doc: doc_name, method: "DELETE", params: {rev: rev}},
callback);
}
/*
* Get's a document from a CouchDB Database
*
* @see relax
*/
function get_doc(doc_name,callback) {
relax({db: db_name, doc: doc_name, method: "GET"},callback);
}
/*
* Lists all the documents in a CouchDB Database
*
* @see relax
*/
function list_docs(callback) {
relax({db: db_name, doc: "_all_docs", method: "GET"},callback);
}
public_functions = { info: function(cb) { get_db(db_name,cb); }
, replicate: function(target,continuous,cb) {
if(typeof continuous === "function") {
cb = continuous;
continuous = false;
}
replicate_db(db_name,target,continuous,cb);
}
, compact: function(cb) { compact_db(db_name,cb); }
// hook.io? socket.io?
//, changes: { add: add_listener
// , remove: remove_listener}
, insert: insert_doc
, get: get_doc
, destroy: destroy_doc
//, bulk: bulk_doc
, list: list_docs
//, views: {}
};
return public_functions;
}
public_functions = { db: { create: create_db

@@ -125,19 +282,27 @@ , get: get_db

, list: list_dbs
//, replicate: replicate_db
//, compact: compact_db
//, changes: { add: add_listener
// , remove: remove_listener}
, use: document_module // Alias
, scope: document_module // Alias
, compact: compact_db
, replicate: replicate_db
}
//, create: create_doc
//, get: get_doc
//, destroy: destroy_doc
//, bulk: bulk_doc
//, list: list_docs
, use: document_module
, scope: document_module // Alias
, request: relax
};
return public_functions;
};
/*
* And now an ASCII Dinosaur
* _
* / _) ROAR! I'm a vegan!
* .-^^^-/ /
* __/ /
* /__.|_|-|_|
*
* Thanks for visiting! Come Again!
*/
nano.version = JSON.parse(
fs.readFileSync(__dirname + "/package.json")).version;
nano.path = __dirname;
{ "name": "nano"
, "description": "NanoCouch is a minimalistic driver for CouchDB built on mikeals/request"
, "homepage": "http://github.com/dscape/nanocouch"
, "version": "0.0.3"
, "version": "0.1.5"
, "author": "Nuno Job <nunojobpinto@gmail.com> (http://nunojob.com)"
, "keywords": ["CouchDB", "data", "request", "json", "nosql", "micro", "nano"]
, "dependencies": { "request": ">=1.9.8", "b64": "1.0.0" }
, "devDependencies": { "async": "~0.1.9", "vows": "~0.5.10" }
, "main": "./nano.js"
, "engines" : { "node" : "~v0.4.8" }
}

@@ -10,6 +10,6 @@ var vows = require('/usr/lib/node_modules/vows/lib/vows')

function create_db (callback) {
nano.db.destroy("cr1", function () {
nano.db.create("cr1", function (e,b) {
callback(e,b);
nano.db.destroy("cr1");
nano.db.destroy("db_cr1", function () {
nano.db.create("db_cr1", function (e,h,b) {
callback(e,h,b);
nano.db.destroy("db_cr1");
});

@@ -19,5 +19,6 @@ });

function create_db_ok(e,b) {
function create_db_ok(e,h,b) {
assert.isNull(e);
assert.equal(b.ok, true);
nano.db.destroy("db_cr1");
}

@@ -29,7 +30,7 @@

function recursive_retries_create_db(tried,callback) {
nano.db.destroy("cr2", function () {
nano.db.create("cr2", function (e,b) {
nano.db.destroy("db_cr2", function () {
nano.db.create("db_cr2", function (e,h,b) {
if(tried.tried === tried.max_retries) {
callback(true);
nano.db.destroy("cr2");
nano.db.destroy("db_cr2");
}

@@ -46,2 +47,3 @@ else {

assert.equal(v,true);
nano.db.destroy("db_cr2");
}

@@ -48,0 +50,0 @@

@@ -6,6 +6,9 @@ var vows = require('/usr/lib/node_modules/vows/lib/vows')

function destroy_db (callback) {
nano.db.create("de1", function () {
nano.db.destroy("de1", function (e,b) {
callback(e,b);
/*****************************************************************************
* destroy_db *
*****************************************************************************/
function destroy_db(callback) {
nano.db.create("db_de1", function () {
nano.db.destroy("db_de1", function (e,h,b) {
callback(e,h,b);
return;

@@ -16,5 +19,6 @@ });

function destroy_db_ok (e,b) {
function destroy_db_ok(e,h,b) {
assert.isNull(e);
assert.equal(b.ok, true);
nano.db.destroy("db_de1");
}

@@ -21,0 +25,0 @@

@@ -6,6 +6,9 @@ var vows = require('/usr/lib/node_modules/vows/lib/vows')

function get_db (callback) {
nano.db.create("ge1", function () {
nano.db.get("ge1", function (e,b) {
callback(e,b);
/*****************************************************************************
* get_db *
*****************************************************************************/
function get_db(callback) {
nano.db.create("db_ge1", function () {
nano.db.get("db_ge1", function (e,h,b) {
callback(e,h,b);
return;

@@ -16,7 +19,8 @@ });

function get_db_ok (e,b) {
function get_db_ok(e,h,b) {
assert.isNull(e);
assert.equal(b.doc_count,0);
assert.equal(b.doc_del_count,0);
assert.equal(b.db_name,"ge1");
assert.equal(b.db_name,"db_ge1");
nano.db.destroy("db_ge1");
}

@@ -23,0 +27,0 @@

@@ -6,6 +6,9 @@ var vows = require('/usr/lib/node_modules/vows/lib/vows')

/*****************************************************************************
* list_db *
*****************************************************************************/
function list_db (callback) {
nano.db.create("li1", function () {
nano.db.list(function (e,b) {
callback(e,b);
nano.db.create("db_li1", function () {
nano.db.list(function (e,h,b) {
callback(e,h,b);
return;

@@ -16,5 +19,6 @@ });

function list_db_ok (e,b) {
function list_db_ok (e,h,b) {
assert.isNull(e);
assert.notEqual(b.indexOf("li1"),-1);
assert.notEqual(b.indexOf("db_li1"),-1);
nano.db.destroy("db_li1");
}

@@ -21,0 +25,0 @@

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