added on nodes for rtdb and firestore andmade nodes pass through msg objects with as little changes as possible

This commit is contained in:
Peter Svensson
2019-07-15 10:27:01 +02:00
parent e9fd502879
commit fefe2f2509
24 changed files with 453 additions and 48 deletions

View File

@ -52,5 +52,10 @@
</script>
<script type="text/x-red" data-help-name="rtdb-get">
<p>A node that wraps the rtdb-get SDK</p>
<p>Get data from a path in the rtdb database </p>
<p>
input: {"payload": {"path": "foo/bar"}}
</p><p>
output: whatever data was at the path "foo/bar" in the rtdb database</p>
</p>
</script>

View File

@ -1,6 +1,5 @@
let oldpath
let msgin
module.exports = function(RED) {
@ -9,20 +8,31 @@ module.exports = function(RED) {
var node = this;
const cb = (res)=>{
console.log('firebase get result '+res)
console.log('firebase rtdb-get result '+res)
//console.dir(res)
let val = res.val()
console.dir(val)
node.send({payload:val})
//console.dir(val)
if(msgin){
msgin.payload = val
node.send(msgin)
} else {
node.send({payload: val})
}
}
let setUpListener = (path)=>{
console.log('rtdb-get setUpListener for path '+path)
if(oldpath){
this.admin.database().ref(oldpath).off('value', cb)
console.log('* rtdb-get setUpListener for path '+path)
if(path){
this.admin.database().ref(path).once('value').then((res)=>{
cb(res)
}, (fail)=>{
console.log('rtdb-get failure ')
console.dir(fail)
})
} else {
console.log('----- rtdb-get got empty path !!')
console.dir(config)
}
this.admin.database().ref(path).on('value', cb)
oldpath = path
}
if(config.cred){
@ -43,11 +53,11 @@ module.exports = function(RED) {
let path = this.path
if(msg && msg.payload){
path = msg.payload.path
msgin = msg
setUpListener(path)
}
}.bind(this));
setUpListener(this.path)
}
RED.nodes.registerType("rtdb-get", FirebaseAdmin);

61
rtdb/rtdb-on.html Normal file
View File

@ -0,0 +1,61 @@
<script type="text/javascript">
RED.nodes.registerType('rtdb-on',{
category: 'firebase-admin',
color: '#a6bbcf',
defaults: {
name: {value:""},
path: {value:""},
cred: {value: "", type: 'firebase-config'}
},
inputs:1,
outputs:1,
icon: "firebase-admin-icon.png",
label: function() {
return this.name||"rtdb-on";
},
oneditsave: function() {
let type = $('#apitype-select').val()
console.log('type is set to '+type)
},
oneditprepare: function() {
$('#apitype-select').change(function () {
$("#node-input-apitype").val($(this).find('option:selected').val())
});
$("#apitype-select").val($("#node-input-apitype").val())
$('#apitype-select').trigger('change');
}
});
</script>
<script type="text/x-red" data-template-name="rtdb-on">
<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>
<div class="form-row">
<label for="node-input-path"><i class="icon-tag"></i> Path</label>
<input type="text" id="node-input-path" placeholder="Path">
</div>
<div class="form-row">
<label for="node-input-cred"><i class="icon-tag"></i> Credentials</label>
<input type="text" id="node-input-cred" placeholder="Name">
</div>
</script>
<script type="text/x-red" data-help-name="rtdb-on">
<p>Get data from a path in the rtdb database </p>
<p>
input: {"payload": {"path": "foo/bar"}}
</p><p>
output: whatever data was at the path "foo/bar" in the rtdb database</p>
</p>
</script>

65
rtdb/rtdb-on.js Normal file
View File

@ -0,0 +1,65 @@
let oldpath
let msgin
module.exports = function(RED) {
function FirebaseAdmin(config) {
RED.nodes.createNode(this, config);
var node = this;
const cb = (res)=>{
console.log('firebase get result '+res)
//console.dir(res)
let val = res.val()
//console.dir(val)
if(msgin){
msgin.payload = val
node.send(msgin)
} else {
node.send({payload: val})
}
}
let setUpListener = (path)=>{
console.log('rtdb-on setUpListener for path '+path)
if(oldpath){
this.admin.database().ref(oldpath).off('value', cb)
}
if(path){
this.admin.database().ref(path).on('value', cb)
oldpath = path
} else {
console.log('----- rtdb-on got empty path !!')
console.dir(config)
}
}
if(config.cred){
let c = RED.nodes.getNode(config.cred)
this.admin = c.admin
}
//console.log('------------------------------- rtdg-get config')
//console.dir(config)
this.path = config.path
if(this.path){
setUpListener(this.path)
}
//console.log('configuring rtdb-on to listen for messages')
node.on('input', function(msg) {
let path = this.path
if(msg && msg.payload){
path = msg.payload.path
msgin = msg
setUpListener(path)
}
}.bind(this));
}
RED.nodes.registerType("rtdb-on", FirebaseAdmin);
}

View File

@ -47,4 +47,7 @@
<script type="text/x-red" data-help-name="rtdb-push">
<p>A node that wraps the rtdb-push SDK.</p>
Pushes the new object onto an array under the path
<p>
input: {"payload": {"path": "foo/bar", "obj": {"the": "object"}}
</script>

View File

@ -47,4 +47,21 @@
<script type="text/x-red" data-help-name="rtdb-query">
<p>A node that wraps the rtdb-query SDK</p>
Set up a reactive query for a path in the rtdb database.
<p>
input: {"payload": {"path": "foo/bar", queries:[], on: "value}}
<p>
on: "value" (can also be "child_added", "child_removed", "child_changed", "child_moved").
If an "on" property is missing, on: "value" is assumed as default
<p>
Where each query is an object that can look like either of the following examples;
<p>
- {"startAt": "foo"}
- {"endAt": "bar"}
- {"equalTo": "quux"}
- {"orderBy": "child", "value": "height"} (can also be "key" or "value)
- {"limitTo": "last", "value": 3} (can also be "first")
<p>
output: [an array of results for the query]
</script>

View File

@ -1,5 +1,5 @@
let msgin
let oldpath
let oldeventtype
@ -18,11 +18,13 @@ module.exports = function(RED) {
console.dir(res)
let val = res.val()
console.log('val='+val)
node.send({payload:val})
msgin.payload = val
node.send(msgin)
}
node.on('input', function(msg) {
if(msg && msg.payload){
msgin = msg
const path = msg.payload.path
const eventtype = msg.payload.on || 'value'
if(oldpath){

View File

@ -47,4 +47,7 @@
<script type="text/x-red" data-help-name="rtdb-set">
<p>A node that wraps the rtdb-set SDK</p>
Set data at a path in the rtdb database. Use "on" snapshot so will fire every time the data at the path changes and so drive flow execution from that point.
<p>
input: {"payload": {"path": "foo/bar", "obj": {"the": "object"}}
</script>