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",
"version": "0.3.0",
"version": "0.3.2",
"description": "A sqlite node for Node-RED",
"dependencies": {
"sqlite3": "^4.0.2"

View File

@ -4,13 +4,24 @@
<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">
</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 type="text/javascript">
RED.nodes.registerType('sqlitedb',{
category: 'config',
defaults: {
db: {value:"",required:true}
db: {value:"", required:true},
mode: {value:"RWC"}
},
label: function() {
return this.db;
@ -111,7 +122,7 @@
clearInterval: true
}
});
$("#node-input-sqlquery").change(function() {
if ($("#node-input-sqlquery").val() == "msg.topic" || $("#node-input-sqlquery").val() == "batch"){
$("#node-input-sqllabel").hide();

View File

@ -7,10 +7,14 @@ module.exports = function(RED) {
RED.nodes.createNode(this,n);
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;
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() {
if (node.tick) { clearTimeout(node.tick); }
node.log("opened "+node.dbname+" ok");
@ -41,6 +45,7 @@ module.exports = function(RED) {
if (this.mydbConfig) {
this.mydbConfig.doConnect();
node.status({fill:"green",shape:"dot",text:this.mydbConfig.mod});
var bind = [];
node.on("input", function(msg) {
if (this.sqlquery == "msg.topic"){