added flow rtdb saving nodes

This commit is contained in:
Peter Svensson
2019-06-23 20:11:05 +02:00
parent 632e06c586
commit 0af1a1b15a
5 changed files with 221 additions and 3 deletions

62
flow-to-rtdb.html Normal file
View File

@ -0,0 +1,62 @@
<script type="text/javascript">
RED.nodes.registerType('flow-to-rtdb',{
category: 'firebase-admin',
color: '#a6bbcf',
defaults: {
name: {value:""},
flowId: {value:""},
path: {value:""},
cred: {value: "", type: 'firebase-config'}
},
inputs:1,
outputs:1,
icon: "firebase-admin-icon.png",
label: function() {
return this.name||"flow-to-rtdb";
},
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="flow-to-rtdb">
<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-flowId"><i class="icon-tag"></i> Flow id</label>
<input type="text" id="node-input-flowId" placeholder="FlowId">
</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="Credentials">
</div>
</script>
<script type="text/x-red" data-help-name="flow-to-rtdb">
<p>A node that wraps the flow-to-rtdb SDK.</p>
</script>

41
flow-to-rtdb.js Normal file
View File

@ -0,0 +1,41 @@
const runtime = require('node-red').runtime
module.exports = function(RED) {
function FirebaseAdmin(config) {
RED.nodes.createNode(this, config);
var node = this;
if(config.cred){
let c = RED.nodes.getNode(config.cred)
this.admin = c.admin
}
node.on('input', function(msg) {
if(msg && msg.payload){
console.log('flow-to-rtdb got input')
console.dir(msg)
this.path = msg.payload.path || config.path
this.flowId = msg.payload.flowId || config.flowId
console.log('flow-to-rtdb path='+this.path+', flowId='+this.flowId)
runtime.flows.getFlow({id: this.flowId}).then((flow)=>{
this.admin.database().ref(this.path).set(flow).then((res)=>{
console.log('firebase set result '+res)
console.dir(res)
node.send({payload: res})
},(err)=>{
console.log('rtdb saving error')
console.dir(err)
}).catch((ex)=>{
console.log('rtdb saving exception')
console.dir(ex)
})
})
}
}.bind(this));
}
RED.nodes.registerType("flow-to-rtdb", FirebaseAdmin);
}

View File

@ -1,6 +1,6 @@
{
"name": "node-red-contrib-firebase-admin",
"version": "1.0.3",
"version": "1.0.4",
"description": "A node-red module that wraps the server-side admin SDK of firebase, firestore, et.c.",
"main": "index.js",
"scripts": {
@ -12,7 +12,9 @@
},
"author": "",
"license": "MIT",
"keywords": [ "node-red" ],
"keywords": [
"node-red"
],
"bugs": {
"url": "https://github.com/psvensson/node-red-contrib-firebase-admin/issues"
},
@ -27,10 +29,13 @@
"firestore-add": "firestore-add.js",
"firestore-get": "firestore-get.js",
"firestore-query": "firestore-query.js",
"firebase-config": "firebase-config.js"
"firebase-config": "firebase-config.js",
"flow-to-rtdb": "flow-to-rtdb.js",
"rtdb-to-flow": "rtdb-to-flow.js"
}
},
"dependencies": {
"@node-red/runtime": "^0.20.6",
"firebase-admin": "^8.0.0"
}
}

57
rtdb-to-flow.html Normal file
View File

@ -0,0 +1,57 @@
<script type="text/javascript">
RED.nodes.registerType('rtdb-to-flow',{
category: 'firebase-admin',
color: '#a6bbcf',
defaults: {
name: {value:""},
flowId: {value:""},
path: {value:""},
cred: {value: "", type: 'firebase-config'}
},
inputs:1,
outputs:1,
icon: "firebase-admin-icon.png",
label: function() {
return this.name||"rtdb-to-flow";
},
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-to-flow">
<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="Credentials">
</div>
</script>
<script type="text/x-red" data-help-name="rtdb-to-flow">
<p>A node that wraps the rtdb-to-flow SDK.</p>
</script>

53
rtdb-to-flow.js Normal file
View File

@ -0,0 +1,53 @@
const runtime = require('node-red').runtime
let oldpath
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()
runtime.flows.updateFlow({id: val.id, flow: val})
//console.log('val='+val)
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)
}
this.admin.database().ref(path).on('value', cb)
oldpath = path
}
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-get to listen for messages')
node.on('input', function(msg) {
let path = this.path
if(msg && msg.payload){
path = msg.payload.path
setUpListener(path)
}
}.bind(this));
}
RED.nodes.registerType("rtdb-to-flow", FirebaseAdmin);
}