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

Updated mongodb nodes : Merged IN and OUT nodes + every operation emits an output with the query result or an error object

Discussed in https://discourse.nodered.org/t/mongodb-node-improvements/4442
This commit is contained in:
Kévin Michelet 2019-02-04 19:21:46 +01:00
parent 81781539f7
commit 44448bd3f0
2 changed files with 271 additions and 348 deletions

View File

@ -1,5 +1,4 @@
<script type="text/x-red" data-template-name="mongodb">
<script type="text/x-red" data-template-name="mongodb-config">
<div class="form-row">
<label for="node-config-input-hostname"><i class="fa fa-bookmark"></i> <span data-i18n="mongodb.label.host"></span></label>
<input class="input-append-left" type="text" id="node-config-input-hostname" placeholder="localhost" style="width: 40%;" >
@ -25,27 +24,27 @@
</script>
<script type="text/javascript">
RED.nodes.registerType('mongodb', {
RED.nodes.registerType('mongodb-config', {
category: 'config',
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"},
password: {type: "password"}
user: { type: "text" },
password: { type: "password" }
},
label: function() {
label: function () {
return this.name || this.hostname + ":" + this.port + "/" + this.db;
}
});
</script>
<script type="text/x-red" data-template-name="mongodb out">
<script type="text/x-red" data-template-name="mongodb">
<div class="form-row">
<label for="node-input-mongodb"><i class="fa fa-bookmark"></i> <span data-i18n="mongodb.label.server"></span></label>
<input type="text" id="node-input-mongodb">
@ -57,7 +56,10 @@
<div class="form-row">
<label for="node-input-operation"><i class="fa fa-wrench"></i> <span data-i18n="mongodb.label.operation"></span></label>
<select type="text" id="node-input-operation" style="display: inline-block; vertical-align: top;">
<option value="store" data-i18n="mongodb.operation.save"></option>
<option value="find" data-i18n="mongodb.operation.find"></option>
<option value="count" data-i18n="mongodb.operation.count"></option>
<option value="aggregate" data-i18n="mongodb.operation.aggregate"></option>
<option value="store" data-i18n="mongodb.operation.save"></option>
<option value="insert" data-i18n="mongodb.operation.insert"></option>
<option value="update" data-i18n="mongodb.operation.update"></option>
<option value="delete" data-i18n="mongodb.operation.remove"></option>
@ -68,7 +70,7 @@
<input type="checkbox" id="node-input-payonly" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-payonly" style="width: 70%;"><span data-i18n="mongodb.label.onlystore"></span></label>
</div>
<div class="form-row node-input-upsert">
<div class="form-row node-input-upsert">
<label>&nbsp;</label>
<input type="checkbox" id="node-input-upsert" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-upsert" style="width: 70%;"><span data-i18n="mongodb.label.createnew"></span></label>
@ -85,29 +87,51 @@
<div class="form-tips" id="node-warning" style="display: none"><span data-i18n="[html]mongodb.tip"></span></div>
</script>
<script type="text/x-red" data-help-name="mongodb out">
<p>A simple MongoDB output node. Can save, insert, update and remove objects from a chosen collection.</p>
<p>Save will update an existing object or insert a new object if one does not already exist.</p>
<p>Insert will insert a new object.</p>
<p>Save and insert either store <code>msg</code> or <code>msg.payload</code>.</p>
<p>Update will modify an existing object or objects. The query to select objects to update uses <code>msg.query</code>,
<script type="text/x-red" data-help-name="mongodb">
<p>A simple MongoDB node. Can manipulate a chosen collection.</p>
<p>Find queries a collection using the <code>msg.payload</code> as the query statement as per the .find() function.
Optionally, you may also set a <code>msg.projection</code> object (via a function) to constrain the returned
fields. You can also set a <code>msg.sort</code> object, a <code>msg.limit</code> number and a <code>msg.skip</code> number.</p>
<p>Count returns a count of the number of documents in a collection, or matches a query using the
<code>msg.payload</code> as the query statement.</p>
<p>Aggregate provides access to the aggregation pipeline using the <code>msg.payload</code> as the pipeline array.</p>
<p>Save will update an existing object or insert a new object if one does not already exist.</p>
<p>Insert will insert a new object.</p>
<p>Update will modify an existing object or objects. The query to select objects to update uses <code>msg.query</code>,
and the update to the element uses <code>msg.payload</code>. If <code>msg.query._id</code> is
a valid mongo ObjectId string it will be converted to an ObjectId type.</p>
<p>Update can add an object if it does not exist or update multiple objects.</p>
<p>Remove will remove objects that match the query passed in on <code>msg.payload</code>. A blank query will delete
<i>all of the objects</i> in the collection.</p>
<i>all of the objects</i> in the collection.</p>
<p>You can either set the collection method in the node config or on <code>msg.collection</code>. Setting it in the
node will override <code>msg.collection</code>.</p>
node will override <code>msg.collection</code>.</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 <code>msg.payload</code>.</p>
<p>By default, MongoDB creates an <i>_id</i> property as the primary key, so repeated injections of the
same <code>msg</code> will result in many database entries.</p>
same <code>msg</code> will result in many database entries.</p>
<p>If this is NOT the desired behaviour, i.e., you want repeated entries to overwrite, then you must set
the <code>msg._id</code> property to be a constant by the use of a previous function node.</p>
the <code>msg._id</code> 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>Currently we do not limit or cap the collection size, however this may well change.</p>
</script>
<script type="text/javascript">
function oneditprepare() {
$("#node-input-operation").change(function () {
var id = $("#node-input-operation option:selected").val();
@ -124,7 +148,7 @@
});
$("#node-input-collection").change(function () {
if($("#node-input-collection").val() === "") {
if ($("#node-input-collection").val() === "") {
$("#node-warning").show();
} else {
$("#node-warning").hide();
@ -132,94 +156,28 @@
});
}
RED.nodes.registerType('mongodb out', {
category: 'storage-output',
RED.nodes.registerType('mongodb', {
category: 'storage',
color: "rgb(218, 196, 180)",
defaults: {
mongodb: {type: "mongodb", required: true},
name: {value: ""},
collection: {value: ""},
payonly: {value: false},
upsert: {value: false},
multi: {value: false},
operation: {value: "store"}
},
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");
},
labelStyle: function() {
return this.name ? "node_label_italic" : "";
},
oneditprepare: oneditprepare
});
</script>
<script type="text/x-red" data-template-name="mongodb in">
<div class="form-row">
<label for="node-input-mongodb"><i class="fa fa-bookmark"></i> <span data-i18n="mongodb.label.server"></span></label>
<input type="text" id="node-input-mongodb">
</div>
<div class="form-row">
<label for="node-input-collection"><i class="fa fa-briefcase"></i> <span data-i18n="mongodb.label.collection"></span></label>
<input type="text" id="node-input-collection">
</div>
<div class="form-row">
<label for="node-input-operation"><i class="fa fa-wrench"></i> <span data-i18n="mongodb.label.operation"></span></label>
<select type="text" id="node-input-operation" style="display: inline-block; vertical-align: top;">
<option value="find" data-i18n="mongodb.operation.find"></option>
<option value="count" data-i18n="mongodb.operation.count"></option>
<option value="aggregate" data-i18n="mongodb.operation.aggregate"></option>
</select>
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="node-red:common.label.name"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]node-red:common.label.name">
</div>
<div class="form-tips" id="node-warning" style="display: none"><span data-i18n="[html]mongodb.tip"></span></div>
</script>
<script type="text/x-red" data-help-name="mongodb in">
<p>Calls a MongoDB collection method based on the selected operator.</p>
<p>Find queries a collection using the <code>msg.payload</code> as the query statement as per the .find() function.
Optionally, you may also set a <code>msg.projection</code> object (via a function) to constrain the returned
fields. You can also set a <code>msg.sort</code> object, a <code>msg.limit</code> number and a <code>msg.skip</code> number.</p>
<p>Count returns a count of the number of documents in a collection, or matches a query using the
<code>msg.payload</code> as the query statement.</p>
<p>Aggregate provides access to the aggregation pipeline using the <code>msg.payload</code> as the pipeline array.</p>
<p>You can either set the collection method in the node config or on <code>msg.collection</code>. Setting it in
the node will override <code>msg.collection</code>.</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 <code>msg.payload</code>.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('mongodb in', {
category: 'storage-input',
color: "rgb(218, 196, 180)",
defaults: {
mongodb: {type: "mongodb", required: true},
name: {value: ""},
collection: {value: ""},
operation: {value: "find"}
mongodb: { type: "mongodb-config", required: true },
name: { value: "" },
collection: { value: "" },
payonly: { value: false },
operation: { value: "find" },
upsert: { value: false },
multi: { value: false }
},
inputs: 1,
outputs: 1,
icon: "mongodb.png",
label: function() {
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() {
labelStyle: function () {
return this.name ? "node_label_italic" : "";
},
oneditprepare: oneditprepare
});
</script>
</script>

View File

@ -1,272 +1,237 @@
module.exports = function (RED) {
'use strict'
const mongo = require('mongodb')
const ObjectID = require('mongodb').ObjectID
const MongoClient = mongo.MongoClient
module.exports = function(RED) {
"use strict";
var mongo = require('mongodb');
var ObjectID = require('mongodb').ObjectID;
var MongoClient = mongo.MongoClient;
function MongoConfigNode(n) {
RED.nodes.createNode(this, n)
this.hostname = n.hostname
this.port = n.port
this.db = n.db
this.name = n.name
function MongoNode(n) {
RED.nodes.createNode(this,n);
this.hostname = n.hostname;
this.port = n.port;
this.db = n.db;
this.name = n.name;
var url = "mongodb://";
var url = 'mongodb://'
if (this.credentials && this.credentials.user && this.credentials.password) {
url += this.credentials.user+":"+this.credentials.password+"@";
url += this.credentials.user + ':' + this.credentials.password + '@'
}
url += this.hostname+":"+this.port+"/"+this.db;
url += this.hostname + ':' + this.port + '/' + this.db
this.url = url;
this.url = url
}
RED.nodes.registerType("mongodb",MongoNode,{
RED.nodes.registerType('mongodb-config', MongoConfigNode, {
credentials: {
user: {type:"text"},
password: {type: "password"}
user: { type: 'text' },
password: { type: 'password' }
}
});
})
function ensureValidSelectorObject(selector) {
if (selector != null && (typeof selector != 'object' || Buffer.isBuffer(selector))) {
return {};
return {}
}
return selector;
return selector
}
function MongoOutNode(n) {
RED.nodes.createNode(this,n);
this.collection = n.collection;
this.mongodb = n.mongodb;
function MongoNode(n) {
RED.nodes.createNode(this, n)
this.collection = n.collection
this.mongodb = n.mongodb
this.payonly = n.payonly || false;
this.operation = n.operation || 'find'
this.mongoConfig = RED.nodes.getNode(this.mongodb)
this.status({ fill: 'grey', shape: 'ring', text: RED._('mongodb.status.connecting') })
var node = this
var noerror = true
this.upsert = n.upsert || false;
this.multi = n.multi || false;
this.operation = n.operation;
this.mongoConfig = RED.nodes.getNode(this.mongodb);
this.status({fill:"grey",shape:"ring",text:RED._("mongodbstatus.connecting")});
var node = this;
var noerror = true;
var connectToDB = function() {
MongoClient.connect(node.mongoConfig.url, function(err, db) {
if (err) {
node.status({fill:"red",shape:"ring",text:RED._("mongodb.status.error")});
if (noerror) { node.error(err); }
noerror = false;
node.tout = setTimeout(connectToDB, 10000);
}
else {
node.status({fill:"green",shape:"dot",text:RED._("mongodb.status.connected")});
node.clientDb = db;
noerror = true;
var coll;
if (node.collection) {
coll = db.collection(node.collection);
var connectToDB = function () {
MongoClient.connect(
node.mongoConfig.url,
function (err, db) {
if (err) {
node.status({ fill: 'red', shape: 'ring', text: RED._('mongodb.status.error') })
if (noerror) {
node.error(err)
}
noerror = false
node.tout = setTimeout(connectToDB, 10000)
} else {
node.status({ fill: 'green', shape: 'dot', text: RED._('mongodb.status.connected') })
node.clientDb = db
noerror = true
var coll
if (node.collection) {
coll = db.collection(node.collection)
}
node.on('input', function (msg) {
if (!node.collection) {
if (msg.collection) {
coll = db.collection(msg.collection)
} else {
node.error(RED._('mongodb.errors.nocollection'))
return
}
} else {
coll = db.collection(node.collection)
}
var selector
if (node.operation === 'find') {
msg.projection = msg.projection || {}
selector = ensureValidSelectorObject(msg.payload)
var limit = msg.limit
if (typeof limit === 'string' && !isNaN(limit)) {
limit = Number(limit)
} else if (typeof limit === 'undefined') {
limit = 0
}
var skip = msg.skip
if (typeof skip === 'string' && !isNaN(skip)) {
skip = Number(skip)
} else if (typeof skip === 'undefined') {
skip = 0
}
coll
.find(selector, msg.projection)
.sort(msg.sort)
.limit(limit)
.skip(skip)
.toArray(function (err, items) {
if (err) {
node.error(err)
} else {
msg.payload = items
delete msg.projection
delete msg.sort
delete msg.limit
delete msg.skip
node.send(msg)
}
})
} else if (node.operation === 'count') {
selector = ensureValidSelectorObject(msg.payload)
coll.count(selector, function (err, count) {
if (err) {
node.error(err)
} else {
msg.payload = count
node.send(msg)
}
})
} else if (node.operation === 'aggregate') {
msg.payload = Array.isArray(msg.payload) ? msg.payload : []
coll.aggregate(msg.payload, function (err, result) {
if (err) {
node.error(err)
} else {
msg.payload = result
node.send(msg)
}
})
} else if (node.operation === 'store') {
if (node.payonly) {
if (typeof msg.payload !== 'object') {
msg.payload = { payload: msg.payload }
}
if (msg.hasOwnProperty('_id') && !msg.payload.hasOwnProperty('_id')) {
msg.payload._id = msg._id
}
coll.save(msg.payload, function (err, item) {
if (err) {
node.error(err, msg)
} else {
msg.payload = item
node.send(msg)
}
})
}
else {
coll.save(msg, function (err, item) {
if (err) {
node.error(err, msg);
}
});
}
} else if (node.operation === 'insert') {
if (node.payonly) {
if (typeof msg.payload !== 'object') {
msg.payload = { payload: msg.payload }
}
if (msg.hasOwnProperty('_id') && !msg.payload.hasOwnProperty('_id')) {
msg.payload._id = msg._id
}
coll.insert(msg.payload, function (err, item) {
if (err) {
node.error(err, msg)
} else {
msg.payload = item
node.send(msg)
}
})
}
else {
coll.insert(msg, function (err, item) {
if (err) {
node.error(err, msg);
}
});
}
} else if (node.operation === 'update') {
if (typeof msg.payload !== 'object') {
msg.payload = { payload: msg.payload }
}
var query = msg.query || {}
var payload = msg.payload || {}
var options = {
upsert: node.upsert,
multi: node.multi
}
if (ObjectID.isValid(msg.query._id)) {
msg.query._id = new ObjectID(msg.query._id)
}
coll.update(query, payload, options, function (err, item) {
if (err) {
node.error(err, msg)
} else {
msg.payload = item
node.send(msg)
}
})
} else if (node.operation === 'delete') {
coll.remove(msg.payload, function (err, items) {
if (err) {
node.error(err, msg)
} else {
msg.payload = items
node.send(msg)
}
})
}
})
}
node.on("input",function(msg) {
if (!node.collection) {
if (msg.collection) {
coll = db.collection(msg.collection);
}
else {
node.error(RED._("mongodb.errors.nocollection"),msg);
return;
}
}
delete msg._topic;
delete msg.collection;
if (node.operation === "store") {
if (node.payonly) {
if (typeof msg.payload !== "object") {
msg.payload = {"payload": msg.payload};
}
if (msg.hasOwnProperty("_id") && !msg.payload.hasOwnProperty("_id")) {
msg.payload._id = msg._id;
}
coll.save(msg.payload,function(err, item) {
if (err) {
node.error(err,msg);
}
});
}
else {
coll.save(msg,function(err, item) {
if (err) {
node.error(err,msg);
}
});
}
}
else if (node.operation === "insert") {
if (node.payonly) {
if (typeof msg.payload !== "object") {
msg.payload = {"payload": msg.payload};
}
if (msg.hasOwnProperty("_id") && !msg.payload.hasOwnProperty("_id")) {
msg.payload._id = msg._id;
}
coll.insert(msg.payload, function(err, item) {
if (err) {
node.error(err,msg);
}
});
}
else {
coll.insert(msg, function(err,item) {
if (err) {
node.error(err,msg);
}
});
}
}
else if (node.operation === "update") {
if (typeof msg.payload !== "object") {
msg.payload = {"payload": msg.payload};
}
var query = msg.query || {};
var payload = msg.payload || {};
var options = {
upsert: node.upsert,
multi: node.multi
};
if (ObjectID.isValid(msg.query._id)) {
msg.query._id = new ObjectID(msg.query._id);
}
coll.update(query, payload, options, function(err, item) {
if (err) {
node.error(err,msg);
}
});
}
else if (node.operation === "delete") {
coll.remove(msg.payload, function(err, items) {
if (err) {
node.error(err,msg);
}
});
}
});
}
});
)
}
if (node.mongoConfig) { connectToDB(); }
else { node.error(RED._("mongodb.errors.missingconfig")); }
node.on("close", function() {
node.status({});
if (node.tout) { clearTimeout(node.tout); }
if (node.clientDb) { node.clientDb.close(); }
});
}
RED.nodes.registerType("mongodb out",MongoOutNode);
function MongoInNode(n) {
RED.nodes.createNode(this,n);
this.collection = n.collection;
this.mongodb = n.mongodb;
this.operation = n.operation || "find";
this.mongoConfig = RED.nodes.getNode(this.mongodb);
this.status({fill:"grey",shape:"ring",text:RED._("mongodb.status.connecting")});
var node = this;
var noerror = true;
var connectToDB = function() {
MongoClient.connect(node.mongoConfig.url, function(err,db) {
if (err) {
node.status({fill:"red",shape:"ring",text:RED._("mongodb.status.error")});
if (noerror) { node.error(err); }
noerror = false;
node.tout = setTimeout(connectToDB, 10000);
}
else {
node.status({fill:"green",shape:"dot",text:RED._("mongodb.status.connected")});
node.clientDb = db;
noerror = true;
var coll;
node.on("input", function(msg) {
if (!node.collection) {
if (msg.collection) {
coll = db.collection(msg.collection);
}
else {
node.error(RED._("mongodb.errors.nocollection"));
return;
}
}
else {
coll = db.collection(node.collection);
}
var selector;
if (node.operation === "find") {
msg.projection = msg.projection || {};
selector = ensureValidSelectorObject(msg.payload);
var limit = msg.limit;
if (typeof limit === "string" && !isNaN(limit)) {
limit = Number(limit);
} else if (typeof limit === "undefined") {
limit = 0;
}
var skip = msg.skip;
if (typeof skip === "string" && !isNaN(skip)) {
skip = Number(skip);
} else if (typeof skip === "undefined") {
skip = 0;
}
coll.find(selector,msg.projection).sort(msg.sort).limit(limit).skip(skip).toArray(function(err, items) {
if (err) {
node.error(err);
}
else {
msg.payload = items;
delete msg.projection;
delete msg.sort;
delete msg.limit;
delete msg.skip;
node.send(msg);
}
});
}
else if (node.operation === "count") {
selector = ensureValidSelectorObject(msg.payload);
coll.count(selector, function(err, count) {
if (err) {
node.error(err);
}
else {
msg.payload = count;
node.send(msg);
}
});
}
else if (node.operation === "aggregate") {
msg.payload = (Array.isArray(msg.payload)) ? msg.payload : [];
coll.aggregate(msg.payload, function(err, result) {
if (err) {
node.error(err);
}
else {
msg.payload = result;
node.send(msg);
}
});
}
});
}
});
if (node.mongoConfig) {
connectToDB()
} else {
node.error(RED._('mongodb.errors.missingconfig'))
}
if (node.mongoConfig) { connectToDB(); }
else { node.error(RED._("mongodb.errors.missingconfig")); }
node.on("close", function() {
node.status({});
if (node.tout) { clearTimeout(node.tout); }
if (node.clientDb) { node.clientDb.close(); }
});
node.on('close', function () {
node.status({})
if (node.tout) {
clearTimeout(node.tout)
}
if (node.clientDb) {
node.clientDb.close()
}
})
}
RED.nodes.registerType("mongodb in",MongoInNode);
}
RED.nodes.registerType('mongodb', MongoNode)
}