mirror of
https://github.com/node-red/node-red-nodes.git
synced 2023-10-10 13:36:58 +02:00
Added missing operations to IN node
This commit is contained in:
parent
44448bd3f0
commit
5a62f5b4d4
@ -1,4 +1,4 @@
|
|||||||
<script type="text/x-red" data-template-name="mongodb-config">
|
<script type="text/x-red" data-template-name="mongodb">
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-config-input-hostname"><i class="fa fa-bookmark"></i> <span data-i18n="mongodb.label.host"></span></label>
|
<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%;" >
|
<input class="input-append-left" type="text" id="node-config-input-hostname" placeholder="localhost" style="width: 40%;" >
|
||||||
@ -24,7 +24,7 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
RED.nodes.registerType('mongodb-config', {
|
RED.nodes.registerType('mongodb', {
|
||||||
category: 'config',
|
category: 'config',
|
||||||
color: "rgb(218, 196, 180)",
|
color: "rgb(218, 196, 180)",
|
||||||
defaults: {
|
defaults: {
|
||||||
@ -44,7 +44,7 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
||||||
<script type="text/x-red" data-template-name="mongodb">
|
<script type="text/x-red" data-template-name="mongodb out">
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-input-mongodb"><i class="fa fa-bookmark"></i> <span data-i18n="mongodb.label.server"></span></label>
|
<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">
|
<input type="text" id="node-input-mongodb">
|
||||||
@ -56,10 +56,7 @@
|
|||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-input-operation"><i class="fa fa-wrench"></i> <span data-i18n="mongodb.label.operation"></span></label>
|
<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;">
|
<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="store" data-i18n="mongodb.operation.save"></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="insert" data-i18n="mongodb.operation.insert"></option>
|
||||||
<option value="update" data-i18n="mongodb.operation.update"></option>
|
<option value="update" data-i18n="mongodb.operation.update"></option>
|
||||||
<option value="delete" data-i18n="mongodb.operation.remove"></option>
|
<option value="delete" data-i18n="mongodb.operation.remove"></option>
|
||||||
@ -70,7 +67,7 @@
|
|||||||
<input type="checkbox" id="node-input-payonly" style="display: inline-block; width: auto; vertical-align: top;">
|
<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>
|
<label for="node-input-payonly" style="width: 70%;"><span data-i18n="mongodb.label.onlystore"></span></label>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-row node-input-upsert">
|
<div class="form-row node-input-upsert">
|
||||||
<label> </label>
|
<label> </label>
|
||||||
<input type="checkbox" id="node-input-upsert" style="display: inline-block; width: auto; vertical-align: top;">
|
<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>
|
<label for="node-input-upsert" style="width: 70%;"><span data-i18n="mongodb.label.createnew"></span></label>
|
||||||
@ -87,51 +84,29 @@
|
|||||||
<div class="form-tips" id="node-warning" style="display: none"><span data-i18n="[html]mongodb.tip"></span></div>
|
<div class="form-tips" id="node-warning" style="display: none"><span data-i18n="[html]mongodb.tip"></span></div>
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script type="text/x-red" data-help-name="mongodb">
|
<script type="text/x-red" data-help-name="mongodb out">
|
||||||
<p>A simple MongoDB node. Can manipulate a chosen collection.</p>
|
<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>Find queries a collection using the <code>msg.payload</code> as the query statement as per the .find() function.
|
<p>Insert will insert a new object.</p>
|
||||||
Optionally, you may also set a <code>msg.projection</code> object (via a function) to constrain the returned
|
<p>Save and insert either store <code>msg</code> or <code>msg.payload</code>.</p>
|
||||||
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>Update will modify an existing object or objects. The query to select objects to update uses <code>msg.query</code>,
|
||||||
|
|
||||||
<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
|
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>
|
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>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
|
<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
|
<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
|
<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
|
<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>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>
|
<p>Currently we do not limit or cap the collection size, however this may well change.</p>
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function oneditprepare() {
|
function oneditprepare() {
|
||||||
$("#node-input-operation").change(function () {
|
$("#node-input-operation").change(function () {
|
||||||
var id = $("#node-input-operation option:selected").val();
|
var id = $("#node-input-operation option:selected").val();
|
||||||
@ -156,17 +131,83 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
RED.nodes.registerType('mongodb', {
|
RED.nodes.registerType('mongodb out', {
|
||||||
category: 'storage',
|
category: 'storage-output',
|
||||||
color: "rgb(218, 196, 180)",
|
color: "rgb(218, 196, 180)",
|
||||||
defaults: {
|
defaults: {
|
||||||
mongodb: { type: "mongodb-config", required: true },
|
mongodb: { type: "mongodb", required: true },
|
||||||
name: { value: "" },
|
name: { value: "" },
|
||||||
collection: { value: "" },
|
collection: { value: "" },
|
||||||
payonly: { value: false },
|
payonly: { value: false },
|
||||||
operation: { value: "find" },
|
|
||||||
upsert: { value: false },
|
upsert: { value: false },
|
||||||
multi: { 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" }
|
||||||
},
|
},
|
||||||
inputs: 1,
|
inputs: 1,
|
||||||
outputs: 1,
|
outputs: 1,
|
||||||
|
@ -1,237 +1,327 @@
|
|||||||
|
|
||||||
module.exports = function (RED) {
|
module.exports = function (RED) {
|
||||||
'use strict'
|
"use strict";
|
||||||
const mongo = require('mongodb')
|
var mongo = require('mongodb');
|
||||||
const ObjectID = require('mongodb').ObjectID
|
var ObjectID = require('mongodb').ObjectID;
|
||||||
const MongoClient = mongo.MongoClient
|
var MongoClient = mongo.MongoClient;
|
||||||
|
|
||||||
function MongoConfigNode(n) {
|
function MongoNode(n) {
|
||||||
RED.nodes.createNode(this, n)
|
RED.nodes.createNode(this, 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
|
this.name = n.name;
|
||||||
|
|
||||||
|
var url = "mongodb://";
|
||||||
var url = 'mongodb://'
|
|
||||||
if (this.credentials && this.credentials.user && this.credentials.password) {
|
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-config', MongoConfigNode, {
|
RED.nodes.registerType("mongodb", MongoNode, {
|
||||||
credentials: {
|
credentials: {
|
||||||
user: { type: 'text' },
|
user: { type: "text" },
|
||||||
password: { type: 'password' }
|
password: { type: "password" }
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
function ensureValidSelectorObject(selector) {
|
function ensureValidSelectorObject(selector) {
|
||||||
if (selector != null && (typeof selector != 'object' || Buffer.isBuffer(selector))) {
|
if (selector != null && (typeof selector != 'object' || Buffer.isBuffer(selector))) {
|
||||||
return {}
|
return {};
|
||||||
}
|
}
|
||||||
return selector
|
return selector;
|
||||||
}
|
}
|
||||||
|
|
||||||
function MongoNode(n) {
|
function MongoOutNode(n) {
|
||||||
RED.nodes.createNode(this, n)
|
RED.nodes.createNode(this, 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 || '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.upsert = n.upsert || false;
|
||||||
this.multi = n.multi || 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 () {
|
var connectToDB = function () {
|
||||||
MongoClient.connect(
|
MongoClient.connect(node.mongoConfig.url, function (err, db) {
|
||||||
node.mongoConfig.url,
|
if (err) {
|
||||||
function (err, db) {
|
node.status({ fill: "red", shape: "ring", text: RED._("mongodb.status.error") });
|
||||||
if (err) {
|
if (noerror) { node.error(err); }
|
||||||
node.status({ fill: 'red', shape: 'ring', text: RED._('mongodb.status.error') })
|
noerror = false;
|
||||||
if (noerror) {
|
node.tout = setTimeout(connectToDB, 10000);
|
||||||
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)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
)
|
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"), msg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
delete msg._topic;
|
||||||
|
delete msg.collection;
|
||||||
|
if (node.operation === "store") {
|
||||||
|
storeInDb(node, msg, coll);
|
||||||
|
}
|
||||||
|
else if (node.operation === "insert") {
|
||||||
|
insertInDb(node, msg, coll);
|
||||||
|
}
|
||||||
|
else if (node.operation === "update") {
|
||||||
|
updateInDb(node, msg, coll);
|
||||||
|
}
|
||||||
|
else if (node.operation === "delete") {
|
||||||
|
deleteInDb(node, msg, coll);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (node.mongoConfig) {
|
if (node.mongoConfig) { connectToDB(); }
|
||||||
connectToDB()
|
else { node.error(RED._("mongodb.errors.missingconfig")); }
|
||||||
} else {
|
|
||||||
node.error(RED._('mongodb.errors.missingconfig'))
|
|
||||||
}
|
|
||||||
|
|
||||||
node.on('close', function () {
|
node.on("close", function () {
|
||||||
node.status({})
|
node.status({});
|
||||||
if (node.tout) {
|
if (node.tout) { clearTimeout(node.tout); }
|
||||||
clearTimeout(node.tout)
|
if (node.clientDb) { node.clientDb.close(); }
|
||||||
}
|
});
|
||||||
if (node.clientDb) {
|
|
||||||
node.clientDb.close()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
RED.nodes.registerType('mongodb', MongoNode)
|
RED.nodes.registerType("mongodb out", MongoOutNode);
|
||||||
}
|
|
||||||
|
|
||||||
|
function MongoInNode(n) {
|
||||||
|
RED.nodes.createNode(this, n);
|
||||||
|
this.collection = n.collection;
|
||||||
|
this.mongodb = n.mongodb;
|
||||||
|
this.payonly = n.payonly || false;
|
||||||
|
this.upsert = n.upsert || false;
|
||||||
|
this.multi = n.multi || 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;
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else if (node.operation === "store") {
|
||||||
|
storeInDb(node, msg, coll);
|
||||||
|
}
|
||||||
|
else if (node.operation === "insert") {
|
||||||
|
insertInDb(node, msg, coll);
|
||||||
|
}
|
||||||
|
else if (node.operation === "update") {
|
||||||
|
updateInDb(node, msg, coll);
|
||||||
|
}
|
||||||
|
else if (node.operation === "delete") {
|
||||||
|
deleteInDb(node, msg, coll);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
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 in", MongoInNode);
|
||||||
|
|
||||||
|
function insertInDb(node, msg, coll) {
|
||||||
|
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 {
|
||||||
|
msg.payload = item;
|
||||||
|
node.send(msg);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateInDb(node, msg, coll) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function deleteInDb(node, msg, coll) {
|
||||||
|
coll.remove(msg.payload, function (err, items) {
|
||||||
|
if (err) {
|
||||||
|
node.error(err, msg);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
msg.payload = items;
|
||||||
|
node.send(msg);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function storeInDb(node, msg, coll) {
|
||||||
|
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 {
|
||||||
|
msg.payload = item;
|
||||||
|
node.send(msg);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user