Permalink
Browse files

[minor] whitespacing

  • Loading branch information...
2 parents f5a23b2 + 11cd741 commit cb6b1412f47bc08174bd096b3c08b75a7de6c44d @dscape committed Jul 4, 2012
Showing with 121 additions and 21 deletions.
  1. +13 −0 README.md
  2. +56 −21 nano.js
  3. 0 tests/att/.temp.bmp
  4. +52 −0 tests/doc/copy.js
View
@@ -327,6 +327,19 @@ alice.head('rabbit', function(err, _, headers) {
});
```
+### db.copy(src_doc, dest_doc, opts, [callback])
+
+`copy` the contents (and attachments) of a document
+to a new document, or overwrite an existing target document
+
+``` js
+alice.copy('rabbit', 'rabbit2', { overwrite: true }, function(err, _, headers) {
+ if (!err)
+ console.log(headers);
+});
+```
+
+
### db.bulk(docs, [params], [callback])
bulk operations(update/delete/insert) on the database, refer to the
View
77 nano.js
@@ -72,7 +72,7 @@ module.exports = exports = nano = function database_module(cfg) {
* @error {request:socket} problem connecting to couchdb
* @error {couch:*} an error proxied from couchdb
*
- * @param {opts:object|string} request options;
+ * @param {opts:object|string} request options;
* e.g. {db: "test", method: "GET"}
* {opts.db:string} database name
* {opts.method:string:optional} http method, defaults to "GET"
@@ -81,7 +81,8 @@ module.exports = exports = nano = function database_module(cfg) {
* {opts.att:string:optional} attachment name
* {opts.headers:object:optional} additional http headers
* {opts.content_type:string:optional} content type, default to json
- * {opts.body:object|string|binary:optional} document or attachment body
+ * {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
*/
@@ -118,7 +119,7 @@ module.exports = exports = nano = function database_module(cfg) {
// cookie jar support
// check github.com/mikeal/request for docs
- if (opts.jar) {
+ if (opts.jar) {
req.jar = opts.jar;
}
@@ -138,7 +139,7 @@ module.exports = exports = nano = function database_module(cfg) {
else if(opts.doc) {
// not a design document
if(!/^_design/.test(opts.doc)) {
- try {
+ try {
req.uri += "/" + encodeURIComponent(opts.doc);
}
catch (error) {
@@ -181,7 +182,7 @@ module.exports = exports = nano = function database_module(cfg) {
['startkey', 'endkey', 'key', 'keys'].forEach(function (key) {
if (key in params) {
try { params[key] = JSON.stringify(params[key]); }
- catch (err) {
+ catch (err) {
return errs.handle(errs.merge(err,
{ "note" : "bad params: " + key + " = " + params[key]
, "scope" : "nano"
@@ -198,7 +199,7 @@ module.exports = exports = nano = function database_module(cfg) {
}), callback);
}
- try {
+ try {
req.uri += "?" + qs.stringify(params);
}
catch (err2) {
@@ -225,7 +226,7 @@ module.exports = exports = nano = function database_module(cfg) {
return value;
}
});
- } catch (err3) {
+ } catch (err3) {
return errs.handle(errs.merge(err3,
{ "note" : "body seems to be invalid json"
, "scope" : "nano"
@@ -236,17 +237,18 @@ module.exports = exports = nano = function database_module(cfg) {
}
if(opts.form) {
- req.headers['content-type'] = 'application/x-www-form-urlencoded; charset=utf-8';
+ req.headers['content-type'] =
+ 'application/x-www-form-urlencoded; charset=utf-8';
req.body = qs.stringify(opts.form).toString('utf8');
}
-
+
log(req);
// streaming mode
if(!callback) {
try {
return request(req);
- } catch (err4) {
+ } catch (err4) {
return errs.handle(errs.merge(err4,
{ "note" : "request threw when you tried to stream"
, "scope" : "request"
@@ -297,7 +299,7 @@ module.exports = exports = nano = function database_module(cfg) {
}
});
return stream;
- } catch(err5) {
+ } catch(err5) {
return errs.merge(err5,
{ "note" : "request threw when you tried to create the object"
, "scope" : "request"
@@ -461,7 +463,7 @@ module.exports = exports = nano = function database_module(cfg) {
* @param {params:object:optional} additions to the querystring
* check the follow documentation for the full api
* https://github.com/iriscouch/follow
- *
+ *
*
* @see relax
*/
@@ -615,7 +617,7 @@ module.exports = exports = nano = function database_module(cfg) {
/*
* get the head of a document from a couchdb database
*
- * e.g. db2.head("foo", function (e,b) {
+ * e.g. db2.head("foo", function (e,b,h) {
* console.log(e,b,h);
* return;
* });
@@ -631,6 +633,37 @@ module.exports = exports = nano = function database_module(cfg) {
}
/*
+ * copy a document to a new document, or overwrite an existing document
+ * [1]: http://wiki.apache.org/couchdb/HTTP_Document_API#COPY
+ *
+ * e.g. db2.copy("source", "target", { overwrite: true }, function(e,b,h) {
+ * console.log(e,b,h);
+ * return;
+ * });
+ *
+ * @param {doc_src:string} source document name
+ * @param {doc_dest:string} destination document name
+ * @param {opts:object:optional} set overwrite preference
+ *
+ * @see relax
+ */
+ function copy_doc(doc_src, doc_dest, opts, callback) {
+ if(typeof opts === "function") {
+ callback = opts;
+ opts = {};
+ }
+ var params = { db: db_name, doc: doc_src, method: "COPY"
+ , headers: {"Destination": doc_dest} };
+ head_doc(doc_dest, function (e,b,h) {
+ if (!e && opts.overwrite) {
+ params.headers.Destination += "?rev=" +
+ h.etag.substring(1, h.etag.length - 1);
+ }
+ return relax(params, callback);
+ });
+ }
+
+ /*
* lists all the documents in a couchdb database
*
* @param {params:object:optional} additions to the querystring
@@ -653,7 +686,8 @@ module.exports = exports = nano = function database_module(cfg) {
* [1]: http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API
*
* @param {doc_names:object} document keys as per the couchdb api[1]
- * @param {params:object} additions to the querystring, note that include_docs is always set to true
+ * @param {params:object} additions to the querystring, note
+ * that include_docs is always set to true
*
* @see get_doc
* @see relax
@@ -705,13 +739,13 @@ module.exports = exports = nano = function database_module(cfg) {
* @param {doc_name:string} document name to update
* @param {params:object} additions to the querystring
*/
- function update_with_handler_doc(design_name, update_name,
+ function update_with_handler_doc(design_name, update_name,
doc_name, body, callback) {
if(typeof body === "function") {
callback = body;
body = {};
}
- var update_path = '_design/' + design_name + '/_update/' +
+ var update_path = '_design/' + design_name + '/_update/' +
update_name + '/' + doc_name;
return relax(
{ db: db_name, path: update_path, method: "PUT"
@@ -812,8 +846,8 @@ module.exports = exports = nano = function database_module(cfg) {
, replicate : function(target, opts, cb) {
return replicate_db(db_name,target,opts,cb);
}
- , compact : function(cb) {
- return compact_db(db_name,cb);
+ , compact : function(cb) {
+ return compact_db(db_name,cb);
}
, changes : function(params,cb) {
return changes_db(db_name,params,cb);
@@ -825,12 +859,13 @@ module.exports = exports = nano = function database_module(cfg) {
, insert : insert_doc
, get : get_doc
, head : head_doc
+ , copy : copy_doc
, destroy : destroy_doc
, bulk : bulk_docs
, list : list_docs
, fetch : fetch_docs
, config : {url: cfg.url, db: db_name}
- , attachment :
+ , attachment :
{ insert : insert_att
, get : get_att
, destroy : destroy_att
@@ -848,7 +883,7 @@ module.exports = exports = nano = function database_module(cfg) {
}
// server level exports
- public_functions =
+ public_functions =
{ db :
{ create : create_db
, get : get_db
@@ -912,7 +947,7 @@ module.exports = exports = nano = function database_module(cfg) {
path = u.parse(cfg.url);
path_array = path.pathname.split('/').filter(function(e) { return e; });
- // nano('http://couch.nodejitsu.com/db1')
+ // nano('http://couch.nodejitsu.com/db1')
// should return a database
// nano('http://couch.nodejitsu.com')
// should return a nano object
View
No changes.
View
@@ -0,0 +1,52 @@
+var specify = require('specify')
+ , helpers = require('../helpers')
+ , timeout = helpers.timeout
+ , nano = helpers.nano
+ , nock = helpers.nock
+ ;
+
+var mock = nock(helpers.couch, "doc/copy")
+ , db = nano.use("doc_copy")
+ , rev
+ ;
+
+specify("doc_copy:setup", timeout, function (assert) {
+ nano.db.create("doc_copy", function (err) {
+ assert.equal(err, undefined, "Failed to create database");
+ db.insert({"foo": "baz"}, "foo_src", function (error, foo) {
+ assert.equal(error, undefined, "Should have stored foo");
+ assert.equal(foo.ok, true, "Response should be ok");
+ assert.ok(foo.rev, "Response should have rev");
+ rev = foo.rev;
+ });
+ db.insert({"baz": "foo"}, "foo_dest", function (error, foo) {
+ assert.equal(error, undefined, "Should have stored foo");
+ assert.equal(foo.ok, true, "Response should be ok");
+ assert.ok(foo.rev, "Response should have rev");
+ rev = foo.rev;
+ });
+ });
+});
+
+specify("doc_copy:test", timeout, function (assert) {
+ db.copy("foo_src", "foo_dest", { overwrite: true }, function (error, response, headers) {
+ assert.equal(error, undefined, "Should have copied and overwritten foo_src to foo_dest");
+ assert.equal(headers["status-code"], 201, "Status code should be 201");
+ });
+ db.copy("foo_src", "foo_dest", { overwrite: false }, function (error, response, headers) {
+ assert.equal(error.error, "conflict", "Should have a document conflict.");
+ });
+ db.copy("foo_src", "baz_dest", function (error, response, headers) {
+ assert.equal(error, undefined, "Should have copied foo_src to new baz_dest document");
+ assert.equal(headers["status-code"], 201, "Status code should be 201");
+ });
+});
+
+specify("doc_copy:teardown", timeout, function (assert) {
+ nano.db.destroy("doc_copy", function (err) {
+ assert.equal(err, undefined, "Failed to destroy database");
+ assert.ok(mock.isDone(), "Some mocks didn't run");
+ });
+});
+
+specify.run(process.argv.slice(2));

0 comments on commit cb6b141

Please sign in to comment.