mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Migrate to new node function style
This commit is contained in:
@@ -14,43 +14,44 @@
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
var RED = require(process.env.NODE_RED_HOME+"/red/red");
|
||||
var fs = require("fs");
|
||||
var spawn = require('child_process').spawn;
|
||||
|
||||
function TailNode(n) {
|
||||
RED.nodes.createNode(this,n);
|
||||
|
||||
this.filename = n.filename;
|
||||
this.split = n.split;
|
||||
var node = this;
|
||||
|
||||
var err = "";
|
||||
var tail = spawn("tail", ["-f", this.filename]);
|
||||
tail.stdout.on("data", function (data) {
|
||||
var msg = {topic:node.filename};
|
||||
if (node.split) {
|
||||
var strings = data.toString().split("\n");
|
||||
for (s in strings) {
|
||||
if (strings[s] != "") {
|
||||
msg.payload = strings[s];
|
||||
node.send(msg);
|
||||
module.exports = function(RED) {
|
||||
var fs = require("fs");
|
||||
var spawn = require('child_process').spawn;
|
||||
|
||||
function TailNode(n) {
|
||||
RED.nodes.createNode(this,n);
|
||||
|
||||
this.filename = n.filename;
|
||||
this.split = n.split;
|
||||
var node = this;
|
||||
|
||||
var err = "";
|
||||
var tail = spawn("tail", ["-f", this.filename]);
|
||||
tail.stdout.on("data", function (data) {
|
||||
var msg = {topic:node.filename};
|
||||
if (node.split) {
|
||||
var strings = data.toString().split("\n");
|
||||
for (s in strings) {
|
||||
if (strings[s] != "") {
|
||||
msg.payload = strings[s];
|
||||
node.send(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
msg.payload = data.toString();
|
||||
node.send(msg);
|
||||
}
|
||||
});
|
||||
|
||||
tail.stderr.on("data", function(data) {
|
||||
node.warn(data.toString());
|
||||
});
|
||||
|
||||
this.on("close", function() {
|
||||
if (tail) tail.kill();
|
||||
});
|
||||
else {
|
||||
msg.payload = data.toString();
|
||||
node.send(msg);
|
||||
}
|
||||
});
|
||||
|
||||
tail.stderr.on("data", function(data) {
|
||||
node.warn(data.toString());
|
||||
});
|
||||
|
||||
this.on("close", function() {
|
||||
if (tail) tail.kill();
|
||||
});
|
||||
}
|
||||
|
||||
RED.nodes.registerType("tail",TailNode);
|
||||
}
|
||||
|
||||
RED.nodes.registerType("tail",TailNode);
|
||||
|
@@ -14,77 +14,78 @@
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
var RED = require(process.env.NODE_RED_HOME+"/red/red");
|
||||
var fs = require("fs");
|
||||
|
||||
function FileNode(n) {
|
||||
RED.nodes.createNode(this,n);
|
||||
|
||||
this.filename = n.filename;
|
||||
this.appendNewline = n.appendNewline;
|
||||
this.overwriteFile = n.overwriteFile;
|
||||
var node = this;
|
||||
this.on("input",function(msg) {
|
||||
var filename = msg.filename || this.filename;
|
||||
|
||||
if (filename == "") {
|
||||
node.warn('No filename specified');
|
||||
} else if (typeof msg.payload != "undefined") {
|
||||
var data = msg.payload;
|
||||
if (typeof data == "object") { data = JSON.stringify(data); }
|
||||
if (typeof data == "boolean") { data = data.toString(); }
|
||||
if (this.appendNewline) {
|
||||
data += "\n";
|
||||
}
|
||||
if (msg.hasOwnProperty('delete')) {
|
||||
fs.unlink(filename, function (err) {
|
||||
if (err) node.warn('Failed to delete file : '+err);
|
||||
//console.log('Deleted file",filename);
|
||||
});
|
||||
}
|
||||
else {
|
||||
if (this.overwriteFile) {
|
||||
fs.writeFile(filename, data, function (err) {
|
||||
if (err) node.warn('Failed to write to file : '+err);
|
||||
//console.log('Message written to file',filename);
|
||||
module.exports = function(RED) {
|
||||
var fs = require("fs");
|
||||
|
||||
function FileNode(n) {
|
||||
RED.nodes.createNode(this,n);
|
||||
|
||||
this.filename = n.filename;
|
||||
this.appendNewline = n.appendNewline;
|
||||
this.overwriteFile = n.overwriteFile;
|
||||
var node = this;
|
||||
this.on("input",function(msg) {
|
||||
var filename = msg.filename || this.filename;
|
||||
|
||||
if (filename == "") {
|
||||
node.warn('No filename specified');
|
||||
} else if (typeof msg.payload != "undefined") {
|
||||
var data = msg.payload;
|
||||
if (typeof data == "object") { data = JSON.stringify(data); }
|
||||
if (typeof data == "boolean") { data = data.toString(); }
|
||||
if (this.appendNewline) {
|
||||
data += "\n";
|
||||
}
|
||||
if (msg.hasOwnProperty('delete')) {
|
||||
fs.unlink(filename, function (err) {
|
||||
if (err) node.warn('Failed to delete file : '+err);
|
||||
//console.log('Deleted file",filename);
|
||||
});
|
||||
}
|
||||
else {
|
||||
fs.appendFile(filename, data, function (err) {
|
||||
if (err) node.warn('Failed to append to file : '+err);
|
||||
//console.log('Message appended to file',filename);
|
||||
});
|
||||
if (this.overwriteFile) {
|
||||
fs.writeFile(filename, data, function (err) {
|
||||
if (err) node.warn('Failed to write to file : '+err);
|
||||
//console.log('Message written to file',filename);
|
||||
});
|
||||
}
|
||||
else {
|
||||
fs.appendFile(filename, data, function (err) {
|
||||
if (err) node.warn('Failed to append to file : '+err);
|
||||
//console.log('Message appended to file',filename);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
RED.nodes.registerType("file",FileNode);
|
||||
|
||||
function FileInNode(n) {
|
||||
RED.nodes.createNode(this,n);
|
||||
|
||||
this.filename = n.filename;
|
||||
this.format = n.format;
|
||||
var node = this;
|
||||
var options = {};
|
||||
if (this.format) {
|
||||
options['encoding'] = this.format;
|
||||
});
|
||||
}
|
||||
this.on("input",function(msg) {
|
||||
var filename = msg.filename || this.filename;
|
||||
|
||||
if (filename == "") {
|
||||
node.warn('No filename specified');
|
||||
} else {
|
||||
fs.readFile(filename,options,function(err,data) {
|
||||
if (err) {
|
||||
node.warn(err);
|
||||
} else {
|
||||
node.send({payload:data});
|
||||
}
|
||||
});
|
||||
RED.nodes.registerType("file",FileNode);
|
||||
|
||||
function FileInNode(n) {
|
||||
RED.nodes.createNode(this,n);
|
||||
|
||||
this.filename = n.filename;
|
||||
this.format = n.format;
|
||||
var node = this;
|
||||
var options = {};
|
||||
if (this.format) {
|
||||
options['encoding'] = this.format;
|
||||
}
|
||||
});
|
||||
this.on("input",function(msg) {
|
||||
var filename = msg.filename || this.filename;
|
||||
|
||||
if (filename == "") {
|
||||
node.warn('No filename specified');
|
||||
} else {
|
||||
fs.readFile(filename,options,function(err,data) {
|
||||
if (err) {
|
||||
node.warn(err);
|
||||
} else {
|
||||
node.send({payload:data});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
RED.nodes.registerType("file in",FileInNode);
|
||||
}
|
||||
RED.nodes.registerType("file in",FileInNode);
|
||||
|
@@ -14,83 +14,84 @@
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
var RED = require(process.env.NODE_RED_HOME+"/red/red");
|
||||
var util = require("util");
|
||||
var redis = require("redis");
|
||||
|
||||
var hashFieldRE = /^([^=]+)=(.*)$/;
|
||||
|
||||
var redisConnectionPool = function() {
|
||||
var connections = {};
|
||||
var obj = {
|
||||
get: function(host,port) {
|
||||
var id = host+":"+port;
|
||||
if (!connections[id]) {
|
||||
connections[id] = redis.createClient(port,host);
|
||||
connections[id].on("error",function(err) {
|
||||
util.log("[redis] "+err);
|
||||
});
|
||||
connections[id].on("connect",function() {
|
||||
util.log("[redis] connected to "+host+":"+port);
|
||||
});
|
||||
connections[id]._id = id;
|
||||
connections[id]._nodeCount = 0;
|
||||
}
|
||||
connections[id]._nodeCount += 1;
|
||||
return connections[id];
|
||||
},
|
||||
close: function(connection) {
|
||||
connection._nodeCount -= 1;
|
||||
if (connection._nodeCount == 0) {
|
||||
if (connection) {
|
||||
clearTimeout(connection.retry_timer);
|
||||
connection.end();
|
||||
module.exports = function(RED) {
|
||||
var util = require("util");
|
||||
var redis = require("redis");
|
||||
|
||||
var hashFieldRE = /^([^=]+)=(.*)$/;
|
||||
|
||||
var redisConnectionPool = function() {
|
||||
var connections = {};
|
||||
var obj = {
|
||||
get: function(host,port) {
|
||||
var id = host+":"+port;
|
||||
if (!connections[id]) {
|
||||
connections[id] = redis.createClient(port,host);
|
||||
connections[id].on("error",function(err) {
|
||||
util.log("[redis] "+err);
|
||||
});
|
||||
connections[id].on("connect",function() {
|
||||
util.log("[redis] connected to "+host+":"+port);
|
||||
});
|
||||
connections[id]._id = id;
|
||||
connections[id]._nodeCount = 0;
|
||||
}
|
||||
delete connections[connection._id];
|
||||
}
|
||||
}
|
||||
};
|
||||
return obj;
|
||||
}();
|
||||
|
||||
|
||||
function RedisOutNode(n) {
|
||||
RED.nodes.createNode(this,n);
|
||||
this.port = n.port||"6379";
|
||||
this.hostname = n.hostname||"127.0.0.1";
|
||||
this.key = n.key;
|
||||
this.structtype = n.structtype;
|
||||
|
||||
this.client = redisConnectionPool.get(this.hostname,this.port);
|
||||
|
||||
this.on("input", function(msg) {
|
||||
if (msg != null) {
|
||||
var k = this.key || msg.topic;
|
||||
if (k) {
|
||||
if (this.structtype == "string") {
|
||||
this.client.set(k,msg.payload);
|
||||
} else if (this.structtype == "hash") {
|
||||
var r = hashFieldRE.exec(msg.payload);
|
||||
if (r) {
|
||||
this.client.hset(k,r[1],r[2]);
|
||||
} else {
|
||||
this.warn("Invalid payload for redis hash");
|
||||
}
|
||||
} else if (this.structtype == "set") {
|
||||
this.client.sadd(k,msg.payload);
|
||||
} else if (this.structtype == "list") {
|
||||
this.client.rpush(k,msg.payload);
|
||||
connections[id]._nodeCount += 1;
|
||||
return connections[id];
|
||||
},
|
||||
close: function(connection) {
|
||||
connection._nodeCount -= 1;
|
||||
if (connection._nodeCount == 0) {
|
||||
if (connection) {
|
||||
clearTimeout(connection.retry_timer);
|
||||
connection.end();
|
||||
}
|
||||
} else {
|
||||
this.warn("No key or topic set");
|
||||
delete connections[connection._id];
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
RED.nodes.registerType("redis out",RedisOutNode);
|
||||
|
||||
RedisOutNode.prototype.close = function() {
|
||||
redisConnectionPool.close(this.client);
|
||||
};
|
||||
return obj;
|
||||
}();
|
||||
|
||||
|
||||
function RedisOutNode(n) {
|
||||
RED.nodes.createNode(this,n);
|
||||
this.port = n.port||"6379";
|
||||
this.hostname = n.hostname||"127.0.0.1";
|
||||
this.key = n.key;
|
||||
this.structtype = n.structtype;
|
||||
|
||||
this.client = redisConnectionPool.get(this.hostname,this.port);
|
||||
|
||||
this.on("input", function(msg) {
|
||||
if (msg != null) {
|
||||
var k = this.key || msg.topic;
|
||||
if (k) {
|
||||
if (this.structtype == "string") {
|
||||
this.client.set(k,msg.payload);
|
||||
} else if (this.structtype == "hash") {
|
||||
var r = hashFieldRE.exec(msg.payload);
|
||||
if (r) {
|
||||
this.client.hset(k,r[1],r[2]);
|
||||
} else {
|
||||
this.warn("Invalid payload for redis hash");
|
||||
}
|
||||
} else if (this.structtype == "set") {
|
||||
this.client.sadd(k,msg.payload);
|
||||
} else if (this.structtype == "list") {
|
||||
this.client.rpush(k,msg.payload);
|
||||
}
|
||||
} else {
|
||||
this.warn("No key or topic set");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
RED.nodes.registerType("redis out",RedisOutNode);
|
||||
|
||||
RedisOutNode.prototype.close = function() {
|
||||
redisConnectionPool.close(this.client);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -14,165 +14,166 @@
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
var RED = require(process.env.NODE_RED_HOME+"/red/red");
|
||||
var mongo = require('mongodb');
|
||||
var MongoClient = mongo.MongoClient;
|
||||
|
||||
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 credentials = RED.nodes.getCredentials(n.id);
|
||||
if (credentials) {
|
||||
this.username = credentials.user;
|
||||
this.password = credentials.password;
|
||||
module.exports = function(RED) {
|
||||
var mongo = require('mongodb');
|
||||
var MongoClient = mongo.MongoClient;
|
||||
|
||||
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 credentials = RED.nodes.getCredentials(n.id);
|
||||
if (credentials) {
|
||||
this.username = credentials.user;
|
||||
this.password = credentials.password;
|
||||
}
|
||||
|
||||
var url = "mongodb://";
|
||||
if (this.username && this.password) {
|
||||
url += this.username+":"+this.password+"@";
|
||||
}
|
||||
url += this.hostname+":"+this.port+"/"+this.db;
|
||||
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
var url = "mongodb://";
|
||||
if (this.username && this.password) {
|
||||
url += this.username+":"+this.password+"@";
|
||||
}
|
||||
url += this.hostname+":"+this.port+"/"+this.db;
|
||||
RED.nodes.registerType("mongodb",MongoNode);
|
||||
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
RED.nodes.registerType("mongodb",MongoNode);
|
||||
|
||||
var querystring = require('querystring');
|
||||
|
||||
RED.httpAdmin.get('/mongodb/:id',function(req,res) {
|
||||
var credentials = RED.nodes.getCredentials(req.params.id);
|
||||
if (credentials) {
|
||||
res.send(JSON.stringify({user:credentials.user,hasPassword:(credentials.password&&credentials.password!="")}));
|
||||
} else {
|
||||
res.send(JSON.stringify({}));
|
||||
}
|
||||
});
|
||||
|
||||
RED.httpAdmin.delete('/mongodb/:id',function(req,res) {
|
||||
RED.nodes.deleteCredentials(req.params.id);
|
||||
res.send(200);
|
||||
});
|
||||
|
||||
RED.httpAdmin.post('/mongodb/:id',function(req,res) {
|
||||
var body = "";
|
||||
req.on('data', function(chunk) {
|
||||
body+=chunk;
|
||||
var querystring = require('querystring');
|
||||
|
||||
RED.httpAdmin.get('/mongodb/:id',function(req,res) {
|
||||
var credentials = RED.nodes.getCredentials(req.params.id);
|
||||
if (credentials) {
|
||||
res.send(JSON.stringify({user:credentials.user,hasPassword:(credentials.password&&credentials.password!="")}));
|
||||
} else {
|
||||
res.send(JSON.stringify({}));
|
||||
}
|
||||
});
|
||||
req.on('end', function(){
|
||||
var newCreds = querystring.parse(body);
|
||||
var credentials = RED.nodes.getCredentials(req.params.id)||{};
|
||||
if (newCreds.user == null || newCreds.user == "") {
|
||||
delete credentials.user;
|
||||
} else {
|
||||
credentials.user = newCreds.user;
|
||||
}
|
||||
if (newCreds.password == "") {
|
||||
delete credentials.password;
|
||||
} else {
|
||||
credentials.password = newCreds.password||credentials.password;
|
||||
}
|
||||
RED.nodes.addCredentials(req.params.id,credentials);
|
||||
|
||||
RED.httpAdmin.delete('/mongodb/:id',function(req,res) {
|
||||
RED.nodes.deleteCredentials(req.params.id);
|
||||
res.send(200);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
function MongoOutNode(n) {
|
||||
RED.nodes.createNode(this,n);
|
||||
this.collection = n.collection;
|
||||
this.mongodb = n.mongodb;
|
||||
this.payonly = n.payonly || false;
|
||||
this.operation = n.operation;
|
||||
this.mongoConfig = RED.nodes.getNode(this.mongodb);
|
||||
|
||||
if (this.mongoConfig) {
|
||||
var node = this;
|
||||
MongoClient.connect(this.mongoConfig.url, function(err,db) {
|
||||
if (err) {
|
||||
node.error(err);
|
||||
} else {
|
||||
node.clientDb = db;
|
||||
var coll = db.collection(node.collection);
|
||||
node.on("input",function(msg) {
|
||||
if (node.operation == "store") {
|
||||
delete msg._topic;
|
||||
if (node.payonly) {
|
||||
if (typeof msg.payload !== "object") { msg.payload = {"payload":msg.payload}; }
|
||||
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 if (node.operation == "insert") {
|
||||
delete msg._topic;
|
||||
if (node.payonly) {
|
||||
if (typeof msg.payload !== "object") { msg.payload = {"payload":msg.payload}; }
|
||||
coll.insert(msg.payload,function(err,item){ if (err){node.error(err);} });
|
||||
} else {
|
||||
coll.insert(msg,function(err,item){if (err){node.error(err);}});
|
||||
}
|
||||
}
|
||||
if (node.operation == "delete") {
|
||||
coll.remove(msg.payload, {w:1}, function(err, items){ if (err) node.error(err); });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
RED.httpAdmin.post('/mongodb/:id',function(req,res) {
|
||||
var body = "";
|
||||
req.on('data', function(chunk) {
|
||||
body+=chunk;
|
||||
});
|
||||
} else {
|
||||
this.error("missing mongodb configuration");
|
||||
}
|
||||
|
||||
this.on("close", function() {
|
||||
if (this.clientDb) {
|
||||
this.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.mongoConfig = RED.nodes.getNode(this.mongodb);
|
||||
|
||||
if (this.mongoConfig) {
|
||||
var node = this;
|
||||
MongoClient.connect(this.mongoConfig.url, function(err,db) {
|
||||
if (err) {
|
||||
node.error(err);
|
||||
req.on('end', function(){
|
||||
var newCreds = querystring.parse(body);
|
||||
var credentials = RED.nodes.getCredentials(req.params.id)||{};
|
||||
if (newCreds.user == null || newCreds.user == "") {
|
||||
delete credentials.user;
|
||||
} else {
|
||||
node.clientDb = db;
|
||||
var coll = db.collection(node.collection);
|
||||
node.on("input",function(msg) {
|
||||
msg.projection = msg.projection || {};
|
||||
coll.find(msg.payload,msg.projection).sort(msg.sort).limit(msg.limit).toArray(function(err, items) {
|
||||
if (err) {
|
||||
node.error(err);
|
||||
} else {
|
||||
msg.payload = items;
|
||||
delete msg.projection;
|
||||
delete msg.sort;
|
||||
delete msg.limit;
|
||||
node.send(msg);
|
||||
credentials.user = newCreds.user;
|
||||
}
|
||||
if (newCreds.password == "") {
|
||||
delete credentials.password;
|
||||
} else {
|
||||
credentials.password = newCreds.password||credentials.password;
|
||||
}
|
||||
RED.nodes.addCredentials(req.params.id,credentials);
|
||||
res.send(200);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
function MongoOutNode(n) {
|
||||
RED.nodes.createNode(this,n);
|
||||
this.collection = n.collection;
|
||||
this.mongodb = n.mongodb;
|
||||
this.payonly = n.payonly || false;
|
||||
this.operation = n.operation;
|
||||
this.mongoConfig = RED.nodes.getNode(this.mongodb);
|
||||
|
||||
if (this.mongoConfig) {
|
||||
var node = this;
|
||||
MongoClient.connect(this.mongoConfig.url, function(err,db) {
|
||||
if (err) {
|
||||
node.error(err);
|
||||
} else {
|
||||
node.clientDb = db;
|
||||
var coll = db.collection(node.collection);
|
||||
node.on("input",function(msg) {
|
||||
if (node.operation == "store") {
|
||||
delete msg._topic;
|
||||
if (node.payonly) {
|
||||
if (typeof msg.payload !== "object") { msg.payload = {"payload":msg.payload}; }
|
||||
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 if (node.operation == "insert") {
|
||||
delete msg._topic;
|
||||
if (node.payonly) {
|
||||
if (typeof msg.payload !== "object") { msg.payload = {"payload":msg.payload}; }
|
||||
coll.insert(msg.payload,function(err,item){ if (err){node.error(err);} });
|
||||
} else {
|
||||
coll.insert(msg,function(err,item){if (err){node.error(err);}});
|
||||
}
|
||||
}
|
||||
if (node.operation == "delete") {
|
||||
coll.remove(msg.payload, {w:1}, function(err, items){ if (err) node.error(err); });
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
this.error("missing mongodb configuration");
|
||||
}
|
||||
|
||||
this.on("close", function() {
|
||||
if (this.clientDb) {
|
||||
this.clientDb.close();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
this.error("missing mongodb configuration");
|
||||
}
|
||||
|
||||
this.on("close", function() {
|
||||
if (this.clientDb) {
|
||||
this.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.mongoConfig = RED.nodes.getNode(this.mongodb);
|
||||
|
||||
if (this.mongoConfig) {
|
||||
var node = this;
|
||||
MongoClient.connect(this.mongoConfig.url, function(err,db) {
|
||||
if (err) {
|
||||
node.error(err);
|
||||
} else {
|
||||
node.clientDb = db;
|
||||
var coll = db.collection(node.collection);
|
||||
node.on("input",function(msg) {
|
||||
msg.projection = msg.projection || {};
|
||||
coll.find(msg.payload,msg.projection).sort(msg.sort).limit(msg.limit).toArray(function(err, items) {
|
||||
if (err) {
|
||||
node.error(err);
|
||||
} else {
|
||||
msg.payload = items;
|
||||
delete msg.projection;
|
||||
delete msg.sort;
|
||||
delete msg.limit;
|
||||
node.send(msg);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
this.error("missing mongodb configuration");
|
||||
}
|
||||
});
|
||||
|
||||
this.on("close", function() {
|
||||
if (this.clientDb) {
|
||||
this.clientDb.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
RED.nodes.registerType("mongodb in",MongoInNode);
|
||||
}
|
||||
RED.nodes.registerType("mongodb in",MongoInNode);
|
||||
|
Reference in New Issue
Block a user