useV3 connections and support clusters

use v3.6.1+ mongodb client -- need to extract db() from connection client before accessing collections
allow specifying 3.4+ type connection to sharded clusters (mongodb+srv://clustername ) in addition to "old style"  (mongodb://host:port)
This commit is contained in:
Ross Cruickshank 2020-09-08 10:46:02 +01:00 committed by GitHub
parent 53c4c44056
commit d728b99de9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,5 @@
module.exports = function(RED) { module.exports = function(RED) {
"use strict"; "use strict";
var mongo = require('mongodb'); var mongo = require('mongodb');
@ -11,13 +12,37 @@ module.exports = function(RED) {
this.port = n.port; this.port = n.port;
this.db = n.db; this.db = n.db;
this.name = n.name; this.name = n.name;
this.clustered = n.clustered;
var url = "mongodb://"; // if(n.user && n.password){
if (this.credentials && this.credentials.user && this.credentials.password) { // this.credentials.user = n.user;
url += this.credentials.user+":"+this.credentials.password+"@"; // this.credentials.password= n.password;
// }
//console.log(this);
//console.log("\n\n\n\n\n",n);
var url = "mongodb+srv://";
if (!this.clustered) {
url = "mongodb://";
} }
url += this.hostname+":"+this.port+"/"+this.db; if (this.credentials && this.credentials.user && this.credentials.password) {
this.user = this.credentials.user;
this.password = this.credentials.password;
} else {
this.user = n.user;
this.password = n.password;
}
if (this.user) {
url += this.user+":"+this.password+"@";
}
if (this.clustered) {
url += this.hostname + "/" + this.db + "?retryWrites=true&w=majority";
} else {
url += this.hostname + ":" + this.port + "/" + this.db;
};
console.log(url);
this.url = url; this.url = url;
} }
@ -49,7 +74,7 @@ module.exports = function(RED) {
var noerror = true; var noerror = true;
var connectToDB = function() { var connectToDB = function() {
MongoClient.connect(node.mongoConfig.url, function(err, db) { MongoClient.connect(node.mongoConfig.url, function(err, client) {
if (err) { if (err) {
node.status({fill:"red",shape:"ring",text:RED._("mongodb.status.error")}); node.status({fill:"red",shape:"ring",text:RED._("mongodb.status.error")});
if (noerror) { node.error(err); } if (noerror) { node.error(err); }
@ -58,9 +83,12 @@ module.exports = function(RED) {
} }
else { else {
node.status({fill:"green",shape:"dot",text:RED._("mongodb.status.connected")}); node.status({fill:"green",shape:"dot",text:RED._("mongodb.status.connected")});
node.clientDb = db; node.clientDb = client.db();
var db = client.db();
console.log( db);
noerror = true; noerror = true;
var coll; var coll;
if (node.collection) { if (node.collection) {
coll = db.collection(node.collection); coll = db.collection(node.collection);
} }
@ -174,7 +202,8 @@ module.exports = function(RED) {
var noerror = true; var noerror = true;
var connectToDB = function() { var connectToDB = function() {
MongoClient.connect(node.mongoConfig.url, function(err,db) { console.log("connecting:" + node.mongoConfig.url);
MongoClient.connect(node.mongoConfig.url, function(err,client) {
if (err) { if (err) {
node.status({fill:"red",shape:"ring",text:RED._("mongodb.status.error")}); node.status({fill:"red",shape:"ring",text:RED._("mongodb.status.error")});
if (noerror) { node.error(err); } if (noerror) { node.error(err); }
@ -183,7 +212,8 @@ module.exports = function(RED) {
} }
else { else {
node.status({fill:"green",shape:"dot",text:RED._("mongodb.status.connected")}); node.status({fill:"green",shape:"dot",text:RED._("mongodb.status.connected")});
node.clientDb = db; node.clientDb = client.db();
var db = client.db();
noerror = true; noerror = true;
var coll; var coll;
node.on("input", function(msg) { node.on("input", function(msg) {