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

Let sqlite open RO as well as RW

Also better reuse of connection object
This commit is contained in:
Dave Conway-Jones 2018-08-11 14:03:16 +01:00
parent f3b0e2277d
commit 211988017f
No known key found for this signature in database
GPG Key ID: 9E7F9C73F5168CD4
3 changed files with 20 additions and 4 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "node-red-node-sqlite", "name": "node-red-node-sqlite",
"version": "0.3.0", "version": "0.3.2",
"description": "A sqlite node for Node-RED", "description": "A sqlite node for Node-RED",
"dependencies": { "dependencies": {
"sqlite3": "^4.0.2" "sqlite3": "^4.0.2"

View File

@ -4,13 +4,24 @@
<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> Database</label>
<input type="text" id="node-config-input-db" placeholder="/tmp/sqlite"> <input type="text" id="node-config-input-db" placeholder="/tmp/sqlite">
</div> </div>
<div class="form-row">
<label for="node-config-input-mode">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>
</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>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
RED.nodes.registerType('sqlitedb',{ RED.nodes.registerType('sqlitedb',{
category: 'config', category: 'config',
defaults: { defaults: {
db: {value:"",required:true} db: {value:"", required:true},
mode: {value:"RWC"}
}, },
label: function() { label: function() {
return this.db; return this.db;

View File

@ -7,10 +7,14 @@ module.exports = function(RED) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.dbname = n.db; this.dbname = n.db;
this.mod = n.mode;
if (n.mode === "RWC") { this.mode = sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE; }
if (n.mode === "RW") { this.mode = sqlite3.OPEN_READWRITE; }
if (n.mode === "RO") { this.mode = sqlite3.OPEN_READONLY; }
var node = this; var node = this;
node.doConnect = function() { node.doConnect = function() {
node.db = new sqlite3.Database(node.dbname); node.db = node.db || new sqlite3.Database(node.dbname,node.mode);
node.db.on('open', function() { node.db.on('open', function() {
if (node.tick) { clearTimeout(node.tick); } if (node.tick) { clearTimeout(node.tick); }
node.log("opened "+node.dbname+" ok"); node.log("opened "+node.dbname+" ok");
@ -41,6 +45,7 @@ module.exports = function(RED) {
if (this.mydbConfig) { if (this.mydbConfig) {
this.mydbConfig.doConnect(); this.mydbConfig.doConnect();
node.status({fill:"green",shape:"dot",text:this.mydbConfig.mod});
var bind = []; var bind = [];
node.on("input", function(msg) { node.on("input", function(msg) {
if (this.sqlquery == "msg.topic"){ if (this.sqlquery == "msg.topic"){