enable replicaset connect

allow arbitrary connect options, including those needed to support replicaset connections

default option remains direct to support older configs
This commit is contained in:
Ross Cruickshank 2020-09-16 21:57:56 +01:00 committed by GitHub
parent e53854b1ce
commit 835047c6c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,37 +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; this.connectOptions= n.connectOptions;
this.topology = n.topology;
// if(n.user && n.password){
// this.credentials.user = n.user;
// this.credentials.password= n.password;
// }
//console.log(this); //console.log(this);
//console.log("\n\n\n\n\n",n);
var url = "mongodb+srv://"; var clustered = !(this.topology === "direct") || false;
if (!this.clustered) {
url = "mongodb://"; var url = "mongodb://";
if (this.topology === "dnscluster") {
url = "mongodb+srv://";
} }
if (this.credentials && this.credentials.user && this.credentials.password) { if (this.credentials && this.credentials.user && this.credentials.password) {
this.user = this.credentials.user; this.user = this.credentials.user;
this.password = this.credentials.password; this.password = this.credentials.password;
} else { } else {
this.user = n.user; this.user = n.user;
this.password = n.password; this.password = n.password;
} }
if (this.user) { if (this.user) {
url += this.user+":"+this.password+"@"; url += this.user+":"+this.password+"@";
} }
if (this.clustered) { if (clustered) {
url += this.hostname + "/" + this.db + "?retryWrites=true&w=majority"; url += this.hostname + "/" + this.db
} else { } else {
url += this.hostname + ":" + this.port + "/" + this.db; url += this.hostname + ":" + this.port + "/" + this.db;
} }
if (this.connectOptions){
url += "?" + this.connectOptions;
}
console.log(url); console.log("MongoDB URL: " + url);
this.url = url; this.url = url;
} }
@ -85,7 +85,7 @@ module.exports = function(RED) {
node.status({fill:"green",shape:"dot",text:RED._("mongodb.status.connected")}); node.status({fill:"green",shape:"dot",text:RED._("mongodb.status.connected")});
node.clientDb = client.db(); node.clientDb = client.db();
var db = client.db(); var db = client.db();
console.log( db); //console.log( db);
noerror = true; noerror = true;
var coll; var coll;
@ -202,7 +202,7 @@ module.exports = function(RED) {
var noerror = true; var noerror = true;
var connectToDB = function() { var connectToDB = function() {
console.log("connecting:" + node.mongoConfig.url); console.log("connecting: " + node.mongoConfig.url);
MongoClient.connect(node.mongoConfig.url, function(err,client) { 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")});