Allow user to select collection in mongo nodes using msg.collection

This commit is contained in:
Anna Thomas
2014-09-19 10:06:15 +01:00
parent 4f2e4b58e4
commit 23b5ac4582
2 changed files with 109 additions and 54 deletions

View File

@@ -40,21 +40,21 @@
</script>
<script type="text/javascript">
RED.nodes.registerType('mongodb',{
RED.nodes.registerType('mongodb', {
category: 'config',
color:"rgb(218, 196, 180)",
color: "rgb(218, 196, 180)",
defaults: {
hostname: { value:"127.0.0.1",required:true},
port: { value: 27017,required:true},
db: { value:"",required:true},
name: { value:"" }
hostname: {value: "127.0.0.1", required: true},
port: {value: 27017, required: true},
db: {value: "", required: true},
name: {value: ""}
},
credentials: {
user: {type:"text"},
user: {type: "text"},
password: {type: "password"}
},
label: function() {
return this.name||this.hostname+":"+this.port+"/"+this.db;
return this.name || this.hostname + ":" + this.port + "/" + this.db;
}
});
</script>
@@ -72,7 +72,7 @@
<div class="form-row">
<label for="node-input-operation"><i class="fa fa-wrench"></i> Operation</label>
<select type="text" id="node-input-operation" style="display: inline-block; vertical-align: top;">
<option value="save">save</option>
<option value="store">save</option>
<option value="insert">insert</option>
<option value="update">update</option>
<option value="delete">remove</option>
@@ -97,6 +97,8 @@
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-tips" id="node-warning" style="display: none"><b> Tip:</b> If no collection is set, ensure <b>msg.collection</b> will contain the collection name
</div>
</script>
<script type="text/x-red" data-help-name="mongodb out">
@@ -107,6 +109,7 @@
<p>Update will modify an existing object or objects. The query to select objects to update uses <b>msg.query</b> and the update to the element uses <b>msg.payload</b>.</p>
<p>Update can add a object if it does not exist or update multiple objects.</p>
<p>Remove will remove objects that match the query passed in on <b>msg.payload</b>. A blank query will delete <i>all of the objects</i> in the collection.</p>
<p>You can either set the collection method in the node config or on <b>msg.collection</b>. Setting it in the node will override <b>msg.collection</b>.</p>
<p>By default MongoDB creates an <i>_id</i> property as the primary key - so repeated injections of the same <b>msg</b> will result in many database entries.</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>
@@ -122,37 +125,45 @@
if (id === "update") {
$(".node-input-payonly").hide();
$(".node-input-upsert, .node-input-multi").show();
} else if (id == "delete") {
} else if (id === "delete") {
$(".node-input-payonly, .node-input-upsert, .node-input-multi").hide();
} else {
$(".node-input-payonly").show();
$(".node-input-upsert, .node-input-multi").hide();
}
});
$("#node-input-collection").change(function () {
if($("#node-input-collection").val() === "") {
$("#node-warning").show();
} else {
$("#node-warning").hide();
}
});
}
RED.nodes.registerType('mongodb out',{
RED.nodes.registerType('mongodb out', {
category: 'storage-output',
color:"rgb(218, 196, 180)",
color: "rgb(218, 196, 180)",
defaults: {
mongodb: {type: "mongodb", required: true},
name: {value: ""},
collection: {value: "", required: true},
collection: {value: ""},
payonly: {value: false},
upsert: {value: false},
multi: {value: false},
operation: {value: "save"}
operation: {value: "store"}
},
inputs:1,
outputs:0,
inputs: 1,
outputs: 0,
icon: "mongodb.png",
align: "right",
label: function() {
var mongoNode = RED.nodes.node(this.mongodb);
return this.name||(mongoNode?mongoNode.label()+" "+this.collection:"mongodb");
return this.name || (mongoNode ? mongoNode.label() + " " + this.collection: "mongodb");
},
labelStyle: function() {
return this.name?"node_label_italic":"";
return this.name ? "node_label_italic" : "";
},
oneditprepare: oneditprepare
});
@@ -180,6 +191,8 @@
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-tips" id="node-warning" style="display: none"><b> Tip:</b> If no collection is set, ensure <b>msg.collection</b> will contain the collection name
</div>
</script>
<script type="text/x-red" data-help-name="mongodb in">
@@ -187,29 +200,32 @@
<p>Find queries a collection using the <b>msg.payload</b> as the query statement as per the .find() function. Optionally, 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>Count returns a count of the number of documents in a collection or matching a query using the <b>msg.payload</b> as the query statement.</p>
<p>Aggregate provides access to the aggregation pipeline using the <b>msg.payload</b> as the pipeline array.</p>
<p>You can override the collection the method is performed on by setting <b>msg.collection</b> to the desired collection name.</p>
<p>See the <a href="http://docs.mongodb.org/manual/reference/method/db.collection.find/" target="new"><i>MongoDB collection methods docs</i></a> for examples.</p>
<p>The result is returned in <b>msg.payload</b>.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('mongodb in',{
RED.nodes.registerType('mongodb in', {
category: 'storage-input',
color:"rgb(218, 196, 180)",
color: "rgb(218, 196, 180)",
defaults: {
mongodb: { type:"mongodb",required:true},
name: {value:""},
collection: {value:"",required:true},
operation: {value:"find"}
mongodb: {type: "mongodb", required: true},
name: {value: ""},
collection: {value: ""},
operation: {value: "find"}
},
inputs:1,
outputs:1,
inputs: 1,
outputs: 1,
icon: "mongodb.png",
label: function() {
var mongoNode = RED.nodes.node(this.mongodb);
return this.name||(mongoNode?mongoNode.label()+" "+this.collection:"mongodb");
return this.name || (mongoNode ? mongoNode.label() + " " + this.collection: "mongodb");
},
labelStyle: function() {
return this.name?"node_label_italic":"";
}
return this.name ? "node_label_italic" : "";
},
oneditprepare: oneditprepare
});
</script>