1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

move MongoDB delete function to output node as it's logically the end of a flow.

This commit is contained in:
Dave C-J 2013-09-25 12:04:12 +01:00
parent 021b4cd610
commit f23762ef9a
2 changed files with 45 additions and 36 deletions

View File

@ -25,6 +25,10 @@
<label for="node-config-input-db"><i class="icon-briefcase"></i> Database</label> <label for="node-config-input-db"><i class="icon-briefcase"></i> Database</label>
<input type="text" id="node-config-input-db" placeholder="test"> <input type="text" id="node-config-input-db" placeholder="test">
</div> </div>
<div class="form-row">
<label for="node-config-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-config-input-name" placeholder="Name">
</div>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
@ -35,9 +39,10 @@
hostname: { value:"127.0.0.1",required:true}, hostname: { value:"127.0.0.1",required:true},
port: { value: 27017,required:true}, port: { value: 27017,required:true},
db: { value:"",required:true}, db: { value:"",required:true},
name: { value:"" }
}, },
label: function() { label: function() {
return this.hostname+":"+this.port+"//"+this.db; return this.name||this.hostname+":"+this.port+"//"+this.db;
} }
}); });
</script> </script>
@ -53,6 +58,13 @@
<input type="text" id="node-input-collection" placeholder="collection"> <input type="text" id="node-input-collection" placeholder="collection">
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-operation"><i class="icon-wrench"></i> Operation</label>
<select type="text" id="node-input-operation" style="display: inline-block; vertical-align: top;">
<option value=store>Store</option>
<option value=delete>Delete</option>
</select>
</div>
<div class="form-row node-input-payonly">
<label>&nbsp;</label> <label>&nbsp;</label>
<input type="checkbox" id="node-input-payonly" placeholder="Only" style="display: inline-block; width: auto; vertical-align: top;"> <input type="checkbox" id="node-input-payonly" placeholder="Only" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-payonly" style="width: 70%;">Only store msg.payload object ?</label> <label for="node-input-payonly" style="width: 70%;">Only store msg.payload object ?</label>
@ -61,6 +73,13 @@
<label for="node-input-name"><i class="icon-tag"></i> Name</label> <label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name"> <input type="text" id="node-input-name" placeholder="Name">
</div> </div>
<script>
$("#node-input-operation").change(function() {
var id = $("#node-input-operation option:selected").val();
if (id == "delete") $(".node-input-payonly").hide();
else $(".node-input-payonly").show();
});
</script>
</script> </script>
<script type="text/x-red" data-help-name="mongodb out"> <script type="text/x-red" data-help-name="mongodb out">
@ -69,6 +88,8 @@
<p>If this is NOT the desired behaviour - ie you want repeated entries to overwrite, then you must set the <b>msg._id</b> property to be a constant by the use of a previous function node.</p> <p>If this is NOT the desired behaviour - ie you want repeated entries to overwrite, then you must set the <b>msg._id</b> property to be a constant by the use of a previous function node.</p>
<p>This could be a unique constant or you could create one based on some other msg property.</p> <p>This could be a unique constant or you could create one based on some other msg property.</p>
<p>Currently we do not limit or cap the collection size at all... this may well change.</p> <p>Currently we do not limit or cap the collection size at all... this may well change.</p>
<p>You can also choose to <b>remove</b> items. To do so the <b>msg.payload</b> <i>MUST</i> contain an object that will select the items(s) to remove.
A blank object will delete <i>all of the objects</i> in the collection. You have been warned...</p>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
@ -79,7 +100,8 @@
mongodb: { type:"mongodb",required:true}, mongodb: { type:"mongodb",required:true},
name: {value:""}, name: {value:""},
collection: {value:"",required:true}, collection: {value:"",required:true},
payonly: {value:false} payonly: {value:false},
operation: {value:"store"}
}, },
inputs:1, inputs:1,
outputs:0, outputs:0,
@ -87,7 +109,7 @@
align: "right", align: "right",
label: function() { label: function() {
var mongoNode = RED.nodes.node(this.mongodb); var mongoNode = RED.nodes.node(this.mongodb);
return this.name||this.collection||(mongoNode?mongoNode.label():"mongodb"); return this.name||(mongoNode?mongoNode.label()+"//"+this.collection:"mongodb");
}, },
labelStyle: function() { labelStyle: function() {
return this.name?"node_label_italic":""; return this.name?"node_label_italic":"";
@ -105,13 +127,6 @@
<label for="node-input-collection"><i class="icon-briefcase"></i> Collection</label> <label for="node-input-collection"><i class="icon-briefcase"></i> Collection</label>
<input type="text" id="node-input-collection" placeholder="collection"> <input type="text" id="node-input-collection" placeholder="collection">
</div> </div>
<div class="form-row">
<label for="node-input-operation"><i class="icon-wrench"></i> Operation</label>
<select type="text" id="node-input-operation" style="display: inline-block; width: auto; vertical-align: top;">
<option value=query>Query</option>
<option value=delete>Delete</option>
</select>
</div>
<div class="form-row"> <div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label> <label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name"> <input type="text" id="node-input-name" placeholder="Name">
@ -122,8 +137,6 @@
<p>Queries a MongoDB collection by using the <b>msg.payload</b> to be a MongoDB query statement as per the .find() function.</p> <p>Queries a MongoDB collection by using the <b>msg.payload</b> to be a MongoDB query statement as per the .find() function.</p>
<p>You may also (via a function) set a <b>msg.projection</b> object to constrain the returned fields, a <b>msg.sort</b> object and a <b>msg.limit</b> object.</p> <p>You may also (via a function) set a <b>msg.projection</b> object to constrain the returned fields, a <b>msg.sort</b> object and a <b>msg.limit</b> object.</p>
<p>All are optional - see the <a href="http://docs.mongodb.org/manual/reference/method/db.collection.find/" target="new"><i>MongoDB find docs</i></a> for examples.</p> <p>All are optional - see the <a href="http://docs.mongodb.org/manual/reference/method/db.collection.find/" target="new"><i>MongoDB find docs</i></a> for examples.</p>
<p>You can also choose to <b>remove</b> items. To do so the <b>msg.payload</b> <i>MUST</i> contain an object that will select the items(s) to remove.
A blank object will delete <i>all of the objects</i> in the collection. You have been warned...</p>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
@ -133,8 +146,7 @@
defaults: { defaults: {
mongodb: { type:"mongodb",required:true}, mongodb: { type:"mongodb",required:true},
name: {value:""}, name: {value:""},
collection: {value:"",required:true}, collection: {value:"",required:true}
operation: {value:"query"}
}, },
inputs:1, inputs:1,
outputs:1, outputs:1,

View File

@ -22,6 +22,7 @@ function MongoNode(n) {
this.hostname = n.hostname; this.hostname = n.hostname;
this.port = n.port; this.port = n.port;
this.db = n.db; this.db = n.db;
this.name = n.name;
} }
RED.nodes.registerType("mongodb",MongoNode); RED.nodes.registerType("mongodb",MongoNode);
@ -31,6 +32,7 @@ function MongoOutNode(n) {
this.collection = n.collection; this.collection = n.collection;
this.mongodb = n.mongodb; this.mongodb = n.mongodb;
this.payonly = n.payonly || false; this.payonly = n.payonly || false;
this.operation = n.operation;
this.mongoConfig = RED.nodes.getNode(this.mongodb); this.mongoConfig = RED.nodes.getNode(this.mongodb);
if (this.mongoConfig) { if (this.mongoConfig) {
@ -43,9 +45,15 @@ function MongoOutNode(n) {
if (err) { node.error(err); } if (err) { node.error(err); }
else { else {
node.on("input",function(msg) { node.on("input",function(msg) {
if (node.operation == "store") {
delete msg._topic; delete msg._topic;
if (node.payonly) coll.save(msg.payload,function(err,item){if (err){node.error(err);}}); if (node.payonly) coll.save(msg.payload,function(err,item){ if (err){node.error(err);} });
else coll.save(msg,function(err,item){if (err){node.error(err);}}); else coll.save(msg,function(err,item){if (err){node.error(err);}});
}
if (node.operation == "delete") {
console.log(msg.payload);
coll.remove(msg.payload, {w:1}, function(err, items){ if (err) node.error(err); });
}
}); });
} }
}); });
@ -67,7 +75,6 @@ MongoOutNode.prototype.close = function() {
function MongoInNode(n) { function MongoInNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.collection = n.collection; this.collection = n.collection;
this.operation = n.operation;
this.mongodb = n.mongodb; this.mongodb = n.mongodb;
this.mongoConfig = RED.nodes.getNode(this.mongodb); this.mongoConfig = RED.nodes.getNode(this.mongodb);
@ -81,7 +88,6 @@ function MongoInNode(n) {
if (err) { node.error(err); } if (err) { node.error(err); }
else { else {
node.on("input",function(msg) { node.on("input",function(msg) {
if (node.operation == "query") {
msg.projection = msg.projection || {}; msg.projection = msg.projection || {};
coll.find(msg.payload,msg.projection).sort(msg.sort).limit(msg.limit).toArray(function(err, items) { coll.find(msg.payload,msg.projection).sort(msg.sort).limit(msg.limit).toArray(function(err, items) {
if (err) { node.error(err); } if (err) { node.error(err); }
@ -91,15 +97,6 @@ function MongoInNode(n) {
delete msg.limit; delete msg.limit;
node.send(msg); node.send(msg);
}); });
}
if (node.operation == "delete") {
console.log(msg.payload);
coll.remove(msg.payload, {w:1}, function(err, items) {
if (err) node.error(err);
msg.payload = items;
node.send(msg);
});
}
}); });
} }
}); });