diff --git a/flow-to-rtdb.html b/flow-to-rtdb.html new file mode 100644 index 0000000..3e33553 --- /dev/null +++ b/flow-to-rtdb.html @@ -0,0 +1,62 @@ + + + + + \ No newline at end of file diff --git a/flow-to-rtdb.js b/flow-to-rtdb.js new file mode 100644 index 0000000..cbe3158 --- /dev/null +++ b/flow-to-rtdb.js @@ -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); +} \ No newline at end of file diff --git a/package.json b/package.json index 6546b62..cc58054 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/rtdb-to-flow.html b/rtdb-to-flow.html new file mode 100644 index 0000000..cb00f88 --- /dev/null +++ b/rtdb-to-flow.html @@ -0,0 +1,57 @@ + + + + + \ No newline at end of file diff --git a/rtdb-to-flow.js b/rtdb-to-flow.js new file mode 100644 index 0000000..06df387 --- /dev/null +++ b/rtdb-to-flow.js @@ -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); +} \ No newline at end of file