Skip to content

enable retrieval of cloudant attachments, fix for error handling upon wrong credentials (cloudant in only) #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 41 additions & 25 deletions 77-cloudant-cf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -226,12 +227,14 @@ module.exports = function(RED) {

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,
Expand All @@ -240,36 +243,47 @@ module.exports = function(RED) {
};

Cloudant(credentials, function(err, cloudant) {
if (err) { node.error(err.description, err); }
if (err) {
node.DBError = err;
node.error(err.description, err);
}
else {
node.on("input", function(msg) {
var db = cloudant.use(node.database);
var options = (typeof msg.payload === "object") ? msg.payload : {};
node.cloudant = cloudant;
}
});
node.on("input", function(msg) {
if (!node.cloudant){
msg.payload = node.DBError;
node.error(node.DBError, msg);
return;
}
//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_") {
var id = getDocumentId(msg.payload);
node.inputId = id;
if (node.search === "_id_") {
var id = getDocumentId(msg.payload);
node.inputId = id;

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.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.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);
});
}
db.list(options, function(err, body) {
sendDocumentOnPayload(err, body, msg);
});
}
});
Expand Down Expand Up @@ -340,9 +354,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 = { };

Expand Down