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:
parent
d26cb057f9
commit
cde422f34a
@ -6,18 +6,24 @@
|
|||||||
<input class="input-append-left" type="text" id="node-config-input-hostname" placeholder="localhost">
|
<input class="input-append-left" type="text" id="node-config-input-hostname" placeholder="localhost">
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-row node-config-input-clustered">
|
<div class="form-row node-config-input-topology">
|
||||||
<label for="node-config-input-clustered"><i class="fa fa-sitemap"></i><span data-i18n="mongodb.label.clustered"></span></label>
|
<label for="node-config-input-topology"><span data-i18n="mongodb.label.topology"></span></label>
|
||||||
<input type="checkbox" id="node-config-input-clustered" style="width:20px;">
|
<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>
|
||||||
|
|
||||||
|
<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">
|
<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>
|
<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;">
|
<input type="text" id="node-config-input-port" style="width:55px;">
|
||||||
</div>
|
</div>
|
||||||
<div class="form-row node-config-clusterVersion">
|
|
||||||
<label> </label>
|
|
||||||
<span data-i18n="mongodb.label.clusteredVersion"></span>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-config-input-db"><i class="fa fa-database"></i> <span data-i18n="mongodb.label.database"></span></label>
|
<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)",
|
color: "rgb(218, 196, 180)",
|
||||||
defaults: {
|
defaults: {
|
||||||
hostname: {value: "127.0.0.1", required: true},
|
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},
|
port: {value: 27017, required: true},
|
||||||
db: {value: "", required: true},
|
db: {value: "", required: true},
|
||||||
name: {value: ""}
|
name: {value: ""}
|
||||||
@ -56,19 +63,55 @@
|
|||||||
return this.name || this.db + "@" + this.hostname;
|
return this.name || this.db + "@" + this.hostname;
|
||||||
},
|
},
|
||||||
oneditprepare: function() {
|
oneditprepare: function() {
|
||||||
$("#node-config-input-clustered").on("change",function () {
|
$("#node-config-input-topology").on("change", function() {
|
||||||
if ( $("#node-config-input-clustered").is(":checked") ) {
|
var topology = $("#node-config-input-topology option:selected").val();
|
||||||
$(".node-config-input-port").hide();
|
if (topology === "direct") {
|
||||||
$(".node-config-clusterVersion").show();
|
|
||||||
} else {
|
|
||||||
$(".node-config-input-port").show();
|
$(".node-config-input-port").show();
|
||||||
$(".node-config-clusterVersion").hide();
|
} else {
|
||||||
|
$(".node-config-input-port").hide();
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
</script>
|
</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">
|
<script type="text/x-red" data-template-name="mongodb out">
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
|
@ -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")});
|
||||||
|
@ -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),
|
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.
|
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.
|
||||||
|
|
||||||
|
@ -2,8 +2,8 @@
|
|||||||
"mongodb": {
|
"mongodb": {
|
||||||
"label": {
|
"label": {
|
||||||
"host": "Host",
|
"host": "Host",
|
||||||
"clustered": "Clustered",
|
"topology":"Connection topology",
|
||||||
"clusteredVersion":"MongoDB databases in sharded cluster (>=3.4)",
|
"connectOptions":"Connect options",
|
||||||
"port": "Port",
|
"port": "Port",
|
||||||
"database": "Database",
|
"database": "Database",
|
||||||
"username": "Username",
|
"username": "Username",
|
||||||
|
Loading…
Reference in New Issue
Block a user