Skip to content

Success response & status #10

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 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
25 changes: 23 additions & 2 deletions 77-cloudant-cf.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@
style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-payonly" style="width:65%;">Only store msg.payload object?</label>
</div>
<div class="form-row node-input-payonly">
<label>&nbsp;</label>
<input type="checkbox" id="node-input-successOutput" placeholder="Success Output"
style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-successOutput" style="width:65%;">Add a success output?</label>
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name" style="width:65%;">
Expand Down Expand Up @@ -140,7 +146,8 @@
database: { value:"", required:true, validate:validateDatabase },
service: { value:"", required:true },
payonly: { value:false },
operation: { value:"insert" }
operation: { value:"insert" },
successOutput: { value: false }
},
inputs: 1,
outputs: 0,
Expand All @@ -150,7 +157,15 @@
labelStyle: function() {
return this.name?"node_label_italic":"";
},
oneditprepare: oneditprepare
oneditprepare: function() {
var node = this;
$('#node-input-successOutput').change(function() {
node.outputs = $(this).is(':checked') ? 1 : 0;
});
if (node.successOutput)
node.outputs = 1
return oneditprepare.call(this)
}
});

RED.nodes.registerType("cloudant in", {
Expand Down Expand Up @@ -264,6 +279,12 @@
<code>msg</code> object itself or as an object in the
<code>msg.payload</code>.
</p>
<p>
It is also possible to have a success output. The <code>msg.payload</code>
will be de <code>body</code> of callback function. Errors are send throught
<code>node.error</code> (You'll have to catch them using the <i>catch</i>
mechanism)
</p>
<p>
The <b>database name</b> must follow these rules:
<ul>
Expand Down
80 changes: 56 additions & 24 deletions 77-cloudant-cf.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = function(RED) {

var MAX_ATTEMPTS = 3;

var appEnv = cfEnv.getAppEnv();
var appEnv = cfEnv.getAppEnv() || {services:{}};
var services = [];

// load the services bound to this application
Expand Down Expand Up @@ -91,22 +91,36 @@ module.exports = function(RED) {
url: node.cloudantConfig.url
};

Cloudant(credentials, function(err, cloudant) {
if (err) { node.error(err.description, err); }
else {
// check if the database exists and create it if it doesn't
createDatabase(cloudant, node);
}

node.on("input", function(msg) {
if (err) {
return node.error(err.description, err);
try {
Cloudant(credentials, function(err, cloudant) {
if (err) {
node.status({fill:"red", shape:"ring", text:"Failed connecting"});
node.error(err.description);
}
else {
// check if the database exists and create it if it doesn't
createDatabase(cloudant, node);
}

delete msg._msgid;
handleMessage(cloudant, node, msg);
node.on("input", function(msg) {
if (err) {
msg.error = err;
return node.error(err.description, msg);
}

delete msg._msgid;
handleMessage(cloudant, node, msg);
});
});
});
}
catch(e) {
node.status({fill:"red", shape:"ring", text:"Cannot connect"});
node.error(e);
node.on("input", function(msg) {
msg.error = e;
node.error("Cannot connect to database", msg);
});
}

function createDatabase(cloudant, node) {
cloudant.db.list(function(err, all_dbs) {
Expand All @@ -117,33 +131,45 @@ module.exports = function(RED) {
return;
}
node.error("Failed to list databases: " + err.description, err);
node.status({fill:"red", shape:"ring", text:"Cannot list databases"});
}
else {
if (all_dbs && all_dbs.indexOf(node.database) < 0) {
cloudant.db.create(node.database, function(err, body) {
if (err) {
node.status({fill:"red", shape:"ring", text:"Cannot create database"});
node.error(
"Failed to create database: " + err.description,
err
);
}
else {
node.status({fill:"green", shape:"ring", text:"Connected"});
}
});
}
else {
node.status({fill:"green", shape:"ring", text:"Connected"});
}
}
});
}

function handleMessage(cloudant, node, msg) {
if (node.operation === "insert") {
var msg = node.payonly ? msg.payload : msg;
var body = node.payonly ? msg.payload : msg;
var root = node.payonly ? "payload" : "msg";
var doc = parseMessage(msg, root);
var doc = parseMessage(body, root);

insertDocument(cloudant, node, doc, MAX_ATTEMPTS, function(err, body) {
if (err) {
console.trace();
console.log(node.error.toString());
msg.error = err;
node.error("Failed to insert document: " + err.description, msg);
} else {
msg.payload = body;
node.send(msg);
}
});
}
Expand All @@ -154,12 +180,17 @@ module.exports = function(RED) {
var db = cloudant.use(node.database);
db.destroy(doc._id, doc._rev, function(err, body) {
if (err) {
msg.error = err;
node.error("Failed to delete document: " + err.description, msg);
}
else {
msg.payload = body;
node.send(msg);
}
});
} else {
var err = new Error("_id and _rev are required to delete a document");
node.error(err.message, msg);
msg.error = new Error("_id and _rev are required to delete a document");
node.error(msg.err.message, msg);
}
}
}
Expand Down Expand Up @@ -350,14 +381,15 @@ module.exports = function(RED) {
return RED.nodes.getNode(n.cloudant);

} else if (n.service !== "") {
var service = appEnv.getService(n.service);
var cloudantConfig = { };
var service = appEnv.getService(n.service) || {};
var cloudantConfig = {};

var host = service.credentials.host;
var credentials = service.credentials || {};
var host = credentials.host || "";

cloudantConfig.username = service.credentials.username;
cloudantConfig.password = service.credentials.password;
cloudantConfig.account = host.substring(0, host.indexOf('.'));
cloudantConfig.username = credentials.username || "";
cloudantConfig.password = credentials.password || "";
cloudantConfig.account = host.substring(0, host.indexOf('.')) || "";

return cloudantConfig;
}
Expand Down