node-red-contrib-sqlitedb
Version:
Sends SQL queries to an SQLite cloud database provided as a service at <a href="https://www.sqlitedb.com/" target="_blank">https://www.sqlitedb.com/</a>
167 lines (162 loc) • 6.12 kB
HTML
<script type="text/javascript">
RED.nodes.registerType('sqlitedb api', {
category: 'storage',
color: '#a6bbcf',
defaults: {
name: { value: '' },
apiurl: { value: '', required: true },
apikey: { value: '', required: true },
sqlquery: { value: 'msg.topic', required: true },
sql: { value: '' },
},
inputs: 1,
outputs: 1,
icon: 'sqlite.png',
label: function () {
return this.name || 'SqliteDB'
},
oneditprepare: function () {
var ace = this
this.editor = RED.editor.createEditor({
id: 'node-input-sql-editor',
mode: 'ace/mode/sql',
value: $('#node-input-sql').val(),
globals: {
msg: true,
context: true,
RED: true,
util: true,
flow: true,
global: true,
console: true,
Buffer: true,
setTimeout: true,
clearTimeout: true,
setInterval: true,
clearInterval: true,
},
})
$('#node-input-sqlquery').change(function () {
if (
$('#node-input-sqlquery').val() == 'msg.topic' ||
$('#node-input-sqlquery').val() == 'batch'
) {
$('#node-input-sqllabel').hide()
$('#node-input-sql-editor').hide()
} else {
$('#node-input-sqllabel').show()
$('#node-input-sql-editor').show()
ace.editor.renderer.updateFull()
}
})
$('#node-input-sqlquery').change()
},
oneditsave: function () {
$('#node-input-sql').val(this.editor.getValue())
this.editor.destroy()
delete this.editor
},
oneditresize: function (size) {
var rows = $('#dialog-form>div:not(.node-text-editor-row)')
var height = $('#dialog-form').height()
for (var i = 0; i < rows.size(); i++) {
height -= $(rows[i]).outerHeight(true)
}
var editorRow = $('#dialog-form>div.node-text-editor-row')
height -=
parseInt(editorRow.css('marginTop')) +
parseInt(editorRow.css('marginBottom'))
$('.node-text-editor').css('height', height + 'px')
this.editor.resize()
},
})
</script>
<script type="text/x-red" data-template-name="sqlitedb api">
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> API URL</label>
<input type="text" id="node-input-apiurl" placeholder="API URL">
</div>
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> API Key</label>
<input type="text" id="node-input-apikey" placeholder="API Key">
</div>
<div class="form-row">
<label for=""><i class="fa fa-code"></i> SQL Query</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>
-->
</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>
</div>
<div>
<input type="hidden" id="node-input-sql" autofocus="autofocus">
</div>
<div class="form-row node-text-editor-row">
<div style="height: 250px; min-height:150px;" class="node-text-editor" id="node-input-sql-editor" ></div>
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name">
</div>
</script>
<script type="text/x-red" data-help-name="sqlitedb api">
<p>
Sends SQL queries to an SQLite cloud database provided
as a service at <a href="https://www.sqlitedb.com/" target="_blank">https://www.sqlitedb.com/</a>
</p>
<p>
To use this node first register for an account at <a href="https://www.sqlitedb.com/" target="_blank">https://www.sqlitedb.com/</a>
and create a database. Get the provided API URL and API KEY and add to this
node configurations to be able to access the cloud database.
</p>
<p>
An online admin panel is provided with each cloud database.
It allows to fully manage the database via a web browser and allows
the following actions to be completed conveniently via a graphical
user interface:
<br />
<b>TABLES</b><br />
- alter tables<br />
- create tables<br />
- drop tables<br />
- export tables<br />
- truncate tables<br />
<br />
<b>RECORDS</b><br />
- delete records<br />
- insert records<br />
- select/read records<br />
- update records<br />
- view/search records<br />
</p>
<p>
The node provides two methods for providing the SQL code for the query:
<br />
- <i>fixed</i> - convenient for permanent SQL statements. The SQL is entered directly in the node configurator.
<br />
- <i>msg.topic</i> - for dynamic statements. The SQL is composed via a functions node and appended to the message's topic attribute before sent onwards.
</p>
<p>
Using any SQL query, the result is returned in <code>msg.payload</code>.
After submitting the query a response message in the payload will be
returned in the following format:
<br />
- Success response<br />
<code>{"status":"success","message":"Message text","data":[]}</code>
- Success response<br />
<code>{"status":"error","message":"Message text"}</code>
<br />
Checking the "status" attribute allows to find out if the SQL query
was executed successfully.
<br />
Checking the "message" attribute allows to find out more information about the query's result.
<br />
The "data" attribute contains the records returned by SELECT type queries.
</p>
</script>