From fefe2f250918bb277907b680b3a04875fa59fb59 Mon Sep 17 00:00:00 2001 From: Peter Svensson Date: Mon, 15 Jul 2019 10:27:01 +0200 Subject: [PATCH] added on nodes for rtdb and firestore andmade nodes pass through msg objects with as little changes as possible --- README.md | 25 +++++++++++-- firestore/firestore-add.html | 6 ++++ firestore/firestore-get.html | 5 +++ firestore/firestore-get.js | 12 +++++-- firestore/firestore-on.html | 61 +++++++++++++++++++++++++++++++ firestore/firestore-on.js | 51 ++++++++++++++++++++++++++ firestore/firestore-query.html | 21 +++++++++++ firestore/firestore-query.js | 6 ++-- firestore/firestore-set.html | 3 ++ package.json | 9 +++-- rtdb/rtdb-get.html | 7 +++- rtdb/rtdb-get.js | 32 +++++++++++------ rtdb/rtdb-on.html | 61 +++++++++++++++++++++++++++++++ rtdb/rtdb-on.js | 65 ++++++++++++++++++++++++++++++++++ rtdb/rtdb-push.html | 3 ++ rtdb/rtdb-query.html | 17 +++++++++ rtdb/rtdb-query.js | 6 ++-- rtdb/rtdb-set.html | 3 ++ storage/storage-list.html | 21 +++++++++++ storage/storage-list.js | 5 ++- storage/storage-read.html | 15 ++++++++ storage/storage-read.js | 25 +++++++------ storage/storage-write.html | 22 ++++++++++++ storage/storage-write.js | 20 +++++------ 24 files changed, 453 insertions(+), 48 deletions(-) create mode 100644 firestore/firestore-on.html create mode 100644 firestore/firestore-on.js create mode 100644 rtdb/rtdb-on.html create mode 100644 rtdb/rtdb-on.js diff --git a/README.md b/README.md index 2875c10..9a94563 100644 --- a/README.md +++ b/README.md @@ -29,14 +29,26 @@ Get data from a path in the rtdb database input: {"payload": {"path": "foo/bar"}} -output: +output: whatever data was at the path "foo/bar" in the rtdb database + +## rtdb-on +Set up a snapshot listsner for a path in the rtdb database + +input: {"payload": {"path": "foo/bar"}} + +output: whatever data was at the path "foo/bar" in the rtdb database, when changed + ## rtdb-set 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. +input: {"payload": {"path": "foo/bar", "obj": {"the": "object"}} + ## rtdb-push Pushes the new object onto an array under the path +input: {"payload": {"path": "foo/bar", "obj": {"the": "object"}} + ## rtdb-query Set up a reactive query for a path in the rtdb database. @@ -63,7 +75,14 @@ Get data from a document path in the firestore database input: {"payload": {"path": "foo/bar"}} -output: +output: the document at the path "foo/bar" in the firestore database + +## firestore-on +Set up a snapshot listener fo a document path in the firestore database + +input: {"payload": {"path": "foo/bar"}} + +output: the document at the path "foo/bar" in the firestore database, when changed ## firestore-set Set data at a path in the firestore database. Uses "onSnapshot" so will fire every time the data at the path changes and so drive flow execution from that point. @@ -115,7 +134,7 @@ input: } } -npm publish .output: Buffer object containing the binary file contents. Can easily be converted to a string by calling toString() on the Buffer. +output: Buffer object containing the binary file contents. Can easily be converted to a string by calling toString() on the Buffer. ## storage-write Writes the content of JavaScript Buffer object to a file path in a storage bucket. diff --git a/firestore/firestore-add.html b/firestore/firestore-add.html index ef24a1e..8679812 100644 --- a/firestore/firestore-add.html +++ b/firestore/firestore-add.html @@ -47,4 +47,10 @@ \ No newline at end of file diff --git a/firestore/firestore-get.html b/firestore/firestore-get.html index 37f4248..a647a34 100644 --- a/firestore/firestore-get.html +++ b/firestore/firestore-get.html @@ -53,4 +53,9 @@ \ No newline at end of file diff --git a/firestore/firestore-get.js b/firestore/firestore-get.js index a071acd..d57e3df 100644 --- a/firestore/firestore-get.js +++ b/firestore/firestore-get.js @@ -1,5 +1,5 @@ - +let msgin let unsub module.exports = function(RED) { @@ -16,7 +16,7 @@ module.exports = function(RED) { if(unsub){ unsub() } - this.admin.firestore().doc(path).onSnapshot(cb) + this.admin.firestore().doc(path).get().then(cb) } const cb = (res)=>{ @@ -24,11 +24,17 @@ module.exports = function(RED) { console.dir(res) let val = res.data() console.log('val='+val) - node.send({payload:val}) + if(msgin){ + msgin.payload = val + node.send(msgin) + } else { + node.send({payload: val}) + } } node.on('input', function(msg) { if(msg && msg.payload){ + msgin = msg const path = msg.payload.path setup(path) diff --git a/firestore/firestore-on.html b/firestore/firestore-on.html new file mode 100644 index 0000000..3d59eb8 --- /dev/null +++ b/firestore/firestore-on.html @@ -0,0 +1,61 @@ + + + + + \ No newline at end of file diff --git a/firestore/firestore-on.js b/firestore/firestore-on.js new file mode 100644 index 0000000..7b633d4 --- /dev/null +++ b/firestore/firestore-on.js @@ -0,0 +1,51 @@ + +let msgin +let unsub + +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 + } + + const setup = ()=>{ + if(unsub){ + unsub() + } + this.admin.firestore().doc(path).onSnapshot(cb) + } + + const cb = (res)=>{ + console.log('firestore get result '+res) + console.dir(res) + let val = res.data() + console.log('val='+val) + if(msgin){ + msgin.payload = val + node.send(msgin) + } else { + node.send({payload: val}) + } + } + + node.on('input', function(msg) { + if(msg && msg.payload){ + msgin = msg + const path = msg.payload.path + setup(path) + + } + }.bind(this)); + + if(config.path){ + setup(config.path) + } + + + } + RED.nodes.registerType("firestore-on", FirebaseAdmin); +} \ No newline at end of file diff --git a/firestore/firestore-query.html b/firestore/firestore-query.html index c0673f7..8f65268 100644 --- a/firestore/firestore-query.html +++ b/firestore/firestore-query.html @@ -47,4 +47,25 @@ \ No newline at end of file diff --git a/firestore/firestore-query.js b/firestore/firestore-query.js index d771fc0..d92b96a 100644 --- a/firestore/firestore-query.js +++ b/firestore/firestore-query.js @@ -1,4 +1,4 @@ - +let msgin let unsub module.exports = function(RED) { @@ -16,13 +16,15 @@ module.exports = function(RED) { console.dir(res) let val = res.docs.map((d)=>{return d.data()}) console.log('val='+val) - node.send({payload:val}) + msgin.payload = val + node.send(msgin) } node.on('input', function(msg) { console.log('firestore-query got input') console.dir(msg.payload) if(msg && msg.payload){ + msgin = msg const path = msg.payload.path if(unsub){ unsub() diff --git a/firestore/firestore-set.html b/firestore/firestore-set.html index ac01a87..4267119 100644 --- a/firestore/firestore-set.html +++ b/firestore/firestore-set.html @@ -47,4 +47,7 @@ \ No newline at end of file diff --git a/package.json b/package.json index ceb00db..ac63af2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-red-contrib-firebase-admin", - "version": "1.1.3", + "version": "1.1.4", "description": "A node-red module that wraps the server-side admin SDK of firebase, firestore, et.c.", "main": "index.js", "scripts": { @@ -13,7 +13,10 @@ "author": "", "license": "MIT", "keywords": [ - "node-red" + "node-red", + "firebase", + "firestore", + "cloud storage" ], "bugs": { "url": "https://github.com/psvensson/node-red-contrib-firebase-admin/issues" @@ -24,10 +27,12 @@ "rtdb-set": "rtdb/rtdb-set.js", "rtdb-push": "rtdb/rtdb-push.js", "rtdb-get": "rtdb/rtdb-get.js", + "rtdb-on": "rtdb/rtdb-on.js", "rtdb-query": "rtdb/rtdb-query.js", "firestore-set": "firestore/firestore-set.js", "firestore-add": "firestore/firestore-add.js", "firestore-get": "firestore/firestore-get.js", + "firestore-on": "firestore/firestore-on.js", "firestore-query": "firestore/firestore-query.js", "firebase-config": "firebase-config.js", "flow-to-rtdb": "rtdb/flow-to-rtdb.js", diff --git a/rtdb/rtdb-get.html b/rtdb/rtdb-get.html index b60be49..0420777 100644 --- a/rtdb/rtdb-get.html +++ b/rtdb/rtdb-get.html @@ -52,5 +52,10 @@ \ No newline at end of file diff --git a/rtdb/rtdb-get.js b/rtdb/rtdb-get.js index 6332fdd..2289dae 100644 --- a/rtdb/rtdb-get.js +++ b/rtdb/rtdb-get.js @@ -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); diff --git a/rtdb/rtdb-on.html b/rtdb/rtdb-on.html new file mode 100644 index 0000000..8d5952f --- /dev/null +++ b/rtdb/rtdb-on.html @@ -0,0 +1,61 @@ + + + + + \ No newline at end of file diff --git a/rtdb/rtdb-on.js b/rtdb/rtdb-on.js new file mode 100644 index 0000000..c9d51c3 --- /dev/null +++ b/rtdb/rtdb-on.js @@ -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); +} \ No newline at end of file diff --git a/rtdb/rtdb-push.html b/rtdb/rtdb-push.html index 666d6f1..c18666d 100644 --- a/rtdb/rtdb-push.html +++ b/rtdb/rtdb-push.html @@ -47,4 +47,7 @@ \ No newline at end of file diff --git a/rtdb/rtdb-query.html b/rtdb/rtdb-query.html index 4fae0b5..336dfe9 100644 --- a/rtdb/rtdb-query.html +++ b/rtdb/rtdb-query.html @@ -47,4 +47,21 @@ \ No newline at end of file diff --git a/rtdb/rtdb-query.js b/rtdb/rtdb-query.js index 647f211..7e341b2 100644 --- a/rtdb/rtdb-query.js +++ b/rtdb/rtdb-query.js @@ -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){ diff --git a/rtdb/rtdb-set.html b/rtdb/rtdb-set.html index e32518b..451a596 100644 --- a/rtdb/rtdb-set.html +++ b/rtdb/rtdb-set.html @@ -47,4 +47,7 @@ \ No newline at end of file diff --git a/storage/storage-list.html b/storage/storage-list.html index 4cb7fc0..7ec1117 100644 --- a/storage/storage-list.html +++ b/storage/storage-list.html @@ -46,4 +46,25 @@ \ No newline at end of file diff --git a/storage/storage-list.js b/storage/storage-list.js index 3b0e139..10228c0 100644 --- a/storage/storage-list.js +++ b/storage/storage-list.js @@ -1,7 +1,5 @@ -let oldpath - module.exports = function(RED) { function FirebaseAdmin(config) { @@ -37,7 +35,8 @@ module.exports = function(RED) { console.log('got file listing') //console.dir(files[0]) let f = files[0].filter((e)=>{ return e.name[e.name.length-1] !== '/' }) - node.send({payload: {files: f}}) + msg.payload = {files: f} + node.send(msg) }) } }.bind(this)); diff --git a/storage/storage-read.html b/storage/storage-read.html index 0ec3ac1..5fbb57e 100644 --- a/storage/storage-read.html +++ b/storage/storage-read.html @@ -40,4 +40,19 @@ \ No newline at end of file diff --git a/storage/storage-read.js b/storage/storage-read.js index 1493dbb..3457111 100644 --- a/storage/storage-read.js +++ b/storage/storage-read.js @@ -1,7 +1,5 @@ -let oldpath - module.exports = function(RED) { function FirebaseAdmin(config) { @@ -20,8 +18,8 @@ module.exports = function(RED) { //console.log('configuring storage-read to listen for messages') node.on('input', function(msg) { if(msg && msg.payload){ - let path = msg.payload.path || this.path - let bucket = msg.payload.bucket || this.bucket + let path = msg.payload.path || msg.path || this.path + let bucket = msg.payload.bucket || msg.bucket || this.bucket console.log('------------------------------ storage-read reading from bucket "'+bucket+'" path "'+path+'"') if(msg.payload.files && msg.payload.files.length > 0){ console.log('--reading from files') @@ -36,12 +34,13 @@ module.exports = function(RED) { console.log('storage-read got file '+file.name) rv[file.name] = content if(--count === 0){ - console.log('all files done, sending..') - for(let x in rv){ - console.log(x) - } - node.send({payload:rv}) + msg.payload = rv + node.send(msg) } + }, (fail)=>{ + console.log('storage-read could not find file '+path) + msg.payload = undefined + node.send(msg) }) })(_file) }) @@ -52,8 +51,12 @@ module.exports = function(RED) { .file(path).download().then((file)=>{ console.log('storage-read got file') //console.dir(file) - node.send({payload:file}) - + msg.payload = file + node.send(msg) + }, (fail)=>{ + console.log('storage-read could not find single file '+path) + msg.payload = undefined + node.send(msg) }) } } diff --git a/storage/storage-write.html b/storage/storage-write.html index 01a22f7..a60340e 100644 --- a/storage/storage-write.html +++ b/storage/storage-write.html @@ -40,4 +40,26 @@ \ No newline at end of file diff --git a/storage/storage-write.js b/storage/storage-write.js index ee2e8d3..5ac4fc3 100644 --- a/storage/storage-write.js +++ b/storage/storage-write.js @@ -17,27 +17,27 @@ module.exports = function(RED) { //console.log('configuring storage-write to listen for messages') node.on('input', function(msg) { if(msg && msg.payload){ - let path = msg.payload.path || this.path - let bucket = msg.payload.bucket || this.bucket - let contents = msg.payload.contents + let path = msg.payload.path || msg.path|| this.path + let bucket = msg.payload.bucket || msg.bucket || this.bucket + let contents = msg.payload.contents || msg.payload let options ={ - contentType: msg.payload.contentType || 'auto', - metadata: msg.payload.metadata || {}, - private: msg.payload.private || true, - public: msg.payload.public || true, + contentType: msg.payload.contentType || msg.contentType || 'auto', + metadata: msg.payload.metadata || msg.metadata || {}, + private: msg.payload.private || msg.private || true, + public: msg.payload.public || msg.public || true, } console.log('storage-write writing file to bucket "'+bucket+'" path "'+path+'" contents is of type '+(typeof contents)) - console.dir(contents) const myBucket = this.storage.bucket(bucket); const file = myBucket.file(path); file.save(contents, options, function(err) { if (!err) { // File written successfully. - node.send({payload:{success:true, filename: path}}) + msg.payload={success:true, filename: path} } else { console.log('cloud storage write error: '+err) - node.send({payload:{success:false, filename: path}}) + msg.payload ={success:false, filename: path} } + node.send(msg) }); } }.bind(this));