Skip to content

Added output message flow after insert or update #8

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 1 commit 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
26 changes: 24 additions & 2 deletions 77-cloudant-cf.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@
<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%;">
</div>
<div class="form-row">
<label for="node-input-outputmsg"><i class="fa fa-wrench"></i>output msg</label>
<select type="text" id="node-input-outputmsg" style="width:68%;">
<option value="none">none</option>
<option value="error">error</option>
<option value="success">success</option>
<option value="all">all</option>
</select>
<script>
$("#node-input-operation").change(function() {
var id = $("#node-input-operation option:selected").val();
Expand Down Expand Up @@ -140,7 +148,8 @@
database: { value:"", required:true, validate:validateDatabase },
service: { value:"", required:true },
payonly: { value:false },
operation: { value:"insert" }
operation: { value:"insert" },
outputmsg: { value:"none" }
},
inputs: 1,
outputs: 0,
Expand All @@ -150,7 +159,12 @@
labelStyle: function() {
return this.name?"node_label_italic":"";
},
oneditprepare: oneditprepare
oneditprepare: oneditprepare,
oneditsave: function() {
var oval = $("#node-input-outputmsg").val();
if (oval == "none") this.outputs = 0;
else this.outputs = 1;
}
});

RED.nodes.registerType("cloudant in", {
Expand Down Expand Up @@ -272,6 +286,14 @@
<li>The first character can't be <code>_</code></li>
</ul>
</p>
<p>
You can specify that an output message is generated - either always (all) or for success or error.
In the event of an error the message output will be the original
message with a dbError field showing the error. For successful inserts or updates,
the message output will be the original message with the _id and _rev fields
updated in either the message body or in the payload.
The default setting is that no output message is generated.
</p>
<p>
Your document should avoid having top-level fields that start with
<code>_</code>, with exceptions for <code>_id</code>, <code>_rev</code>
Expand Down
34 changes: 33 additions & 1 deletion 77-cloudant-cf.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ module.exports = function(RED) {

this.operation = n.operation;
this.payonly = n.payonly || false;
this.outputMsg = n.outputmsg || "none";
this.database = _cleanDatabaseName(n.database, this);
this.cloudantConfig = _getCloudantConfig(n);

Expand All @@ -103,7 +104,6 @@ module.exports = function(RED) {
return node.error(err.description, err);
}

delete msg._msgid;
handleMessage(cloudant, node, msg);
});
});
Expand Down Expand Up @@ -134,7 +134,10 @@ module.exports = function(RED) {
}

function handleMessage(cloudant, node, msg) {
var origMsgId = msg._msgid ;
delete msg._msgid;
if (node.operation === "insert") {
var origMsg = msg ;
var msg = node.payonly ? msg.payload : msg;
var root = node.payonly ? "payload" : "msg";
var doc = parseMessage(msg, root);
Expand All @@ -144,6 +147,35 @@ module.exports = function(RED) {
console.trace();
console.log(node.error.toString());
node.error("Failed to insert document: " + err.description, msg);
if (node.outputMsg && (node.outputMsg == "error"
|| node.outputMsg == "all")) {
//send original message on error
origMsg._msgid = origMsgId ;
origMsg.dbError = {} ;

origMsg.dbError.statusCode = err.statusCode;
origMsg.dbError.description = err.description;
origMsg.dbError.message = err.message;
origMsg.dbError.reason = err.reason;
origMsg.dbError.scope = err.scope;
node.send([origMsg]);
}
}
else if (node.outputMsg && (node.outputMsg == "success"
|| node.outputMsg == "all")) {
// send message with updated id and rev on success
origMsg._msgid = origMsgId ;
delete origMsg.dbError; // just in case original message came from error path
if (node.payonly) {
origMsg.payload._id = body.id;
origMsg.payload._rev = body.rev;
}
else {
origMsg._id = body.id;
origMsg._rev = body.rev;
}

node.send([origMsg]);
}
});
}
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ in JSON format, it will be transformed before being stored.
For **update** and **delete**, you must pass the `_id` and the `_rev`as part
of the input `msg` object.

For **insert** and **update**, you can specify that an output message is generated - either
always (all) or for success or error. In the event of an error the message output will be the original
message with a dbError field showing the error. For successful inserts or updates, the message output
will be the original message with the _id and _rev fields updated in either the message body or in the payload.

To **search** for a document you have two options: get a document directly by
its `_id` or use an existing [search index](https://cloudant.com/for-developers/search/)
from the database. For both cases, the query should be passed in the
Expand Down