Comparing version 4.2.1 to 4.3.0
100
nano.js
@@ -18,2 +18,4 @@ /* minimal couch in node | ||
/* jshint node:true */ | ||
/* jshint laxcomma:true */ | ||
'use strict'; | ||
@@ -129,2 +131,7 @@ | ||
// add multiparts | ||
if(opts.multipart) { | ||
req.multipart = opts.multipart; | ||
} | ||
// make sure we add our headers to the request | ||
@@ -266,2 +273,6 @@ req.headers = _.extend(req.headers, opts.headers, cfg.default_headers); | ||
if (req.headers['content-type'] === 'multipart/related' && req.method === 'GET') { | ||
req.encoding = null; | ||
} | ||
try { | ||
@@ -690,3 +701,6 @@ var stream = request(req, function(e,h,b) { | ||
return head_doc(doc_dest, function (e,b,h) { | ||
if (typeof h.etag === 'string') { | ||
if (e && e.status_code !== 404) { | ||
return callback(e); | ||
} | ||
if (h && typeof h.etag === 'string') { | ||
params.headers.Destination += '?rev=' + | ||
@@ -869,2 +883,82 @@ h.etag.substring(1, h.etag.length - 1); | ||
/************************************************************************** | ||
* multipart * | ||
*************************************************************************/ | ||
/* | ||
* inserting a document with attachments | ||
* [2]: http://wiki.apache.org/couchdb/HTTP_Document_API#Multiple_Attachments | ||
* | ||
* e.g. | ||
* db.multipart.insert('new', 'att', buffer, 'image/bmp', {rev: b.rev}, | ||
* function(_,response) { | ||
* console.log(response); | ||
* }); | ||
* | ||
* 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:object|string} document body | ||
* @param {attachments:array} attachments | ||
* @param {doc_name:string:optional} document name | ||
* @param {params:string:optional} additions to the querystring | ||
* | ||
* @see relax | ||
*/ | ||
function insert_multipart(doc,attachments,params,callback) { | ||
if(typeof params === 'function') { | ||
callback = params; | ||
params = {}; | ||
} | ||
if(typeof params === 'string') { | ||
params = {doc_name: params}; | ||
} | ||
var doc_name = params.doc_name; | ||
delete params.doc_name; | ||
var stubs = attachments.reduce(function(memo, att) { | ||
memo[att.name] = { | ||
follows: true, | ||
length: att.data.length, | ||
content_type: att.content_type | ||
}; | ||
return memo; | ||
}, {}); | ||
var multipart = [ | ||
{ | ||
'content-type': 'application/json', | ||
body: JSON.stringify(_.extend({}, doc, { _attachments: stubs })) | ||
} | ||
]; | ||
attachments.forEach(function(att) { | ||
multipart.push({ | ||
body: att.data | ||
}); | ||
}); | ||
return relax( | ||
{ db: db_name, method: 'PUT' | ||
, content_type: 'multipart/related', doc: doc_name, params: params | ||
, multipart: multipart}, callback); | ||
} | ||
/* | ||
* get a document with attachments | ||
* | ||
* @param {doc_name:string} document name | ||
* @param {params:object:optional} additions to the querystring | ||
* | ||
* @see relax | ||
*/ | ||
function get_multipart(doc_name,params,callback) { | ||
if(typeof params === 'function') { | ||
callback = params; | ||
params = {}; | ||
} | ||
params.attachments = true; | ||
return relax({ db: db_name, method: 'GET', doc: doc_name | ||
, content_type: 'multipart/related', params: params},callback); | ||
} | ||
/************************************************************************** | ||
* attachment * | ||
@@ -997,2 +1091,6 @@ *************************************************************************/ | ||
, config : {url: cfg.url, db: db_name} | ||
, multipart : | ||
{ insert : insert_multipart | ||
, get : get_multipart | ||
} | ||
, attachment : | ||
@@ -999,0 +1097,0 @@ { insert : insert_att |
@@ -6,3 +6,3 @@ { | ||
"repository": "git://github.com/dscape/nano", | ||
"version": "4.2.1", | ||
"version": "4.3.0", | ||
"author": "Nuno Job <nunojobpinto@gmail.com> (http://nunojob.com)", | ||
@@ -42,3 +42,4 @@ "contributors": [ | ||
"Jan Krems @jkrems", | ||
"Owen Evans <owen@bgeek.net> (http://www.bgeek.net)" | ||
"Owen Evans <owen@bgeek.net> (http://www.bgeek.net)", | ||
"Johannes J. Schmidt <schmidt@netzmerk.com> (http://die-tf.de)" | ||
], | ||
@@ -45,0 +46,0 @@ "keywords": [ |
@@ -12,3 +12,3 @@ # nano | ||
* **errors** - errors are proxied directly from couchdb: if you know couchdb | ||
you already know `nano` | ||
you already know `nano`. | ||
@@ -47,2 +47,5 @@ | ||
- [db.fetch(docnames, [params], [callback])](#dbfetchdocnames-params-callback) | ||
- [multipart functions](#multipart-functions) | ||
- [db.multipart.insert(doc, attachments, [params], [callback])](#dbmultipartinsertdoc-attachments-params-callback) | ||
- [db.multipart.get(docname, [params], [callback])](#dbmultipartgetdocname-params-callback) | ||
- [attachments functions](#attachments-functions) | ||
@@ -325,2 +328,3 @@ - [db.attachment.insert(docname, attname, att, contenttype, [params], [callback])](#dbattachmentinsertdocname-attname-att-contenttype-params-callback) | ||
* `opts.encoding` – the encoding for attachments | ||
* `opts.multipart` – array of objects for multipart request | ||
@@ -436,2 +440,37 @@ ### nano.relax(opts, [callback]) | ||
## multipart functions | ||
### db.multipart.insert(doc, attachments, [params], [callback]) | ||
inserts a `doc` together with `attachments` and optional `params`. if params is a string, its assumed as the intended document name. if params is an object, its passed as query string parameters and `doc_name` is checked for defining the document name. | ||
refer to the [doc](http://wiki.apache.org/couchdb/HTTP_Document_API#Multiple_Attachments) for more details. | ||
`attachments` must be an array of objects with `name`, `data` and `content_type` properties. | ||
``` js | ||
var fs = require('fs'); | ||
fs.readFile('rabbit.png', function(err, data) { | ||
if (!err) { | ||
alice.multipart.insert({ foo: 'bar' }, [{name: 'rabbit.png', data: data, content_type: 'image/png'}], 'mydoc', function(err, body) { | ||
if (!err) | ||
console.log(body); | ||
}); | ||
} | ||
}); | ||
``` | ||
### db.multipart.get(docname, [params], [callback]) | ||
get `docname` together with its attachments via `multipart/related` request with optional query string additions | ||
`params`. refer to the | ||
[doc](http://wiki.apache.org/couchdb/HTTP_Document_API#Getting_Attachments_With_a_Document) for more details. | ||
the multipart response body is a `Buffer`. | ||
``` js | ||
alice.multipart.get('rabbit', function(err, buffer) { | ||
if (!err) | ||
console.log(buffer.toString()); | ||
}); | ||
``` | ||
## attachments functions | ||
@@ -438,0 +477,0 @@ |
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
177420
88
4128
771