Comparing version 0.1.9 to 0.2.0
20
nano.js
@@ -195,3 +195,3 @@ /* Minimal Couch In Node | ||
if(continuous) { body.continuous = true; } | ||
relax({db: "_replicate", doc: "_compact", body: body, method: "POST"},callback); | ||
relax({db: "_replicate", body: body, method: "POST"},callback); | ||
} | ||
@@ -250,4 +250,8 @@ | ||
*/ | ||
function get_doc(doc_name,callback) { | ||
relax({db: db_name, doc: doc_name, method: "GET"},callback); | ||
function get_doc(doc_name,params,callback) { | ||
if(typeof params === "function") { | ||
callback = params; | ||
params = {}; | ||
} | ||
relax({db: db_name, doc: doc_name, method: "GET", params: params},callback); | ||
} | ||
@@ -260,4 +264,8 @@ | ||
*/ | ||
function list_docs(callback) { | ||
relax({db: db_name, doc: "_all_docs", method: "GET"},callback); | ||
function list_docs(params,callback) { | ||
if(typeof params === "function") { | ||
callback = params; | ||
params = {}; | ||
} | ||
relax({db: db_name, doc: "_all_docs", method: "GET", params: params},callback); | ||
} | ||
@@ -300,2 +308,4 @@ | ||
, request: relax | ||
, relax: relax // Alias | ||
, dinosaur: relax // Alias | ||
}; | ||
@@ -302,0 +312,0 @@ return public_functions; |
{ "name": "nano" | ||
, "description": "NanoCouch is a minimalistic driver for CouchDB built on mikeals/request" | ||
, "homepage": "http://github.com/dscape/nano" | ||
, "version": "0.1.9" | ||
, "version": "0.2.0" | ||
, "author": "Nuno Job <nunojobpinto@gmail.com> (http://nunojob.com)" | ||
@@ -6,0 +6,0 @@ , "keywords": ["CouchDB", "data", "request", "json", "nosql", "micro", "nano"] |
178
README.md
@@ -1,40 +0,34 @@ | ||
# Nano | ||
# 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` | ||
## Instalation | ||
## instalation | ||
1. Install [npm][1] | ||
1. install [npm][1] | ||
2. `npm install nano` | ||
## Usage | ||
## usage | ||
A quick example on using `nano`. | ||
a quick example using `nano` | ||
In `nano` callback always return three arguments: | ||
to use `nano` you have to either provide a) a `json` `configuration object` or b) a `configuration file path` like `cfg/tests.js`. refer to [cfg/couch.example.js][4] for a example | ||
err: The error, if any. Check error.js for more info. | ||
headers: The HTTP response headers from CouchDB, if no error. | ||
response: The HTTP response body from CouchDB, if no error. | ||
Because in `nano` you can do database operations you are not bound to one and only one database. The first thing you do is load the module pointing either providing a `json` `configuration object` or a `configuration file path` like `cfg/tests.js`. Do refer to [cfg/couch.example.js][4] for a example. There you will also specify where `couchdb` lives, if you want to use `https`, or even to use a `proxy` server. | ||
var nano = require('nano')('./cfg/tests.js'); | ||
Now you can do your database operations using `nano`. These include things like create, delete or list databases. Let's create a database to store some documents: | ||
within the `nano` variable you have various methods you can call. these include tasks like create, delete or list databases: | ||
nano.db.create("alice"); | ||
Where is my callback? Well in `nano` you have the option of not having a callback and say "I don't care". | ||
in this function there is not callback. in `nano` the absence of callback means "do this, ignore what happens" | ||
Of course now you want to insert some documents and you wish you had the callback, so let's add it: | ||
you normally don't want to do that though: | ||
// Clean up the database we created previously | ||
// clean up the database we created previously | ||
nano.db.destroy("alice", function(err,headers,response) { | ||
nano.db.create("alice", function(){ | ||
// Specify the database we are going to use | ||
// specify the database we are going to use | ||
var alicedb = nano.use("alice"); | ||
alicedb.insert("rabbit", {crazy: true}, function(e,h,r){ | ||
if(e) { throw e; } | ||
console.log("You have inserted the rabbit.") | ||
console.log("you have inserted the rabbit.") | ||
}); | ||
@@ -44,84 +38,128 @@ }); | ||
The `alicedb.use` method creates a `scope` where you operate inside a single database. This is just a convenience so you don't have to specify the database name every single time you do an update or delete. | ||
the `alicedb.use` method creates a `scope` where you operate inside a single database. this is just a convenience so you don't have to specify the database name every single time you do an update or delete | ||
Don't forget to delete the database you created: | ||
// 5: var alicedb = nano.use("alice"); | ||
in `nano` a callback has always three arguments | ||
// 6: alicedb.insert("rabbit", {crazy: true}, function(e,h,r){ | ||
// 7: if(e) { throw e; } | ||
// 8: console.log("you have inserted the rabbit.") | ||
// 9: }); | ||
meaning: | ||
e: the `error`, if any. check error.js for more info. | ||
h: the http response `headers` from couchdb, if no error. | ||
r: the http `response body` from couchdb, if no error. | ||
that's it. don't forget to delete the database you created: | ||
nano.db.destroy("alice"); | ||
## Interfaces | ||
## interfaces | ||
### Documents | ||
`*` marks optional | ||
Assuming `var db = nano.use("somedb");`: | ||
### databases (nano) | ||
db.insert(doc_name,doc,callback) // doc_name is optional | ||
db.update(doc_name,rev,doc,callback) | ||
db.destroy(doc_name,rev,callback) | ||
db.get(doc_name,callback) | ||
db.list(callback) | ||
#### functions | ||
### Databases | ||
`nano.db.create(db_name,callback*)` | ||
`nano.db.get(db_name,callback*)` | ||
`nano.db.destroy(db_name,callback*)` | ||
`nano.db.list(callback*)` | ||
`nano.db.compact(db_name,callback*)` | ||
`nano.db.replicate(source,target,continuous*,callback*)` | ||
`nano.use(db_name)` | ||
`nano.request(opts,callback*)` | ||
nano.db.create(db_name,callback) | ||
nano.db.destroy(db_name,callback) | ||
nano.db.get(db_name,callback) | ||
nano.db.list(callback) | ||
nano.db.compact(db_name,callback) | ||
nano.db.replicate(source,target,continuous,callback) // continuous is optional | ||
#### aliases | ||
### Other / Advanced | ||
`nano.use: [nano.db.use, nano.db.scope, nano.scope]` | ||
`nano.request: [nano.relax, nano.dinosaur]` | ||
nano.use(db_name) | ||
nano.request(opts,callback) | ||
### documents (nano.use) | ||
### Aliases | ||
#### functions | ||
You can use `nano.scope` instead of `nano.use`. They are both also available inside db, e.g. `nano.db.scope`. | ||
`db.insert(doc_name*,doc,callback*)` | ||
`db.update(doc_name,rev,doc,callback*)` | ||
`db.destroy(doc_name,rev,callback*)` | ||
`db.get(doc_name,params*,callback*)` | ||
`db.list(params*,callback*)` | ||
When using a database with `nano.use` you can still `replicate`, `compact`, and `list` the database you are using. e.g. to list you can use `db.info` (because `db.list` is for listing documents). Other methods remain the same, e.g. `db.replicate`. | ||
#### aliases | ||
## Future plans | ||
`nano.use` simply sets `db_name` in scope. this way you don't have to specify it every time | ||
Some future plans are mostly: | ||
`nano.db.get: [db.info(callback*)]` | ||
`nano.db.replicate: [db.replicate(target,continuous*,callback*)]` | ||
`nano.db.compact: [db.compact(callback*)]` | ||
1. Add `pipe`, support as provided by request | ||
2. Explore adding `_changes` feed | ||
3. Convenience functions for attachments | ||
4. Support views | ||
5. Support bulk load | ||
### advanced | ||
`nano` is minimalistic so it provides advanced users with a way to code their own extension functions: | ||
nano.request(opts,callback*) | ||
to get a document in a specific rev an advanced user might do: | ||
nano.request( { db: "alice" | ||
, doc: "rabbit" | ||
, method: "GET" | ||
, params: { rev: "1-967a00dff5e02add41819138abb3284d"} | ||
}, | ||
function (_,_,b) { console.log(b) } | ||
); | ||
this is the same as (assuming `db = nano.use("alice");`): | ||
nano.get("rabbit", {rev: "1-967a00dff5e02add41819138abb3284d"}, | ||
function (_,_,b) { console.log(b) | ||
); | ||
## roadmap | ||
1. add `pipe` support as provided by request | ||
2. explore adding `_changes` feed | ||
3. convenience functions for attachments | ||
4. support views | ||
5. support bulk load | ||
6. `_uuids`, `_stats`, `_config`, `_active_tasks`, `_all_docs_by_seq` | ||
7. Support `batch` in updates and inserts. | ||
7. support `batch` in updates and inserts | ||
Great segway to contribute. | ||
## contribute | ||
## Contribute | ||
everyone is welcome to contribute. patches, bugfixes, new features | ||
Everyone is welcome to contribute. | ||
1. create an [issue][2] on github so the community can comment on your idea | ||
2. fork `nano` in github | ||
3. create a new branch `git checkout -b my_branch` | ||
4. create tests for the changes you made | ||
5. make sure you pass both existing and newly inserted tests | ||
6. commit your changes | ||
7. push to your branch `git push origin my_branch` | ||
8. create an pull request | ||
1. Fork `nano` in github | ||
2. Create a new branch - `git checkout -b my_branch` | ||
3. Create tests for the changes you made | ||
4. Make sure you pass both existing and newly inserted tests | ||
5. Commit your changes | ||
6. Push to your branch - `git push origin my_branch` | ||
7. Create an pull request | ||
### tests | ||
### Running the tests | ||
1. install the packages referred as dev dependencies in `package.json` | ||
2. browse to `test/` and `./run`. | ||
To run the tests simply browse to `test/` and `./run`. Don't forget to install the packages referred as dev dependencies in `package.json`. | ||
always make sure all the tests pass before sending in your pull request! | ||
Always make sure all the tests pass before sending in your pull request! | ||
OR I'll tell Santa! | ||
we will tell santa | ||
## Meta | ||
## meta | ||
_ | ||
/ _) ROAR! I'm a vegan! | ||
/ _) ROAR! i'm a vegan! | ||
.-^^^-/ / | ||
__/ / | ||
/__.|_|-|_| | ||
/__.|_|-|_| cannes est superb | ||
* Code: `git clone git://github.com/dscape/nano.git` | ||
* Home: <http://github.com/dscape/nano> | ||
* Bugs: <http://github.com/dscape/nano/issues> | ||
* code: `git clone git://github.com/dscape/nano.git` | ||
* home: <http://github.com/dscape/nano> | ||
* bugs: <http://github.com/dscape/nano/issues> | ||
@@ -128,0 +166,0 @@ `(oO)--',-` in [caos][3] |
@@ -9,3 +9,3 @@ var vows = require('/usr/lib/node_modules/vows/lib/vows') | ||
*****************************************************************************/ | ||
function list_db (callback) { | ||
function list_db(callback) { | ||
nano.db.create("db_li1", function () { | ||
@@ -19,3 +19,3 @@ nano.db.list(function (e,h,b) { | ||
function list_db_ok (e,h,b) { | ||
function list_db_ok(e,h,b) { | ||
nano.db.destroy("db_li1"); | ||
@@ -22,0 +22,0 @@ assert.isNull(e); |
@@ -1,7 +0,9 @@ | ||
var vows = require('/usr/lib/node_modules/vows/lib/vows') | ||
, assert = require('assert') | ||
, cfg = require('../../cfg/tests.js') | ||
, nano = require('../../nano')(cfg) | ||
, db_name = "doc_ge1" | ||
, db = nano.use(db_name); | ||
var vows = require('/usr/lib/node_modules/vows/lib/vows') | ||
, assert = require('assert') | ||
, cfg = require('../../cfg/tests.js') | ||
, nano = require('../../nano')(cfg) | ||
, db_name = "doc_ge1" | ||
, db2_name = "doc_ge2" | ||
, db = nano.use(db_name) | ||
, db2 = nano.use(db2_name); | ||
@@ -30,7 +32,33 @@ /***************************************************************************** | ||
/***************************************************************************** | ||
* get_doc_params * | ||
*****************************************************************************/ | ||
function get_doc_params(callback) { | ||
nano.db.create(db2_name, function () { | ||
db2.insert("foo", {foo: "bar"}, function () { | ||
db2.insert("foo", {foo: "bar"}, function () { // Conflict, no rev | ||
db2.get("foo", {revs_info: true}, function (e,h,b) { | ||
callback(e,h,b); | ||
return; | ||
}); | ||
}); | ||
}); | ||
}); | ||
} | ||
function get_doc_params_ok(e,h,b) { | ||
nano.db.destroy(db2_name); | ||
assert.isNull(e); | ||
assert.ok(b._revs_info); | ||
assert.equal(b._id, "foo"); | ||
assert.equal(b.foo, "bar"); | ||
} | ||
vows.describe('db.get').addBatch({ | ||
"get_doc": { | ||
topic: function () { get_doc(this.callback); } | ||
, "=": get_doc_ok | ||
} | ||
, "=": get_doc_ok }, | ||
"get_doc_params": { | ||
topic: function () { get_doc_params(this.callback); } | ||
, "=": get_doc_params_ok } | ||
}).exportTo(module); |
@@ -1,8 +0,12 @@ | ||
var vows = require('/usr/lib/node_modules/vows/lib/vows') | ||
, assert = require('assert') | ||
, async = require('async') | ||
, cfg = require('../../cfg/tests.js') | ||
, nano = require('../../nano')(cfg) | ||
, db_name = "doc_li1" | ||
, db = nano.use(db_name); | ||
var vows = require('/usr/lib/node_modules/vows/lib/vows') | ||
, assert = require('assert') | ||
, async = require('async') | ||
, cfg = require('../../cfg/tests.js') | ||
, nano = require('../../nano')(cfg) | ||
, db_name = "doc_li1" | ||
, db2_name = "doc_li2" | ||
, db3_name = "doc_li3" | ||
, db = nano.use(db_name) | ||
, db2 = nano.use(db2_name) | ||
, db3 = nano.use(db3_name); | ||
@@ -32,7 +36,66 @@ /***************************************************************************** | ||
/***************************************************************************** | ||
* ns_list_doc * | ||
*****************************************************************************/ | ||
function ns_list_doc(callback) { | ||
nano.db.create(db2_name, function () { | ||
async.parallel( | ||
[ function(cb) { db2.insert("foobar", {"foo": "bar"}, cb); } | ||
, function(cb) { db2.insert("barfoo", {"bar": "foo"}, cb); } | ||
, function(cb) { db2.insert("foobaz", {"foo": "baz"}, cb); } | ||
], | ||
function(err, results){ | ||
nano.request( { db: db2_name | ||
, doc: "_all_docs" | ||
, method: "GET" | ||
, params: {limit: 1} | ||
}, callback); | ||
}); | ||
}); | ||
} | ||
function ns_list_doc_ok(e,h,b) { | ||
nano.db.destroy(db2_name); | ||
assert.isNull(e); | ||
assert.equal(b.rows.length,1); | ||
assert.equal(b.total_rows,3); | ||
assert.ok(b.rows); | ||
} | ||
/***************************************************************************** | ||
* list_doc_params * | ||
*****************************************************************************/ | ||
function list_doc_params(callback) { | ||
nano.db.create(db3_name, function () { | ||
async.parallel( | ||
[ function(cb) { db3.insert("foobar", {"foo": "bar"}, cb); } | ||
, function(cb) { db3.insert("barfoo", {"bar": "foo"}, cb); } | ||
, function(cb) { db3.insert("foobaz", {"foo": "baz"}, cb); } | ||
], | ||
function(err, results){ | ||
db3.list({startkey: '"c"'},callback); | ||
}); | ||
}); | ||
} | ||
function list_doc_params_ok(e,h,b) { | ||
nano.db.destroy(db3_name); | ||
assert.isNull(e); | ||
assert.equal(b.rows.length,2); | ||
assert.equal(b.total_rows,3); | ||
assert.ok(b.rows); | ||
} | ||
vows.describe('doc.list').addBatch({ | ||
"list_doc": { | ||
topic: function () { list_doc(this.callback); } | ||
, "=": list_doc_ok | ||
} | ||
, "=": list_doc_ok }, | ||
"ns_list_doc": { | ||
topic: function () { ns_list_doc(this.callback); } | ||
, "=": ns_list_doc_ok }, | ||
"list_doc_params": { | ||
topic: function () { list_doc_params(this.callback); } | ||
, "=": list_doc_params_ok } | ||
}).exportTo(module); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
34571
835
170