From f2f3a7d403c7d7533191cd268d7d420634094e8f Mon Sep 17 00:00:00 2001 From: Orit Prince Date: Tue, 16 Aug 2016 14:54:16 +0300 Subject: [PATCH 1/5] enable retrieval of cloudant attachments --- 77-cloudant-cf.js | 56 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/77-cloudant-cf.js b/77-cloudant-cf.js index ff1d580..fbb8b6e 100644 --- a/77-cloudant-cf.js +++ b/77-cloudant-cf.js @@ -248,11 +248,27 @@ module.exports = function(RED) { if (node.search === "_id_") { var id = getDocumentId(msg.payload); + var attachmentName = getAttachementName(msg.payload); + var attachmentType = getAttachementType(msg.payload); node.inputId = id; - - db.get(id, function(err, body) { - sendDocumentOnPayload(err, body, msg); - }); + if (attachmentName){ + if (attachmentType){ + db.attachment.get(id, attachmentName, function(err, body) { + sendAttachementOnPayload(err, body, msg, attachmentType); + }); + }else{ + db.get(id, function(err, body) { + attachmentType = body._attachments[attachmentName]["content_type"]; + db.attachment.get(id, attachmentName, function(err, body) { + sendAttachementOnPayload(err, body, msg, attachmentType); + }); + }); + } + }else{ + db.get(id, function(err, body) { + sendDocumentOnPayload(err, body, msg); + }); + } } else if (node.search === "_idx_") { options.query = options.query || options.q || formatSearchQuery(msg.payload); @@ -283,7 +299,22 @@ module.exports = function(RED) { return payload; } - + function getAttachementName(payload) { + if (typeof payload === "object") { + if ("attachmentName" in payload) { + return payload.attachmentName; + } + } + return null; + } + function getAttachementType(payload) { + if (typeof payload === "object") { + if ("attachmentType" in payload) { + return payload.attachmentType; + } + } + return null; + } function formatSearchQuery(query) { if (typeof query === "object") { // useful when passing the query on HTTP params @@ -298,7 +329,20 @@ module.exports = function(RED) { } return query; } - + function sendAttachementOnPayload(err, body, msg, attachmentType){ + if (!err) { + msg.cloudant = body; + msg.payload = body; + msg.headers = { + "Content-Type" : attachmentType + } + } + else { + msg.payload = null; + node.error(err.description, err); + } + node.send(msg); + } function sendDocumentOnPayload(err, body, msg) { if (!err) { msg.cloudant = body; From d36de1bf9de527114bd22a492d8fe0f20b931fae Mon Sep 17 00:00:00 2001 From: Orit Prince Date: Sun, 9 Oct 2016 08:45:21 +0300 Subject: [PATCH 2/5] Fix for: No response on wrong Cloudant credentials. --- 77-cloudant-cf.js | 93 ++++++++++++++++++++++++++--------------------- 1 file changed, 52 insertions(+), 41 deletions(-) diff --git a/77-cloudant-cf.js b/77-cloudant-cf.js index fbb8b6e..8fe02d8 100644 --- a/77-cloudant-cf.js +++ b/77-cloudant-cf.js @@ -240,52 +240,63 @@ module.exports = function(RED) { }; Cloudant(credentials, function(err, cloudant) { - if (err) { node.error(err.description, err); } + if (err) { + node.error(err.description, err); + node.cloudantError = err; + } else { - node.on("input", function(msg) { - var db = cloudant.use(node.database); - var options = (typeof msg.payload === "object") ? msg.payload : {}; - - if (node.search === "_id_") { - var id = getDocumentId(msg.payload); - var attachmentName = getAttachementName(msg.payload); - var attachmentType = getAttachementType(msg.payload); - node.inputId = id; - if (attachmentName){ - if (attachmentType){ - db.attachment.get(id, attachmentName, function(err, body) { - sendAttachementOnPayload(err, body, msg, attachmentType); - }); - }else{ - db.get(id, function(err, body) { - attachmentType = body._attachments[attachmentName]["content_type"]; - db.attachment.get(id, attachmentName, function(err, body) { - sendAttachementOnPayload(err, body, msg, attachmentType); - }); - }); - } - }else{ - db.get(id, function(err, body) { - sendDocumentOnPayload(err, body, msg); - }); - } - } - else if (node.search === "_idx_") { - options.query = options.query || options.q || formatSearchQuery(msg.payload); - options.include_docs = options.include_docs || true; - options.limit = options.limit || 200; + node.cloudant = cloudant; + } + }); - db.search(node.design, node.index, options, function(err, body) { - sendDocumentOnPayload(err, body, msg); + node.on("input", function(msg) { + if (!node.cloudant){ + //msg.payload = node.cloudantError; + msg.payload = null; + node.error(node.cloudantError, msg); + return; + } + var db = cloudant.use(node.database); + var options = (typeof msg.payload === "object") ? msg.payload : {}; + + if (node.search === "_id_") { + var id = getDocumentId(msg.payload); + var attachmentName = getAttachementName(msg.payload); + var attachmentType = getAttachementType(msg.payload); + node.inputId = id; + if (attachmentName){ + if (attachmentType){ + db.attachment.get(id, attachmentName, function(err, body) { + sendAttachementOnPayload(err, body, msg, attachmentType); }); - } - else if (node.search === "_all_") { - options.include_docs = options.include_docs || true; - - db.list(options, function(err, body) { - sendDocumentOnPayload(err, body, msg); + }else{ + db.get(id, function(err, body) { + attachmentType = body._attachments[attachmentName]["content_type"]; + db.attachment.get(id, attachmentName, function(err, body) { + sendAttachementOnPayload(err, body, msg, attachmentType); + }); }); } + }else{ + db.get(id, function(err, body) { + sendDocumentOnPayload(err, body, msg); + }); + } + } + else if (node.search === "_idx_") { + options.query = options.query || options.q || formatSearchQuery(msg.payload); + options.include_docs = options.include_docs || true; + options.limit = options.limit || 200; + + db.search(node.design, node.index, options, function(err, body) { + sendDocumentOnPayload(err, body, msg); + }); + } + else if (node.search === "_all_") { + options.include_docs = options.include_docs || true; + + db.list(options, function(err, body) { + sendDocumentOnPayload(err, body, msg); }); } }); From 2c00811a5384a33dc7b220137b86fba56ffbb1b5 Mon Sep 17 00:00:00 2001 From: Orit Prince Date: Mon, 10 Oct 2016 07:39:28 +0300 Subject: [PATCH 3/5] Bug fix: reference error --- 77-cloudant-cf.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/77-cloudant-cf.js b/77-cloudant-cf.js index 8fe02d8..b5ad1a5 100644 --- a/77-cloudant-cf.js +++ b/77-cloudant-cf.js @@ -223,7 +223,7 @@ module.exports = function(RED) { function CloudantInNode(n) { RED.nodes.createNode(this,n); - + console.log("CloudantInNode new version"); this.cloudantConfig = _getCloudantConfig(n); this.database = _cleanDatabaseName(n.database, this); this.search = n.search; @@ -245,6 +245,7 @@ module.exports = function(RED) { node.cloudantError = err; } else { + node.log("CloudantInNode connection succeeded"); node.cloudant = cloudant; } }); @@ -256,7 +257,8 @@ module.exports = function(RED) { node.error(node.cloudantError, msg); return; } - var db = cloudant.use(node.database); + var db = node.cloudant.use(node.database); + var options = (typeof msg.payload === "object") ? msg.payload : {}; if (node.search === "_id_") { From 7f9a9064994edb79fcec30b9d740dafc247af657 Mon Sep 17 00:00:00 2001 From: Orit Prince Date: Thu, 3 Nov 2016 13:25:13 +0200 Subject: [PATCH 4/5] dev --- 77-cloudant-cf.js | 73 ++++++++++------------------------------------- 1 file changed, 15 insertions(+), 58 deletions(-) diff --git a/77-cloudant-cf.js b/77-cloudant-cf.js index b5ad1a5..9829d73 100644 --- a/77-cloudant-cf.js +++ b/77-cloudant-cf.js @@ -81,6 +81,7 @@ module.exports = function(RED) { this.operation = n.operation; this.payonly = n.payonly || false; this.database = _cleanDatabaseName(n.database, this); + console.log("CloudantOutNode this.database = " + this.database); this.cloudantConfig = _getCloudantConfig(n); var node = this; @@ -223,15 +224,17 @@ module.exports = function(RED) { function CloudantInNode(n) { RED.nodes.createNode(this,n); - console.log("CloudantInNode new version"); + this.cloudantConfig = _getCloudantConfig(n); this.database = _cleanDatabaseName(n.database, this); + console.log("CloudantInNode this.database = " + this.database); this.search = n.search; this.design = n.design; this.index = n.index; this.inputId = ""; var node = this; + node.log("cloudantConfig: " + JSON.stringify(node.cloudantConfig)); var credentials = { account: node.cloudantConfig.account, key: node.cloudantConfig.username, @@ -241,49 +244,29 @@ module.exports = function(RED) { Cloudant(credentials, function(err, cloudant) { if (err) { + node.DBError = err; node.error(err.description, err); - node.cloudantError = err; } else { - node.log("CloudantInNode connection succeeded"); node.cloudant = cloudant; } }); - node.on("input", function(msg) { if (!node.cloudant){ - //msg.payload = node.cloudantError; - msg.payload = null; - node.error(node.cloudantError, msg); + msg.payload = node.DBError; + node.error(node.DBError, msg); return; } var db = node.cloudant.use(node.database); - var options = (typeof msg.payload === "object") ? msg.payload : {}; if (node.search === "_id_") { var id = getDocumentId(msg.payload); - var attachmentName = getAttachementName(msg.payload); - var attachmentType = getAttachementType(msg.payload); node.inputId = id; - if (attachmentName){ - if (attachmentType){ - db.attachment.get(id, attachmentName, function(err, body) { - sendAttachementOnPayload(err, body, msg, attachmentType); - }); - }else{ - db.get(id, function(err, body) { - attachmentType = body._attachments[attachmentName]["content_type"]; - db.attachment.get(id, attachmentName, function(err, body) { - sendAttachementOnPayload(err, body, msg, attachmentType); - }); - }); - } - }else{ - db.get(id, function(err, body) { - sendDocumentOnPayload(err, body, msg); - }); - } + + db.get(id, function(err, body) { + sendDocumentOnPayload(err, body, msg); + }); } else if (node.search === "_idx_") { options.query = options.query || options.q || formatSearchQuery(msg.payload); @@ -312,22 +295,7 @@ module.exports = function(RED) { return payload; } - function getAttachementName(payload) { - if (typeof payload === "object") { - if ("attachmentName" in payload) { - return payload.attachmentName; - } - } - return null; - } - function getAttachementType(payload) { - if (typeof payload === "object") { - if ("attachmentType" in payload) { - return payload.attachmentType; - } - } - return null; - } + function formatSearchQuery(query) { if (typeof query === "object") { // useful when passing the query on HTTP params @@ -342,20 +310,7 @@ module.exports = function(RED) { } return query; } - function sendAttachementOnPayload(err, body, msg, attachmentType){ - if (!err) { - msg.cloudant = body; - msg.payload = body; - msg.headers = { - "Content-Type" : attachmentType - } - } - else { - msg.payload = null; - node.error(err.description, err); - } - node.send(msg); - } + function sendDocumentOnPayload(err, body, msg) { if (!err) { msg.cloudant = body; @@ -397,9 +352,11 @@ module.exports = function(RED) { // password for the Cloudant service at the top-level of the object function _getCloudantConfig(n) { if (n.service === "_ext_") { + console.log("get _ext_ configuration of " + n.cloudant); return RED.nodes.getNode(n.cloudant); } else if (n.service !== "") { + console.log("get cloud configuration of " + n.cloudant); var service = appEnv.getService(n.service); var cloudantConfig = { }; From 2cf88e080f80ebe5697de3b25f4d780d426ee9c7 Mon Sep 17 00:00:00 2001 From: Orit Prince Date: Thu, 3 Nov 2016 13:29:59 +0200 Subject: [PATCH 5/5] Dynamic DB name --- 77-cloudant-cf.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/77-cloudant-cf.js b/77-cloudant-cf.js index 9829d73..c22fb13 100644 --- a/77-cloudant-cf.js +++ b/77-cloudant-cf.js @@ -257,7 +257,9 @@ module.exports = function(RED) { node.error(node.DBError, msg); return; } - var db = node.cloudant.use(node.database); + //var db = node.cloudant.use(node.database); + var dbName = msg.payload.dbName || node.database; + var db = node.cloudant.use(dbName); var options = (typeof msg.payload === "object") ? msg.payload : {}; if (node.search === "_id_") {