diff --git a/storage/mongodb/66-mongodb.js b/storage/mongodb/66-mongodb.js index 61d7852c..2be88b26 100644 --- a/storage/mongodb/66-mongodb.js +++ b/storage/mongodb/66-mongodb.js @@ -59,13 +59,17 @@ module.exports = function(RED) { 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; - if (this.mongoConfig) { - var node = this; - MongoClient.connect(this.mongoConfig.url, function(err, db) { + var connectToDB = function() { + MongoClient.connect(node.mongoConfig.url, function(err, db) { if (err) { + node.status({fill:"red",shape:"ring",text:RED._("mongodb.status.error")}); node.error(err); + node.tout = setTimeout(connectToDB(), 10000); } else { + node.status({fill:"green",shape:"dot",text:RED._("mongodb.status.connected")}); node.clientDb = db; var coll; if (node.collection) { @@ -148,31 +152,37 @@ module.exports = function(RED) { }); } }); - } else { - this.error(RED._("mongodb.errors.missingconfig")); } - this.on("close", function() { - if (this.clientDb) { - this.clientDb.close(); - } + 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; - if (this.mongoConfig) { - var node = this; - MongoClient.connect(this.mongoConfig.url, function(err,db) { + var connectToDB = function() { + MongoClient.connect(node.mongoConfig.url, function(err,db) { if (err) { + node.status({fill:"red",shape:"ring",text:RED._("mongodb.status.error")}); node.error(err); + node.tout = setTimeout(connectToDB(), 10000); } else { + node.status({fill:"green",shape:"dot",text:RED._("mongodb.status.connected")}); node.clientDb = db; var coll; if (node.collection) { @@ -236,14 +246,15 @@ module.exports = function(RED) { }); } }); - } else { - this.error(RED._("mongodb.errors.missingconfig")); } - this.on("close", function() { - if (this.clientDb) { - this.clientDb.close(); - } + 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); diff --git a/storage/mongodb/locales/en-US/66-mongodb.json b/storage/mongodb/locales/en-US/66-mongodb.json index 0846820a..e4ab1a40 100644 --- a/storage/mongodb/locales/en-US/66-mongodb.json +++ b/storage/mongodb/locales/en-US/66-mongodb.json @@ -22,6 +22,11 @@ "count": "count", "aggregate": "aggregate" }, + "status": { + "connecting": "connecting", + "connected": "connected", + "error": "error" + }, "tip": " Tip: If no collection is set, ensure msg.collection will contain the collection name", "errors": { "nocollection": "No collection defined", diff --git a/storage/mongodb/package.json b/storage/mongodb/package.json index c079d03c..bb691e8c 100644 --- a/storage/mongodb/package.json +++ b/storage/mongodb/package.json @@ -1,6 +1,6 @@ { "name" : "node-red-node-mongodb", - "version" : "0.0.6", + "version" : "0.0.7", "description" : "Node-RED nodes to talk to an Mongo database", "dependencies" : { "mongodb" : "^2.2.5"