1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

mongodb: update to add support replicaset clusters (ICD4Mongo) (#688)

This commit is contained in:
Ross Cruickshank 2020-09-17 09:17:43 +01:00 committed by GitHub
parent d26cb057f9
commit cde422f34a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 80 additions and 37 deletions

View File

@ -6,18 +6,24 @@
<input class="input-append-left" type="text" id="node-config-input-hostname" placeholder="localhost">
</div>
<div class="form-row node-config-input-clustered">
<label for="node-config-input-clustered"><i class="fa fa-sitemap"></i><span data-i18n="mongodb.label.clustered"></span></label>
<input type="checkbox" id="node-config-input-clustered" style="width:20px;">
<div class="form-row node-config-input-topology">
<label for="node-config-input-topology"><span data-i18n="mongodb.label.topology"></span></label>
<select id="node-config-input-topology">
<option type="button" class="red-ui-button toggle topology-group" selected value="direct">Direct (mongodb://)</button>
<option type="button" class="red-ui-button toggle topology-group" value="replicaset">RelicaSet/Cluster (mongodb://)</button>
<option type="button" class="red-ui-button toggle topology-group" value="dnscluster">DNS Cluster (mongodb+srv://)</button>
</select>
</div>
<div class="form-row node-config-connectOptions">
<label for="node-config-input-connectOptions"><i class="fa fa-wrench"></i><span data-i18n="mongodb.label.connectOptions"></span></label>
<input type="text" id="node-config-input-connectOptions">
</div>
<div class="form-row node-config-input-port">
<label for="node-config-input-port"><i class="fa fa-plug"></i><span data-i18n="mongodb.label.port"></span></label>
<input type="text" id="node-config-input-port" style="width:55px;">
</div>
<div class="form-row node-config-clusterVersion">
<label> &nbsp; </label>
<span data-i18n="mongodb.label.clusteredVersion"></span>
</div>
<div class="form-row">
<label for="node-config-input-db"><i class="fa fa-database"></i> <span data-i18n="mongodb.label.database"></span></label>
@ -43,7 +49,8 @@
color: "rgb(218, 196, 180)",
defaults: {
hostname: {value: "127.0.0.1", required: true},
clustered: {value: true, required: true},
topology: {value: "direct", required: true},
connectOptions: {value: "", required: false},
port: {value: 27017, required: true},
db: {value: "", required: true},
name: {value: ""}
@ -56,19 +63,55 @@
return this.name || this.db + "@" + this.hostname;
},
oneditprepare: function() {
$("#node-config-input-clustered").on("change",function () {
if ( $("#node-config-input-clustered").is(":checked") ) {
$(".node-config-input-port").hide();
$(".node-config-clusterVersion").show();
} else {
$("#node-config-input-topology").on("change", function() {
var topology = $("#node-config-input-topology option:selected").val();
if (topology === "direct") {
$(".node-config-input-port").show();
$(".node-config-clusterVersion").hide();
} else {
$(".node-config-input-port").hide();
}
})
});
},
});
</script>
<script type="text/x-red" data-help-name="mongodb">
<p>Define a connection method to your MongoDB server instance.</p>
<p>There are 3 supported options:
<details><summary>Standard/direct</summary>
For databases that request connections in the form
<code>
mongodb://[username]:[password]@[hostname]:[port]/[dbname]
</code>
Most often used for local MongoDB instances (localhost:27017), and other stand-alone instances.
</details>
<details><summary>Standard/replicaset</summary>
For databases that request connections in the form
<code>
mongodb://[username]:[password]@[hostnameA]:[port],[hostnameB]:[port]/[dbname]?replicaSet=[replsetname]
</code>
Often used with <q>database as a service</q> offerings,
or on-premises instances that have been configured for availability and resilience
</details>
<details><summary>Clustered by DNS seedlist</summary>
For databases that request connections in the form
<code>
mongodb+srv://[username]:[password]@[clustername]/[dbname]?retryWrites=true&w=majority
</code>
A configuration of MongoDB instances that provide availability and performance
through replication and sharding, accessed through a cluster alias name, rather than
by specific host:port connections. This is the default for MongoDB instances in the
<a href="https://www.mongodb.com/cloud/atlas" target="_blank">Atlas cloud service</a>.
</details>
<p><strong>Connect options</strong> is where you add the optional parameters required by your MongoDB instance.
This might include:
<ul><li>w=majority</li><li>replicaSet=replset</li><li>authSource=admin</li></ul> and any other options appropriate -
full set available at <a href="https://docs.mongodb.com/manual/reference/connection-string/" target="_blank">
Connection String URI Format — MongoDB Manual</a>.
<p>If you are connecting to <a href="https://cloud.ibm.com/catalog/services/databases-for-mongodb-group" target="_blank">
IBM Databases for MongoDB</a>, as a replica-set, be sure to append <code>ssl=true&tlsAllowInvalidCertificates=true </code>
to the <strong>Connect options</strong>.
</script>
<script type="text/x-red" data-template-name="mongodb out">
<div class="form-row">

View File

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

View File

@ -30,7 +30,7 @@ Usage
-----
Nodes to save and retrieve data in a MongoDB instance - the database server can be local (mongodb//:localhost:27017), remote (mongodb://hostname.network:27017),
and sharded cluster (mongodb+srv://clustername.network).
replica-set or cluster (mongodb://hostnameA.network:27017,hostnameB.network:27017), and DNS seedlist cluster (mongodb+srv://clustername.network).
Reference [MongoDB docs](https://docs.mongodb.com/manual/reference/connection-string/) to see which connection method (host or clustered) to use for your MongoDB instance.

View File

@ -2,8 +2,8 @@
"mongodb": {
"label": {
"host": "Host",
"clustered": "Clustered",
"clusteredVersion":"MongoDB databases in sharded cluster (>=3.4)",
"topology":"Connection topology",
"connectOptions":"Connect options",
"port": "Port",
"database": "Database",
"username": "Username",