mirror of
https://github.com/node-red/node-red-nodes.git
synced 2025-03-01 10:37:43 +00:00
Add sqlite node to node-red-nodes (and npm)
This commit is contained in:
BIN
storage/sqlite/nodes/icons/sqlite.png
Normal file
BIN
storage/sqlite/nodes/icons/sqlite.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.1 KiB |
77
storage/sqlite/nodes/sqlite.html
Normal file
77
storage/sqlite/nodes/sqlite.html
Normal file
@@ -0,0 +1,77 @@
|
||||
<!--
|
||||
Copyright 2014 IBM Corp.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
|
||||
<script type="text/x-red" data-template-name="sqlitedb">
|
||||
<div class="form-row">
|
||||
<label for="node-config-input-db"><i class="icon-briefcase"></i> Database</label>
|
||||
<input type="text" id="node-config-input-db" placeholder="/tmp/sqlite">
|
||||
</div>
|
||||
</script>
|
||||
|
||||
<script type="text/javascript">
|
||||
RED.nodes.registerType('sqlitedb',{
|
||||
category: 'config',
|
||||
defaults: {
|
||||
db: {value:"",required:true}
|
||||
},
|
||||
label: function() {
|
||||
return this.db;
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
|
||||
<script type="text/x-red" data-template-name="sqlite">
|
||||
<div class="form-row">
|
||||
<label for="node-input-mydb"><i class="icon-briefcase"></i> Database</label>
|
||||
<input type="text" id="node-input-mydb">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
|
||||
<input type="text" id="node-input-name" placeholder="Name">
|
||||
</div>
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-help-name="sqlite">
|
||||
<p>Allows basic access to a Sqlite database.</p>
|
||||
<p>This node uses the <b>db.all</b> operation against the configured database. This does allow INSERTS, UPDATES and DELETES.
|
||||
By it's very nature it is SQL injection... so <i>be careful out there...</i></p>
|
||||
<p><b>msg.topic</b> must hold the <i>query</i> for the database, and the result is returned in <b>msg.payload</b>.</p>
|
||||
<p>Typically the returned payload will be an array of the result rows, (or an error).</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/javascript">
|
||||
RED.nodes.registerType('sqlite',{
|
||||
category: 'storage-input',
|
||||
color:"#e97b00",
|
||||
defaults: {
|
||||
mydb: {type:"sqlitedb",required:true},
|
||||
name: {value:""}
|
||||
},
|
||||
inputs:1,
|
||||
outputs:1,
|
||||
icon: "sqlite.png",
|
||||
label: function() {
|
||||
var dbNode = RED.nodes.node(this.mydb);
|
||||
return this.name||(dbNode?dbNode.label():"sqlite");
|
||||
},
|
||||
labelStyle: function() {
|
||||
return this.name?"node_label_italic":"";
|
||||
}
|
||||
});
|
||||
</script>
|
80
storage/sqlite/nodes/sqlite.js
Normal file
80
storage/sqlite/nodes/sqlite.js
Normal file
@@ -0,0 +1,80 @@
|
||||
/**
|
||||
* Copyright 2014 IBM Corp.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
module.exports = function(RED) {
|
||||
"use strict";
|
||||
var reconnect = RED.settings.sqliteReconnectTime || 20000;
|
||||
var sqlite3 = require('sqlite3');
|
||||
|
||||
function SqliteNodeDB(n) {
|
||||
RED.nodes.createNode(this,n);
|
||||
|
||||
this.dbname = n.db;
|
||||
var node = this;
|
||||
|
||||
node.doConnect = function() {
|
||||
node.db = new sqlite3.Database(node.dbname);
|
||||
node.db.on('open',function(err) {
|
||||
if (node.tick) { clearTimeout(node.tick); }
|
||||
node.log("opened "+node.dbname+" ok");
|
||||
});
|
||||
node.db.on('error', function(err) {
|
||||
node.warn(err);
|
||||
node.log("failed to open "+node.dbname);
|
||||
node.tick = setTimeout(doConnect, reconnect);
|
||||
});
|
||||
}
|
||||
|
||||
node.on('close', function () {
|
||||
if (node.tick) { clearTimeout(node.tick); }
|
||||
if (node.db) { node.db.close(); }
|
||||
});
|
||||
}
|
||||
RED.nodes.registerType("sqlitedb",SqliteNodeDB);
|
||||
|
||||
|
||||
function SqliteNodeIn(n) {
|
||||
RED.nodes.createNode(this,n);
|
||||
this.mydb = n.mydb;
|
||||
this.mydbConfig = RED.nodes.getNode(this.mydb);
|
||||
|
||||
if (this.mydbConfig) {
|
||||
this.mydbConfig.doConnect();
|
||||
var node = this;
|
||||
node.on("input", function(msg) {
|
||||
if (typeof msg.topic === 'string') {
|
||||
//console.log("query:",msg.topic);
|
||||
node.mydbConfig.db.all(msg.topic, function(err, row) {
|
||||
if (err) { node.warn(err); }
|
||||
else {
|
||||
msg.payload = row;
|
||||
node.send(msg);
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
if (typeof msg.topic !== 'string') {
|
||||
node.error("msg.topic : the query is not defined as a string");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
this.error("Sqlite database not configured");
|
||||
}
|
||||
}
|
||||
RED.nodes.registerType("sqlite",SqliteNodeIn);
|
||||
}
|
Reference in New Issue
Block a user