Permalink
Browse files

Added Attachments

  • Loading branch information...
dscape committed Aug 17, 2011
1 parent c75b097 commit 783a965f173a3f9c1a78adde3b794ca85efee21d
View
@@ -90,6 +90,9 @@ that's it. don't forget to delete the database you created:
`doc.get(doc_name,params*,callback*)`
`doc.bulk(docs,callback*)`
`doc.list(params*,callback*)`
+`doc.attachment.insert(doc_name,att_name,att,content_type,params*,callback*)`
+`doc.attachment.get(doc_name,att_name,params*,callback*)`
+`doc.attachment.destroy(doc_name,att_name,rev,callback*)`
#### aliases
View
42 nano.js
@@ -341,6 +341,12 @@ module.exports = exports = nano = function database_module(cfg) {
* Inserting an attachment
* http://wiki.apache.org/couchdb/HTTP_Document_API
*
+ * e.g.
+ * db.attachment.insert("new", "att", buffer, "image/bmp", {rev: b.rev},
+ * function(_,_,response) {
+ * console.log(response);
+ * });
+ *
* Don't forget that params.rev is required in all cases except when
* creating a new document with a new attachment via this method
*
@@ -361,6 +367,38 @@ module.exports = exports = nano = function database_module(cfg) {
, doc: doc_name, params: params, body: att},callback);
}
+ /*
+ * 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
+ *
+ * @see relax
+ */
+ function get_att(doc_name,att_name,params,callback) {
+ if(typeof params === "function") {
+ callback = params;
+ params = {};
+ }
+ relax({ db: db_name, att: att_name, method: "GET"
+ , doc: doc_name, params: params},callback);
+ }
+
+ /*
+ * 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
+ *
+ * @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);
+ }
+
public_functions = { info: function(cb) { get_db(db_name,cb); }
, replicate: function(target,continuous,cb) {
if(typeof continuous === "function") {
@@ -381,8 +419,8 @@ module.exports = exports = nano = function database_module(cfg) {
, list: list_docs
//, views: {}
, attachment: { insert: insert_att
- // , get: get_att
- // , destroy: destroy_att
+ , get: get_att
+ , destroy: destroy_att
}
};
return public_functions;
View
@@ -1,10 +1,10 @@
{ "name": "nano"
, "description": "NanoCouch is a minimalistic driver for CouchDB built on mikeals/request"
, "homepage": "http://github.com/dscape/nano"
-, "version": "0.3.2"
+, "version": "0.4.0"
, "author": "Nuno Job <nunojobpinto@gmail.com> (http://nunojob.com)"
, "keywords": ["CouchDB", "data", "request", "json", "nosql", "micro", "nano"]
, "devDependencies": { "async": "~0.1.9", "vows": "~0.5.10" }
, "main": "./nano.js"
-, "engines" : { "node" : "~v0.4.8" }
+, "engines" : { "node": ">=0.3.6" }
}
View
@@ -0,0 +1,33 @@
+var vows = require('vows')
+ , assert = require('assert')
+ , async = require('async')
+ , cfg = require('../../cfg/tests.js')
+ , nano = require('../../nano')(cfg);
+
+function db_name(i) { return "att_de" + i; }
+function db(i) { return nano.use(db_name(i)); }
+
+/*****************************************************************************
+ * att_des *
+ *****************************************************************************/
+function att_des(callback) {
+ nano.db.create(db_name("a"), function () {
+ db("a").attachment.insert("new", "att", "Hello World!", "text/plain",
+ function (e,h,b) {
+ db("a").attachment.destroy("new", "att", b.rev, callback);
+ });
+ });
+}
+
+function att_des_ok(e,h,b) {
+ nano.db.destroy(db_name("a"));
+ assert.isNull(e);
+ assert.ok(b.ok);
+ assert.equal(b.id, "new");
+}
+
+vows.describe('attachment.destroy').addBatch({
+ "att_del": {
+ topic: function () { att_des(this.callback); }
+ , "=": att_des_ok }
+}).exportTo(module);
View
@@ -0,0 +1,39 @@
+var vows = require('vows')
+ , assert = require('assert')
+ , async = require('async')
+ , cfg = require('../../cfg/tests.js')
+ , nano = require('../../nano')(cfg)
+ , pixel = "Qk06AAAAAAAAADYAAAAoAAAAAQAAAP////8BABgAAAAAAAAAAAATCwAAEwsAAAAAAAAAAAAAWm2CAA==";
+
+function db_name(i) { return "att_ge" + i; }
+function db(i) { return nano.use(db_name(i)); }
+
+/*****************************************************************************
+ * att_get *
+ *****************************************************************************/
+function att_get(callback) {
+ var buffer = new Buffer(pixel, 'base64');
+ nano.db.create(db_name("a"), function () {
+ db("a").attachment.insert("new", "att", "Hello", "text/plain",
+ function(_,_,b) {
+ db("a").attachment.insert("new", "att", buffer, "image/bmp", {rev: b.rev},
+ function (e,_,b) {
+ if(e) { callback(e); }
+ db("a").attachment.get("new", "att", {rev: b.rev}, callback);
+ });
+ });
+ });
+}
+
+function att_get_ok(e,h,b) {
+ nano.db.destroy(db_name("a"));
+ var buffer = new Buffer(b, "binary");
+ assert.isNull(e);
+ assert.equal(buffer.toString('base64'), pixel);
+}
+
+vows.describe('attachment.get').addBatch({
+ "att_get": {
+ topic: function () { att_get(this.callback); }
+ , "=": att_get_ok }
+}).exportTo(module);
View
@@ -1,4 +1,4 @@
-var vows = require('/usr/lib/node_modules/vows/lib/vows')
+var vows = require('vows')
, assert = require('assert')
, async = require('async')
, cfg = require('../../cfg/tests.js')
@@ -10,26 +10,21 @@ function db(i) { return nano.use(db_name(i)); }
/*****************************************************************************
* att_new_doc *
*****************************************************************************/
-function att_doc(callback) {
+function att_new_doc(callback) {
nano.db.create(db_name("a"), function () {
db("a").attachment.insert("new", "att", "Hello World!", "text/plain", callback);
});
}
-function att_doc_ok(e,h,b) {
+function att_new_doc_ok(e,h,b) {
nano.db.destroy(db_name("a"));
assert.isNull(e);
assert.ok(b.ok);
assert.equal(b.id, "new");
}
-/*****************************************************************************
- * att_doc *
- *****************************************************************************/
- // Same but on existing doc with rev
-
vows.describe('attachment.insert').addBatch({
- "att_doc": {
- topic: function () { att_doc(this.callback); }
- , "=": att_doc_ok }
+ "att_new_doc": {
+ topic: function () { att_new_doc(this.callback); }
+ , "=": att_new_doc_ok }
}).exportTo(module);
View
@@ -0,0 +1,37 @@
+var vows = require('vows')
+ , assert = require('assert')
+ , async = require('async')
+ , cfg = require('../../cfg/tests.js')
+ , nano = require('../../nano')(cfg);
+
+function db_name(i) { return "att_up" + i; }
+function db(i) { return nano.use(db_name(i)); }
+
+/*****************************************************************************
+ * att_doc *
+ *****************************************************************************/
+function att_doc(callback) {
+ var pixel = "Qk06AAAAAAAAADYAAAAoAAAAAQAAAP////8BABgAAAAAAAAAAAATCwAAEwsAAAAAAAAAAAAAWm2CAA=="
+ , buffer = new Buffer(pixel, 'base64');
+ nano.db.create(db_name("b"), function () {
+ db("b").attachment.insert("new", "att", "Hello World!", "text/plain",
+ function (e,_,b) {
+ if(e) { callback(e); }
+ db("b").attachment.insert("new", "att", buffer, "image/bmp", {rev: b.rev},
+ callback);
+ });
+ });
+}
+
+function att_doc_ok(e,h,b) {
+ nano.db.destroy(db_name("b"));
+ assert.isNull(e);
+ assert.ok(b.ok);
+ assert.equal(b.id, "new");
+}
+
+vows.describe('attachment.update').addBatch({
+ "att_doc": {
+ topic: function () { att_doc(this.callback); }
+ , "=": att_doc_ok }
+}).exportTo(module);
View
@@ -1,4 +1,4 @@
-var vows = require('/usr/lib/node_modules/vows/lib/vows')
+var vows = require('vows')
, assert = require('assert')
, async = require('async')
, cfg = require('../../cfg/tests.js')
View
@@ -1,4 +1,4 @@
-var vows = require('/usr/lib/node_modules/vows/lib/vows')
+var vows = require('vows')
, assert = require('assert')
, cfg = require('../../cfg/tests.js')
, nano = require('../../nano')(cfg);
View
@@ -1,4 +1,4 @@
-var vows = require('/usr/lib/node_modules/vows/lib/vows')
+var vows = require('vows')
, assert = require('assert')
, cfg = require('../../cfg/tests.js')
, nano = require('../../nano')(cfg);
View
@@ -1,4 +1,4 @@
-var vows = require('/usr/lib/node_modules/vows/lib/vows')
+var vows = require('vows')
, assert = require('assert')
, cfg = require('../../cfg/tests.js')
, nano = require('../../nano')(cfg);
View
@@ -1,4 +1,4 @@
-var vows = require('/usr/lib/node_modules/vows/lib/vows')
+var vows = require('vows')
, assert = require('assert')
, cfg = require('../../cfg/tests.js')
, nano = require('../../nano')(cfg);
View
@@ -1,4 +1,4 @@
-var vows = require('/usr/lib/node_modules/vows/lib/vows')
+var vows = require('vows')
, assert = require('assert')
, async = require('async')
, cfg = require('../../cfg/tests.js')
View
@@ -1,4 +1,4 @@
-var vows = require('/usr/lib/node_modules/vows/lib/vows')
+var vows = require('vows')
, assert = require('assert')
, async = require('async')
, cfg = require('../../cfg/tests.js')
View
@@ -1,4 +1,4 @@
-var vows = require('/usr/lib/node_modules/vows/lib/vows')
+var vows = require('vows')
, assert = require('assert')
, cfg = require('../../cfg/tests.js')
, nano = require('../../nano')(cfg)
View
@@ -1,4 +1,4 @@
-var vows = require('/usr/lib/node_modules/vows/lib/vows')
+var vows = require('vows')
, assert = require('assert')
, cfg = require('../../cfg/tests.js')
, nano = require('../../nano')(cfg)
View
@@ -1,4 +1,4 @@
-var vows = require('/usr/lib/node_modules/vows/lib/vows')
+var vows = require('vows')
, assert = require('assert')
, cfg = require('../../cfg/tests.js')
, nano = require('../../nano')(cfg)
View
@@ -1,4 +1,4 @@
-var vows = require('/usr/lib/node_modules/vows/lib/vows')
+var vows = require('vows')
, assert = require('assert')
, async = require('async')
, cfg = require('../../cfg/tests.js')
View
@@ -1,4 +1,4 @@
-var vows = require('/usr/lib/node_modules/vows/lib/vows')
+var vows = require('vows')
, assert = require('assert')
, cfg = require('../../cfg/tests.js')
, nano = require('../../nano')(cfg)

1 comment on commit 783a965

@dscape

This comment has been minimized.

Show comment Hide comment
@dscape

dscape Aug 17, 2011

Collaborator

fixes #3

Collaborator

dscape commented on 783a965 Aug 17, 2011

fixes #3

Please sign in to comment.