Merge remote-tracking branch 'upstream/master'

This commit is contained in:
Kévin Michelet
2021-01-02 11:17:25 +01:00
254 changed files with 8408 additions and 10729 deletions

View File

@@ -1,16 +1,26 @@
<script type="text/x-red" data-template-name="leveldbase">
<script type="text/html" data-template-name="leveldbase">
<div class="form-row">
<label for="node-config-input-db"><i class="fa fa-briefcase"></i> Database</label>
<input type="text" id="node-config-input-db" placeholder="database path/name">
</div>
<div class="form-row">
<label for="node-config-input-encoding"><i class="fa fa-random"></i> Values are</label>
<select type="text" id="node-config-input-encoding">
<option value="utf8">text</option>
<option value="json">JSON</option>
<option value="binary">binary</option>
</select>
</div>
</script>
<script type="text/javascript">
RED.nodes.registerType('leveldbase',{
category: 'config',
defaults: {
db: {value:"",required:true}
db: {value:"",required:true},
encoding: {value:"utf8"}
},
label: function() {
return this.db;
@@ -18,7 +28,7 @@
});
</script>
<script type="text/x-red" data-template-name="leveldb in">
<script type="text/html" data-template-name="leveldb in">
<div class="form-row node-input-level">
<label for="node-input-level"><i class="fa fa-briefcase"></i> Database</label>
<input type="text" id="node-input-level">
@@ -29,7 +39,7 @@
</div>
</script>
<script type="text/x-red" data-help-name="leveldb in">
<script type="text/html" data-help-name="leveldb in">
<p>Uses <a href="https://code.google.com/p/leveldb/" target="_new"><i>LevelDB</i></a> for a simple key value pair database.</p>
<p>Use this node to <b>get</b>, or retrieve the data already saved in the database.</p>
<p><code>msg.topic</code> must hold the <i>key</i> for the database, and the result is returned in <code>msg.payload</code>.</p>
@@ -58,7 +68,7 @@
</script>
<script type="text/x-red" data-template-name="leveldb out">
<script type="text/html" data-template-name="leveldb out">
<div class="form-row node-input-level">
<label for="node-input-level"><i class="fa fa-briefcase"></i> Database</label>
<input type="text" id="node-input-level">
@@ -77,7 +87,7 @@
</script>
<script type="text/x-red" data-help-name="leveldb out">
<script type="text/html" data-help-name="leveldb out">
<p>Uses <a href="https://code.google.com/p/leveldb/" target="_new"><i>LevelDB</i></a> for a simple key value pair database.</p>
<p>Use this node to either <b>put</b> (store) the <code>msg.payload</code> to the named database file, using <code>msg.topic</code> as the key.</p>
<p>To <b>delete</b> information select delete in the properties dialogue and again use <code>msg.topic</code> as the key.</b>.</p>

View File

@@ -6,9 +6,10 @@ module.exports = function(RED) {
function LevelNode(n) {
RED.nodes.createNode(this,n);
this.dbname = n.db;
this.encoding = n.encoding || "utf8";
this.ready = false;
var node = this;
lvldb(this.dbname, function(err, db) {
lvldb(this.dbname, {valueEncoding:this.encoding}, function(err, db) {
if (err) { node.error(err); }
node.db = db;
node.db.on('ready', function() { node.ready = true; });

View File

@@ -1,9 +1,9 @@
{
"name" : "node-red-node-leveldb",
"version" : "0.1.0",
"version" : "0.3.0",
"description" : "A Node-RED node to read and write to a LevelDB database",
"dependencies" : {
"level" : "^4.0.0"
"level" : "^6.0.0"
},
"repository" : {
"type":"git",

View File

@@ -1,10 +1,29 @@
<script type="text/x-red" data-template-name="mongodb">
<script type="text/html" data-template-name="mongodb">
<div class="form-row">
<label for="node-config-input-hostname"><i class="fa fa-bookmark"></i> <span data-i18n="mongodb.label.host"></span></label>
<input class="input-append-left" type="text" id="node-config-input-hostname" placeholder="localhost" style="width: 40%;" >
<label for="node-config-input-port" style="margin-left: 10px; width: 35px; "> <span data-i18n="mongodb.label.port"></span></label>
<input type="text" id="node-config-input-port" style="width:45px">
<label for="node-config-input-hostname"><i class="fa fa-bookmark"></i><span data-i18n="mongodb.label.host"></span></label>
<input class="input-append-left" type="text" id="node-config-input-hostname" placeholder="localhost">
</div>
<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">
<label for="node-config-input-db"><i class="fa fa-database"></i> <span data-i18n="mongodb.label.database"></span></label>
<input type="text" id="node-config-input-db">
@@ -28,23 +47,72 @@
category: 'config',
color: "rgb(218, 196, 180)",
defaults: {
hostname: { value: "127.0.0.1", required: true },
port: { value: 27017, required: true },
db: { value: "", required: true },
name: { value: "" }
hostname: {value: "127.0.0.1", required: true},
topology: {value: "direct", required: true},
connectOptions: {value: "", required: false},
port: {value: 27017, required: true},
db: {value: "", required: true},
name: {value: ""}
},
credentials: {
user: { type: "text" },
password: { type: "password" }
},
label: function () {
return this.name || this.hostname + ":" + this.port + "/" + this.db;
}
label: function() {
return this.name || this.db + "@" + this.hostname;
},
oneditprepare: function() {
$("#node-config-input-topology").on("change", function() {
var topology = $("#node-config-input-topology option:selected").val();
if (topology === "direct") {
$(".node-config-input-port").show();
} else {
$(".node-config-input-port").hide();
}
});
},
});
</script>
<script type="text/html" 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/html" data-template-name="mongodb out">
<div class="form-row">
<label for="node-input-mongodb"><i class="fa fa-bookmark"></i> <span data-i18n="mongodb.label.server"></span></label>
<input type="text" id="node-input-mongodb">
@@ -84,29 +152,28 @@
<div class="form-tips" id="node-warning" style="display: none"><span data-i18n="[html]mongodb.tip"></span></div>
</script>
<script type="text/x-red" data-help-name="mongodb out">
<script type="text/html" data-help-name="mongodb out">
<p>A simple MongoDB output node. Can save, insert, update and remove objects from a chosen collection.</p>
<p>Save will update an existing object or insert a new object if one does not already exist.</p>
<p>Insert will insert a new object.</p>
<p>Save and insert either store <code>msg</code> or <code>msg.payload</code>.</p>
<p>Update will modify an existing object or objects. The query to select objects to update uses <code>msg.query</code>,
<p>Update will modify an existing object or objects. The query to select objects to update uses <code>msg.query</code>
and the update to the element uses <code>msg.payload</code>. If <code>msg.query._id</code> is
a valid mongo ObjectId string it will be converted to an ObjectId type.</p>
<p>Update can add an object if it does not exist or update multiple objects.</p>
<p>Update can add a object if it does not exist or update multiple objects.</p>
<p>Remove will remove objects that match the query passed in on <code>msg.payload</code>. A blank query will delete
<i>all of the objects</i> in the collection.</p>
<p>You can either set the collection method in the node config or on <code>msg.collection</code>. Setting it in the
node will override <code>msg.collection</code>.</p>
<p>By default, MongoDB creates an <i>_id</i> property as the primary key, so repeated injections of the
<p>By default MongoDB creates an <i>_id</i> property as the primary key - so repeated injections of the
same <code>msg</code> will result in many database entries.</p>
<p>If this is NOT the desired behaviour, i.e., you want repeated entries to overwrite, then you must set
<p>If this is NOT the desired behaviour - ie. you want repeated entries to overwrite, then you must set
the <code>msg._id</code> property to be a constant by the use of a previous function node.</p>
<p>This could be a unique constant or you could create one based on some other msg property.</p>
<p>Currently we do not limit or cap the collection size, however this may well change.</p>
<p>Currently we do not limit or cap the collection size at all... this may well change.</p>
</script>
<script type="text/javascript">
function oneditprepare() {
$("#node-input-operation").change(function () {
var id = $("#node-input-operation option:selected").val();
@@ -159,7 +226,7 @@
</script>
<script type="text/x-red" data-template-name="mongodb in">
<script type="text/html" data-template-name="mongodb in">
<div class="form-row">
<label for="node-input-mongodb"><i class="fa fa-bookmark"></i> <span data-i18n="mongodb.label.server"></span></label>
<input type="text" id="node-input-mongodb">
@@ -183,12 +250,12 @@
<div class="form-tips" id="node-warning" style="display: none"><span data-i18n="[html]mongodb.tip"></span></div>
</script>
<script type="text/x-red" data-help-name="mongodb in">
<script type="text/html" data-help-name="mongodb in">
<p>Calls a MongoDB collection method based on the selected operator.</p>
<p>Find queries a collection using the <code>msg.payload</code> as the query statement as per the .find() function.
Optionally, you may also set a <code>msg.projection</code> object (via a function) to constrain the returned
fields. You can also set a <code>msg.sort</code> object, a <code>msg.limit</code> number and a <code>msg.skip</code> number.</p>
<p>Count returns a count of the number of documents in a collection, or matches a query using the
Optionally, you may also (via a function) set a <code>msg.projection</code> object to constrain the returned
fields, a <code>msg.sort</code> object, a <code>msg.limit</code> number and a <code>msg.skip</code> number.</p>
<p>Count returns a count of the number of documents in a collection or matching a query using the
<code>msg.payload</code> as the query statement.</p>
<p>Aggregate provides access to the aggregation pipeline using the <code>msg.payload</code> as the pipeline array.</p>
<p>You can either set the collection method in the node config or on <code>msg.collection</code>. Setting it in
@@ -199,7 +266,6 @@
</script>
<script type="text/javascript">
RED.nodes.registerType('mongodb in', {
category: 'storage-input',
color: "rgb(218, 196, 180)",

View File

@@ -1,5 +1,6 @@
module.exports = function (RED) {
module.exports = function(RED) {
"use strict";
var mongo = require('mongodb');
var ObjectID = require('mongodb').ObjectID;
@@ -11,13 +12,37 @@ module.exports = function (RED) {
this.port = n.port;
this.db = n.db;
this.name = n.name;
this.connectOptions= n.connectOptions;
this.topology = n.topology;
//console.log(this);
var clustered = (this.topology !== "direct") || false;
var url = "mongodb://";
if (this.credentials && this.credentials.user && this.credentials.password) {
url += this.credentials.user + ":" + this.credentials.password + "@";
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;
} else {
this.user = n.user;
this.password = n.password;
}
if (this.user) {
url += this.user+":"+this.password+"@";
}
if (clustered) {
url += this.hostname + "/" + this.db
} else {
url += this.hostname + ":" + this.port + "/" + this.db;
}
if (this.connectOptions){
url += "?" + this.connectOptions;
}
url += this.hostname + ":" + this.port + "/" + this.db;
console.log("MongoDB URL: " + url);
this.url = url;
}
@@ -44,12 +69,12 @@ 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") });
this.status({fill:"grey",shape:"ring",text:RED._("mongodb.status.connecting")});
var node = this;
var noerror = true;
var connectToDB = function () {
MongoClient.connect(node.mongoConfig.url, function (err, db) {
var connectToDB = function() {
MongoClient.connect(node.mongoConfig.url, function(err, client) {
if (err) {
node.status({ fill: "red", shape: "ring", text: RED._("mongodb.status.error") });
if (noerror) { node.error(err); }
@@ -57,10 +82,13 @@ module.exports = function (RED) {
node.tout = setTimeout(connectToDB, 10000);
}
else {
node.status({ fill: "green", shape: "dot", text: RED._("mongodb.status.connected") });
node.clientDb = db;
node.status({fill:"green",shape:"dot",text:RED._("mongodb.status.connected")});
node.clientDb = client.db();
var db = client.db();
//console.log( db);
noerror = true;
var coll;
if (node.collection) {
coll = db.collection(node.collection);
}
@@ -118,8 +146,9 @@ module.exports = function (RED) {
var node = this;
var noerror = true;
var connectToDB = function () {
MongoClient.connect(node.mongoConfig.url, function (err, db) {
var connectToDB = function() {
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") });
if (noerror) { node.error(err); }
@@ -127,8 +156,9 @@ module.exports = function (RED) {
node.tout = setTimeout(connectToDB, 10000);
}
else {
node.status({ fill: "green", shape: "dot", text: RED._("mongodb.status.connected") });
node.clientDb = db;
node.status({fill:"green",shape:"dot",text:RED._("mongodb.status.connected")});
node.clientDb = client.db();
var db = client.db();
noerror = true;
var coll;
node.on("input", function (msg) {
@@ -161,10 +191,10 @@ module.exports = function (RED) {
skip = 0;
}
coll.find(selector, msg.projection).sort(msg.sort).limit(limit).skip(skip).toArray(function (err, items) {
coll.find(selector).project(msg.projection).sort(msg.sort).limit(limit).skip(skip).toArray(function(err, items) {
if (err) {
node.error(err);
}
> }
else {
msg.payload = items;
delete msg.projection;
@@ -189,13 +219,21 @@ module.exports = function (RED) {
}
else if (node.operation === "aggregate") {
msg.payload = (Array.isArray(msg.payload)) ? msg.payload : [];
coll.aggregate(msg.payload, function (err, result) {
coll.aggregate(msg.payload, function(err, cursor) {
if (err) {
node.error(err);
}
else {
msg.payload = result;
node.send(msg);
cursor.toArray(function(cursorError, cursorDocs) {
//console.log(cursorDocs);
if (cursorError) {
node.error(cursorError);
}
else {
msg.payload = cursorDocs;
node.send(msg);
}
});
}
});
}

View File

@@ -16,13 +16,23 @@ Install
-------
Run the following command in your Node-RED user directory - typically `~/.node-red`
```
npm install node-red-node-mongodb
```
Note that this package requires a MongoDB client package at least version 3.6.1 - if you have an older (version 2) client,
you may need to remove that before installing this
```
npm remove mongodb
npm install node-red-node-mongodb
```
Usage
-----
Nodes to save and retrieve data in a local MongoDB instance.
Nodes to save and retrieve data in a MongoDB instance - the database server can be local (mongodb//:localhost:27017), remote (mongodb://hostname.network:27017),
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.
### Input

View File

@@ -2,6 +2,8 @@
"mongodb": {
"label": {
"host": "Host",
"topology":"Connection topology",
"connectOptions":"Connect options",
"port": "Port",
"database": "Database",
"username": "Username",

View File

@@ -0,0 +1,38 @@
{
"mongodb": {
"label": {
"host": "ホスト",
"topology":"接続トポロジ",
"connectOptions":"接続オプション",
"port": "ポート",
"database": "データベース",
"username": "ユーザ",
"password": "パスワード",
"server": "サーバ",
"collection": "コレクション",
"operation": "操作",
"onlystore": "msg.payloadオブジェクトのみ保存",
"createnew": "一致しなければ新しいドキュメントを作成",
"updateall": "合致する全ドキュメントを更新"
},
"operation": {
"save": "save",
"insert": "insert",
"update": "update",
"remove": "remove",
"find": "find",
"count": "count",
"aggregate": "aggregate"
},
"status": {
"connecting": "接続中",
"connected": "接続しました",
"error": "エラー"
},
"tip": "<b> Tip:</b> コレクションが設定されていない場合、 <code>msg.collection</code>がコレクション名として使われます。",
"errors": {
"nocollection": "コレクションが定義されていません",
"missingconfig": "mongodbの設定がみつかりません"
}
}
}

View File

@@ -1,9 +1,9 @@
{
"name" : "node-red-node-mongodb",
"version" : "0.0.14",
"version" : "0.2.4",
"description" : "Node-RED nodes to talk to an Mongo database",
"dependencies" : {
"mongodb" : "^2.2.34"
"mongodb" : "^3.6.2"
},
"repository" : {
"type":"git",
@@ -20,5 +20,11 @@
"name": "Dave Conway-Jones",
"email": "ceejay@vnet.ibm.com",
"url": "http://nodered.org"
},
"contributors": [
{
"name": "Ross Cruickshank",
"email": "ross@vnet.ibm.com"
}
]
}

View File

@@ -1,5 +1,5 @@
<script type="text/x-red" data-template-name="MySQLdatabase">
<script type="text/html" data-template-name="MySQLdatabase">
<div class="form-row">
<label for="node-config-input-host"><i class="fa fa-globe"></i> Host</label>
<input type="text" id="node-config-input-host">
@@ -24,31 +24,45 @@
<label for="node-config-input-tz"><i class="fa fa-clock-o"></i> Timezone</label>
<input type="text" id="node-config-input-tz">
</div>
<div class="form-row">
<label for="node-config-input-charset"><i class="fa fa-language"></i> Charset</label>
<input type="text" id="node-config-input-charset">
</div>
<div class="form-row">
<label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-config-input-name" placeholder="Name">
</div>
</script>
<script type="text/javascript">
RED.nodes.registerType('MySQLdatabase',{
category: 'config',
defaults: {
name: {value:""},
host: {value:"127.0.0.1",required:true},
port: {value:"3306",required:true},
//user: {value:"",required:true},
//pass: {value:"",required:true},
db: {value:"",required:true},
tz: {value:""}
tz: {value:""},
charset: {value:"UTF8"}
},
credentials: {
user: {type: "text"},
password: {type: "password"}
},
label: function() {
return this.db;
return this.name || this.db;
}
});
</script>
<script type="text/html" data-help-name="MySQLdatabase">
<p>Add the credentials for accessing your database here.</p>
<p>Timezone can be set like GMT, EST5EDT, UTC, etc</p>
<p>The Charset defaults to the "old" 3 byte Mysql UTF8. If you need support for emojis etc then use UTF8MB4.</p>
</script>
<script type="text/x-red" data-template-name="mysql">
<script type="text/html" data-template-name="mysql">
<div class="form-row">
<label for="node-input-mydb"><i class="fa fa-database"></i> Database</label>
<input type="text" id="node-input-mydb">
@@ -59,7 +73,7 @@
</div>
</script>
<script type="text/x-red" data-help-name="mysql">
<script type="text/html" data-help-name="mysql">
<p>Allows basic access to a MySQL database.</p>
<p>This node uses the <b>query</b> operation against the configured database. This does allow both INSERTS and DELETES.
By its very nature it allows SQL injection... so <i>be careful out there...</i></p>
@@ -86,7 +100,7 @@
var levelNode = RED.nodes.node(this.mydb);
return this.name||(levelNode?levelNode.label():"mysql");
},
labelStyle: function() {
labelStyle: function() {
return this.name?"node_label_italic":"";
}
});

View File

@@ -9,6 +9,7 @@ module.exports = function(RED) {
this.host = n.host;
this.port = n.port;
this.tz = n.tz || "local";
this.charset = (n.charset || "UTF8_GENERAL_CI").toUpperCase();
this.connected = false;
this.connecting = false;
@@ -41,7 +42,8 @@ module.exports = function(RED) {
timezone : node.tz,
insecureAuth: true,
multipleStatements: true,
connectionLimit: 25
connectionLimit: 25,
charset: node.charset
});
}
@@ -86,6 +88,7 @@ module.exports = function(RED) {
if (this.tick) { clearTimeout(this.tick); }
if (this.check) { clearInterval(this.check); }
node.connected = false;
node.connection.release();
node.emit("state"," ");
node.pool.end(function (err) { done(); });
});
@@ -102,6 +105,7 @@ module.exports = function(RED) {
RED.nodes.createNode(this,n);
this.mydb = n.mydb;
this.mydbConfig = RED.nodes.getNode(this.mydb);
this.status({});
if (this.mydbConfig) {
this.mydbConfig.connect();
@@ -122,16 +126,36 @@ module.exports = function(RED) {
if (node.mydbConfig.connected) {
if (typeof msg.topic === 'string') {
//console.log("query:",msg.topic);
var bind = Array.isArray(msg.payload) ? msg.payload : [];
var bind = [];
if (Array.isArray(msg.payload)) { bind = msg.payload; }
else if (typeof msg.payload === 'object' && msg.payload !== null) {
bind=msg.payload;
node.mydbConfig.connection.config.queryFormat = function (query, values) {
if (!values){
return query;
}
return query.replace(/\:(\w+)/g, function (txt, key) {
if (values.hasOwnProperty(key)) {
return this.escape(values[key]);
}
return txt;
}.bind(this));
};
}
node.mydbConfig.connection.query(msg.topic, bind, function(err, rows) {
if (err) {
status = {fill:"red",shape:"ring",text:"Error: "+err.code};
node.status(status);
node.error(err,msg);
status = {fill:"red",shape:"ring",text:"Error"};
}
else {
msg.payload = rows;
if (rows.constructor.name === "OkPacket") {
msg.payload = JSON.parse(JSON.stringify(rows));
}
else { msg.payload = rows; }
node.send(msg);
status = {fill:"green",shape:"dot",text:"OK"};
node.status(status);
}
});
}

View File

@@ -5,9 +5,9 @@ A <a href="http://nodered.org" target="_new">Node-RED</a> node to read and write
Install
-------
Run the following command in your Node-RED user directory - typically `~/.node-red`
Either use the `Node-RED Menu - Manage Palette - Install`, or run the following command in your Node-RED user directory - typically `~/.node-red`
npm install node-red-node-mysql
npm i node-red-node-mysql
Usage
@@ -15,15 +15,45 @@ Usage
Allows basic access to a MySQL database.
This node uses the <b>query</b> operation against the configured database. This does allow both INSERTS and DELETES.
This node uses the **query** operation against the configured database. This does allow both INSERTS and DELETES.
By it's very nature it allows SQL injection... so <i>be careful out there...</i>
By its very nature it allows SQL injection... so *be careful out there...*
The `msg.topic` must hold the <i>query</i> for the database, and the result is returned in `msg.payload`.
The `msg.topic` must hold the *query* for the database, and the result is returned in `msg.payload`.
Typically the returned payload will be an array of the result rows.
If nothing is found for the key then <i>null</i> is returned.
If nothing is found for the key then *null* is returned.
The reconnect retry timeout in milliseconds can be changed by adding a line to <b>settings.js</b>
<pre>mysqlReconnectTime: 30000,</pre></p>
The reconnect retry timeout in milliseconds can be changed by adding a line to **settings.js**
```javascript
mysqlReconnectTime: 30000,
```
The timezone can be set like GMT, EST5EDT, UTC, etc.
The charset defaults to the "old" Mysql 3 byte UTF. If you need support for emojis etc then use UTF8MB4.
Preparing Queries
-----
```javascript
msg.payload=[24, 'example-user'];
msg.topic="INSERT INTO users (`userid`, `username`) VALUES (?, ?);"
return msg;
```
with named parameters:
```javascript
msg.payload={}
msg.payload.userToChange=42;
msg.payload.newUsername="example-user";
msg.topic="INSERT INTO users (`userid`, `username`) VALUES (:userToChange, :newUsername) ON DUPLICATE KEY UPDATE `username`=:newUsername;"
return msg;
```
Documentation
-----
<a href="https://www.npmjs.com/package/mysql" target="_new">Documentation</a> of the used Node.js package

View File

@@ -1,18 +1,21 @@
{
"name" : "node-red-node-mysql",
"version" : "0.0.18",
"description" : "A Node-RED node to read and write to a MySQL database",
"dependencies" : {
"mysql" : "^2.16.0"
"name": "node-red-node-mysql",
"version": "0.1.1",
"description": "A Node-RED node to read and write to a MySQL database",
"dependencies": {
"mysql": "^2.18.1"
},
"repository" : {
"type":"git",
"url":"https://github.com/node-red/node-red-nodes/tree/master/storage/mysql"
"repository": {
"type": "git",
"url": "https://github.com/node-red/node-red-nodes/tree/master/storage/mysql"
},
"license": "Apache-2.0",
"keywords": [ "node-red", "mysql" ],
"node-red" : {
"nodes" : {
"keywords": [
"node-red",
"mysql"
],
"node-red": {
"nodes": {
"mysql": "68-mysql.js"
}
},

View File

@@ -10,22 +10,27 @@ Run the following command in your Node-RED user directory - typically `~/.node-r
npm i --unsafe-perm node-red-node-sqlite
**Note**: the install process requires a compile of native code. This can take 15-20 minutes on
devices like a Raspberry Pi - please be prepared to wait a long time. Also if node.js is upgraded at any point you will need to rebuild the native part manually, for example.
cd ~/.node-red
npm rebuild
Usage
-----
Allows basic access to a Sqlite database.
This node uses the <b>db.all</b> operation against the configured database.
This node uses the **db.all** operation against the configured database.
This does allow INSERTS, UPDATES and DELETES.
By it's very nature it is SQL injection... so *be careful* out there...
`msg.topic` must hold the <i>query</i> for the database, and the result is returned in `msg.payload`.
`msg.topic` must hold the *query* for the database, and the result is returned in `msg.payload`.
Typically the returned payload will be an array of the result rows, (or an error).
You can load sqlite extensions by inputting a <code>msg.extension</code> property containing the full path and filename.
You can load sqlite extensions by inputting a `msg.extension` property containing the full path and filename.
The reconnect timeout in milliseconds can be changed by adding a line to **settings.js**

View File

@@ -0,0 +1,28 @@
<script type="text/html" data-help-name="sqlite">
<p>Allows access to a SQLite database.</p>
<p>SQL Query sets how the query is passed to the node.</p>
<p>SQL Query <i>Via msg.topic</i> and <i>Fixed Statement</i> uses the <b>db.all</b> operation against the configured database. This does allow INSERTS, UPDATES and DELETES.
By its very nature it is SQL injection... so <i>be careful out there...</i></p>
<p>SQL Type <i>Prepared Statement</i> also uses <b>db.all</b> but sanitizes parameters passed, eliminating the possibility of SQL injection.</p>
<p>SQL Type <i>Batch without response</i> uses <b>db.exec</b> which runs all SQL statements in the provided string. No result rows are returned.</p>
<p>When using <i>Via msg.topic</i> or <i>Batch without response</i> <code>msg.topic</code> must hold the <i>query</i> for the database.</p>
<p>When using Normal or Prepared Statement, the <i>query</i> must be entered in the node config.</p>
<p>Pass in the parameters as an object in <code>msg.params</code> for Prepared Statement. Ex:<br />
<code>msg.params = {<br />
&nbsp;&nbsp;&nbsp;&nbsp;$id:1,<br />
&nbsp;&nbsp;&nbsp;&nbsp;$name:"John Doe"<br />
}</code><br />
Parameter object names must match parameters set up in the Prepared Statement. If you get the error <code>SQLITE_RANGE: bind or column index out of range</code>
be sure to include $ on the parameter object key.<br />
The SQL query for the example above could be: <code>insert into user_table (user_id, user) VALUES ($id, $name);</code></p>
<p>Using any SQL Query, the result is returned in <code>msg.payload</code></p>
<p>Typically the returned payload will be an array of the result rows, (or an error).</p>
<p>You can load SQLite extensions by inputting a <code>msg.extension</code> property containing the full
path and filename.</p>
<p>The reconnect timeout in milliseconds can be changed by adding a line to <b>settings.js</b>
<pre>sqliteReconnectTime: 20000,</pre></p>
</script>
<script type="text/html" data-help-name="sqlitedb">
<p>The default directory for the database file is the user's home directory through which the Node-RED process was started. You can specify absolute path to change it.</p>
</script>

View File

@@ -0,0 +1,20 @@
{
"sqlite": {
"label": {
"database": "Database",
"sqlQuery": "SQL Query",
"viaMsgTopic": "Via msg.topic",
"fixedStatement": "Fixed Statement",
"preparedStatement": "Prepared Statement",
"batchWithoutResponse": "Batch without response",
"sqlStatement": "SQL Statement",
"mode": "Mode",
"readWriteCreate": "Read-Write-Create",
"readWrite": "Read-Write",
"readOnly": "Read-Only"
},
"tips": {
"memoryDb": "<b>Note</b>: Setting the database name to <code>:memory:</code> will create a non-persistant in memory database."
}
}
}

View File

@@ -0,0 +1,26 @@
<script type="text/html" data-help-name="sqlite">
<p>SQLiteデータベースにアクセスする機能を提供します</p>
<p>SQLクエリには本ノードへどの様にクエリを渡すかを設定します</p>
<p><i>msg.topic経由</i> と <i>固定文</i> のSQLクエリは設定したデータベースに対して <b>db.all</b> 操作を実行します。これによって、INSERTSとUPDATES、DELETESを利用できます。性質上、SQLインジェクションに<i>注意してください</i></p>
<p><i>事前定義文</i> SQL <b>db.all</b> 使SQL</p>
<p><i>一括(応答なし)</i> SQLSQL <b>db.exec</b> 使</p>
<p><i>msg.topic経由</i> または <i>一括(応答なし)</i> を用いる時 <code>msg.topic</code> には、データベースに問い合わせるための <i>クエリ</i> が格納されている必要があります</p>
<p>通常の方法や事前定義文を用いる時 <i>クエリ</i> </p>
<p>事前定義文を用いるためには <code>msg.params</code> をオブジェクトとしてパラメータに渡します。例:<br />
<code>msg.params = {<br />
&nbsp;&nbsp;&nbsp;&nbsp;$id:1,<br />
&nbsp;&nbsp;&nbsp;&nbsp;$name:"John Doe"<br />
}</code><br />
パラメータのオブジェクト名は事前定義文に設定したパラメータと一致させる必要があります
もし <code>SQLITE_RANGE: bind or column index out of range</code> というエラーが発生した場合は、バラメータのオブジェクトのキーに$を含めてください。<br />
上の例で用いるSQLクエリは次の様になります: <code>insert into user_table (user_id, user) VALUES ($id, $name);</code></p>
<p>SQLクエリを使用すると <code>msg.payload</code> </p>
<p>通常返されるペイロードは結果の行から成る配列(またはエラー)になります</p>
<p>フルパスやファイル名を含む <code>msg.extension</code> SQLite</p>
<p>ミリ秒単位の再接続タイムアウトは<b>settings.js</b>
<pre>sqliteReconnectTime: 20000,</pre></p>
</script>
<script type="text/html" data-help-name="sqlitedb">
<p>データベースファイルのデフォルトディレクトリはNode-REDプロセスを開始したユーザのホームディレクトリですこれは絶対パスを用いることで変更できます</p>
</script>

View File

@@ -0,0 +1,20 @@
{
"sqlite": {
"label": {
"database": "データベース",
"sqlQuery": "SQLクエリ",
"viaMsgTopic": "msg.topic経由",
"fixedStatement": "固定文",
"preparedStatement": "事前定義文",
"batchWithoutResponse": "一括(応答なし)",
"sqlStatement": "SQL文",
"mode": "モード",
"readWriteCreate": "読み取り-書き込み-作成",
"readWrite": "読み取り-書き込み",
"readOnly": "読み取りのみ"
},
"tips": {
"memoryDb": "<b>注釈</b>: データベース名に <code>:memory:</code> を設定すると、非永続的なメモリデータベースを作成します。"
}
}
}

View File

@@ -1,13 +1,13 @@
{
"name": "node-red-node-sqlite",
"version": "0.3.6",
"version": "0.4.4",
"description": "A sqlite node for Node-RED",
"dependencies": {
"sqlite3": "^4.0.4"
"sqlite3": "~4.2.0"
},
"repository": {
"type": "git",
"url": "https://github.com/node-red/node-red-nodes/storage/sqlite/"
"url": "https://github.com/node-red/node-red-nodes/tree/master/storage/sqlite"
},
"license": "Apache-2.0",
"keywords": [

View File

@@ -1,19 +1,17 @@
<script type="text/x-red" data-template-name="sqlitedb">
<script type="text/html" data-template-name="sqlitedb">
<div class="form-row">
<label for="node-config-input-db"><i class="fa fa-database"></i> Database</label>
<label for="node-config-input-db"><i class="fa fa-database"></i> <span data-i18n="sqlite.label.database"></label>
<input type="text" id="node-config-input-db" placeholder="/tmp/sqlite">
</div>
<div class="form-row">
<label for="node-config-input-mode">Mode</label>
<label for="node-config-input-mode" data-i18n="sqlite.label.mode"></label>
<select id="node-config-input-mode" style="width:70%">
<option value="RWC">Read-Write-Create</option>
<option value="RW">Read-Write</option>
<option value="RO">Read-Only</option>
<option value="RWC" data-i18n="sqlite.label.readWriteCreate"></option>
<option value="RW" data-i18n="sqlite.label.readWrite"></option>
<option value="RO" data-i18n="sqlite.label.readOnly"></option>
</select>
</div>
<div class="form-tips"><b>Note</b>: Setting the database name to <code>:memory:</code>
will create a non-persistant in memory database.</div>
<div class="form-tips" data-i18n="[html]sqlite.tips.memoryDb"></div>
</script>
<script type="text/javascript">
@@ -29,27 +27,26 @@
});
</script>
<script type="text/x-red" data-template-name="sqlite">
<script type="text/html" data-template-name="sqlite">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="node-red:common.label.name"></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]node-red:common.label.name">
</div>
<div class="form-row">
<label for="node-input-mydb"><i class="fa fa-database"></i> Database</label>
<label for="node-input-mydb"><i class="fa fa-database"></i> <span data-i18n="sqlite.label.database"></label>
<input type="text" id="node-input-mydb">
</div>
<div class="form-row">
<label for=""><i class="fa fa-code"></i> SQL Query</label>
<label for=""><i class="fa fa-code"></i> <span data-i18n="sqlite.label.sqlQuery"></label>
<select id="node-input-sqlquery">
<option value="msg.topic">Via msg.topic</option>
<option value="fixed">Fixed Statement</option>
<option value="prepared">Prepared Statement</option>
<option value="batch">Batch without response</option>
<option value="msg.topic" data-i18n="sqlite.label.viaMsgTopic"></option>
<option value="fixed" data-i18n="sqlite.label.fixedStatement"></option>
<option value="prepared" data-i18n="sqlite.label.preparedStatement"></option>
<option value="batch" data-i18n="sqlite.label.batchWithoutResponse"></option>
</select>
</div>
<div class="form-row" style="margin-bottom: 0px;">
<label for="" style="width: unset;" id="node-input-sqllabel"><i class="fa fa-code"></i> SQL Statement</label>
<label for="" style="width: unset;" id="node-input-sqllabel"><i class="fa fa-code"></i> <span data-i18n="sqlite.label.sqlStatement"></label>
</div>
<div>
<input type="hidden" id="node-input-sql" autofocus="autofocus">
@@ -59,34 +56,6 @@
</div>
</script>
<script type="text/x-red" data-help-name="sqlite">
<p>Allows access to a Sqlite database.</p>
<p>SQL Query sets how the query is passed to the node.</p>
<p>SQL Query <i>Via msg.topic</i> and <i>Fixed Statement</i> uses the <b>db.all</b> operation against the configured database. This does allow INSERTS, UPDATES and DELETES.
By its very nature it is SQL injection... so <i>be careful out there...</i></p>
<p>SQL Type <i>Prepared Statement</i> also uses <b>db.all</b> but sanitizes parameters passed, eliminating the possibility of SQL injection.</p>
<p>SQL Type <i>Batch without response</i> uses <b>db.exec</b> which runs all SQL statements in the provided string. No result rows are returned.</p>
<p>When using <i>Via msg.topic</i> or <i>Batch without response</i> <code>msg.topic</code> must hold the <i>query</i> for the database.</p>
<p>When using Normal or Prepared the <i>query</i> must be entered in the node config.</p>
<p>Pass in the parameters as an object in <code>msg.params</code> for Prepared. Ex:<br />
<code>msg.params = {<br />
&nbsp;&nbsp;&nbsp;&nbsp;$id:1,<br />
&nbsp;&nbsp;&nbsp;&nbsp;$name:"John Doe"<br />
}</code><br />
Parameter object names must match parameters set up in the Prepared Statement. If you get the error <code>SQLITE_RANGE: bind or column index out of range</code>
be sure to include $ on the parameter object key.</p>
<p>Using any SQL Query, the result is returned in <code>msg.payload</code></p>
<p>Typically the returned payload will be an array of the result rows, (or an error).</p>
<p>You can load sqlite extensions by inputting a <code>msg.extension</code> property containing the full
path and filename.</p>
<p>The reconnect timeout in milliseconds can be changed by adding a line to <b>settings.js</b>
<pre>sqliteReconnectTime: 20000,</pre></p>
</script>
<script type="text/x-red" data-help-name="sqlitedb">
<p>The default directory for the database file is the user's home directory through which the NR process was started. You can specify absolute path to change it.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('sqlite',{
category: 'storage-input',

View File

@@ -14,7 +14,8 @@ module.exports = function(RED) {
var node = this;
node.doConnect = function() {
node.db = node.db || new sqlite3.Database(node.dbname,node.mode);
if (node.db) { return; }
node.db = new sqlite3.Database(node.dbname,node.mode);
node.db.on('open', function() {
if (node.tick) { clearTimeout(node.tick); }
node.log("opened "+node.dbname+" ok");
@@ -49,7 +50,7 @@ module.exports = function(RED) {
var bind = [];
var doQuery = function(msg) {
if (node.sqlquery == "msg.topic"){
if (node.sqlquery == "msg.topic") {
if (typeof msg.topic === 'string') {
if (msg.topic.length > 0) {
bind = Array.isArray(msg.payload) ? msg.payload : [];
@@ -84,7 +85,7 @@ module.exports = function(RED) {
node.status({fill:"red", shape:"dot",text:"msg.topic error"});
}
}
if (node.sqlquery == "fixed"){
if (node.sqlquery == "fixed") {
if (typeof node.sql === 'string') {
if (node.sql.length > 0) {
node.mydbConfig.db.all(node.sql, bind, function(err, row) {
@@ -96,14 +97,14 @@ module.exports = function(RED) {
});
}
}
else{
else {
if (node.sql === null || node.sql == "") {
node.error("SQL statement config not set up",msg);
node.status({fill:"red",shape:"dot",text:"SQL config not set up"});
}
}
}
if (node.sqlquery == "prepared"){
if (node.sqlquery == "prepared") {
if (typeof node.sql === 'string' && typeof msg.params !== "undefined" && typeof msg.params === "object") {
if (node.sql.length > 0) {
node.mydbConfig.db.all(node.sql, msg.params, function(err, row) {

View File

@@ -1,4 +1,3 @@
<script type="text/x-red" data-template-name="tail">
<div class="form-row">
<label for="node-input-filename"><i class="fa fa-file"></i> <span data-i18n="tail.label.filename"></span></label>
@@ -24,24 +23,15 @@
</div>
</script>
<script type="text/x-red" data-help-name="tail">
<p>Tails (watches for things to be added) to the configured file. (Linux/Mac ONLY)</p>
<p>This will not work on Windows filesystems, as it relies on the <b>tail -F</b> command.</p>
<h3>Outputs</h3>
<ul>
<li>Text (UTF-8) files will be returned as strings.</li>
<li>Binary files will be returned as Buffer objects.</li>
</ul>
</script>
<script type="text/javascript">
RED.nodes.registerType('tail',{
category: 'storage-input',
defaults: {
name: {value:""},
filetype: {value:"text"},
split: {value:"[\r]{0,1}\n"},
filename: {value:"",required:true}
split: {value:"[\\r]{0,1}\\n"},
filename: {value:""},
inputs: {value:0}
},
color:"BurlyWood",
inputs:0,
@@ -58,6 +48,11 @@
if (this.value === "text") { $("#node-tail-split").show(); }
else { $("#node-tail-split").hide(); }
});
}
},
oneditsave: function() {
var that = this;
if ($("#node-input-filename").val()==="") { that.inputs = 1; }
else { that.inputs = 0; }
}
});
</script>

View File

@@ -7,9 +7,9 @@ module.exports = function(RED) {
function TailNode(n) {
RED.nodes.createNode(this,n);
this.filename = n.filename;
this.filename = n.filename || "";
this.filetype = n.filetype || "text";
this.split = new RegExp(n.split || "[\r]{0,1}\n");
this.split = new RegExp(n.split.replace(/\\r/g,'\r').replace(/\\n/g,'\n').replace(/\\t/g,'\t') || "[\r]{0,1}\n");
var node = this;
var fileTail = function() {
@@ -30,23 +30,43 @@ module.exports = function(RED) {
}
else {
msg.payload = Buffer.from(data,"binary");
//msg.payload = data;
node.send(msg);
}
}
});
node.tail.on("error", function(err) {
node.status({ fill: "red",shape:"ring", text: "node-red:common.status.error" });
node.error(err.toString());
});
}
else {
node.tout = setTimeout(function() { fileTail(); },10000);
node.warn(RED._("tail.errors.filenotfound") + node.filename);
node.warn(RED._("tail.errors.filenotfound") + ": "+node.filename);
}
}
fileTail();
if (node.filename !== "") {
node.status({});
fileTail();
} else {
node.status({ fill: "grey", text: "tail.state.stopped" });
node.on('input', function (msg) {
if (!msg.hasOwnProperty("filename")) {
node.error(RED._("tail.state.nofilename"));
} else if (msg.filename === "") {
node.filename = "";
if (node.tail) { node.tail.unwatch(); }
if (node.tout) { clearTimeout(node.tout); }
node.status({ fill: "grey", text: "tail.state.stopped" });
} else {
node.filename = msg.filename;
if (node.tail) { node.tail.unwatch(); }
if (!node.tout) { fileTail(); }
node.status({ fill: "green", text: node.filename });
}
});
}
node.on("close", function() {
/* istanbul ignore else */

View File

@@ -0,0 +1,15 @@
<script type="text/html" data-help-name="tail">
<p>Tails (watches for things to be added) to the configured file.</p>
<p>Note: On Windows you may need to close the file between writes for any updates to be registered.</p>
<h3>Input</h3>
<dl class="message-properties">
<dt class="optional">filename <span class="property-type">string</span></dt>
<dd>If not configured in the node, this optional property sets the name of the file to be tailed.
If an empty string <code>""</code> is received, the tailing will stop.</dd>
</dl>
<h3>Outputs</h3>
<ul>
<li>Text (UTF-8) files will be returned as strings.</li>
<li>Binary files will be returned as Buffer objects.</li>
</ul>
</script>

View File

@@ -15,6 +15,10 @@
"errors": {
"windowsnotsupport": "Not currently supported on Windows.",
"filenotfound": "File not found"
},
"state":{
"stopped": "stopped",
"nofilename":"Missing filename on input"
}
}
}

View File

@@ -14,9 +14,9 @@
limitations under the License.
-->
<script type="text/x-red" data-help-name="tail">
<p>設定したファイルの末尾を出力(追加されたデータを監視)します(Linux/Macのみ)</p>
<p>このノードは<b>tail -F</b>Windows</p>
<script type="text/html" data-help-name="tail">
<p>設定したファイルの末尾を出力(追加されたデータを監視)します.</p>
<p>: Windowsでは追加されたデータを出力するには書き込みと書き込みの間にファイルを閉じる必要があります</p>
<h3>出力</h3>
<ul>
<li>(UTF-8形式の)テキストファイルは文字列を返却</li>

View File

@@ -13,7 +13,12 @@
"binary": "バイナリバッファ"
},
"errors": {
"windowsnotsupport": "現在Windows上での動作は対応していません"
"windowsnotsupport": "現在Windows上での動作は対応していません",
"filenotfound": "ファイルが見つかりませんでした"
},
"state":{
"stopped": "停止",
"nofilename":"ファイル名の指定がありません"
}
}
}

View File

@@ -1,9 +1,9 @@
{
"name": "node-red-node-tail",
"version": "0.0.2",
"version": "0.1.1",
"description": "A node to tail files for Node-RED",
"dependencies": {
"tail": "^2.0.0"
"tail": "^2.0.3"
},
"repository": {
"type": "git",