From 053e368a4cc82cceb708cec643341f7bc1355e5d Mon Sep 17 00:00:00 2001 From: Kunal Kamble Date: Mon, 13 Apr 2020 15:37:14 -0400 Subject: [PATCH 1/8] Add missing parameter to setup method in firestore-get node --- firestore/firestore-get.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/firestore/firestore-get.js b/firestore/firestore-get.js index d57e3df..7152017 100644 --- a/firestore/firestore-get.js +++ b/firestore/firestore-get.js @@ -12,7 +12,7 @@ module.exports = function(RED) { this.admin = c.admin } - const setup = ()=>{ + const setup = (path)=>{ if(unsub){ unsub() } @@ -48,4 +48,4 @@ module.exports = function(RED) { } RED.nodes.registerType("firestore-get", FirebaseAdmin); -} \ No newline at end of file +} From 2487685aea0a97271b888894e0b4aa48a0824807 Mon Sep 17 00:00:00 2001 From: Kunal Kamble Date: Mon, 13 Apr 2020 15:38:33 -0400 Subject: [PATCH 2/8] Update package version for get latest changes made in firebase-get node --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index fc424ce..77c14d9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-red-contrib-firebase-admin", - "version": "1.1.16", + "version": "1.1.17", "description": "A node-red module that wraps the server-side admin SDK of firebase, firestore, et.c.", "main": "index.js", "scripts": { From 186b402d0715794e10d196811a04abea4fba5e57 Mon Sep 17 00:00:00 2001 From: Peter Svensson Date: Wed, 22 Apr 2020 20:03:17 +0200 Subject: [PATCH 3/8] fixed bug in storage module --- package.json | 2 +- storage/storage-list.js | 3 ++- storage/storage-read.js | 4 ++-- storage/storage-write.js | 7 ++++--- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 77c14d9..85be533 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-red-contrib-firebase-admin", - "version": "1.1.17", + "version": "1.1.18", "description": "A node-red module that wraps the server-side admin SDK of firebase, firestore, et.c.", "main": "index.js", "scripts": { diff --git a/storage/storage-list.js b/storage/storage-list.js index 10228c0..b0c8bf4 100644 --- a/storage/storage-list.js +++ b/storage/storage-list.js @@ -10,7 +10,8 @@ module.exports = function(RED) { if(config.cred){ let c = RED.nodes.getNode(config.cred) this.admin = c.admin - this.storage = c.storage + let global = this.context().global + this.storage = c.storage || global.get('cloud-storage') this.bucket = config.bucket || c.bucket this.path = config.path } diff --git a/storage/storage-read.js b/storage/storage-read.js index db91a13..458b313 100644 --- a/storage/storage-read.js +++ b/storage/storage-read.js @@ -10,8 +10,8 @@ module.exports = function(RED) { if(config.cred){ let c = RED.nodes.getNode(config.cred) this.admin = c.admin - this.storage = c.storage - storage = this.storage + let global = this.context().global + this.storage = c.storage || global.get('cloud-storage') this.bucket = config.bucket || c.bucket this.path = config.path diff --git a/storage/storage-write.js b/storage/storage-write.js index e43fc4b..595ac8f 100644 --- a/storage/storage-write.js +++ b/storage/storage-write.js @@ -9,7 +9,8 @@ module.exports = function(RED) { if(config.cred){ let c = RED.nodes.getNode(config.cred) this.admin = c.admin - this.storage = c.storage + let global = this.context().global + this.storage = c.storage || global.get('cloud-storage') this.bucket = config.bucket || c.bucket this.path = config.path /* @@ -21,7 +22,7 @@ module.exports = function(RED) { } - node.on('input', function(msg) { + node.on('input', function(msg) { if(msg && msg.payload){ let path = msg.payload.path || msg.path|| this.path let bucket = msg.payload.bucket || msg.bucket || this.bucket @@ -40,7 +41,7 @@ module.exports = function(RED) { // File written successfully. msg.payload={success:true, filename: path} } else { - console.log('cloud storage write error: '+err) + console.log('cloud storage write error: '+JSON.stringify(err)) msg.payload ={success:false, filename: path} } node.send(msg) From c7941c2647eff836271b89a47eb7f7d34ca11165 Mon Sep 17 00:00:00 2001 From: Peter Svensson Date: Thu, 23 Apr 2020 12:58:55 +0200 Subject: [PATCH 4/8] fixed sloppy coding whenrefering to storage instead of this.storage in storage nodes --- package.json | 2 +- storage/storage-read.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 85be533..fcf9d3c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-red-contrib-firebase-admin", - "version": "1.1.18", + "version": "1.1.19", "description": "A node-red module that wraps the server-side admin SDK of firebase, firestore, et.c.", "main": "index.js", "scripts": { diff --git a/storage/storage-read.js b/storage/storage-read.js index 458b313..d86393f 100644 --- a/storage/storage-read.js +++ b/storage/storage-read.js @@ -27,7 +27,7 @@ module.exports = function(RED) { if(msg && msg.payload){ 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+'" this.storage = '+this.storage+' storage = '+storage) + console.log('------------------------------ storage-read reading from bucket "'+bucket+'" path "'+path+'" this.storage = '+this.storage) if(msg.payload.files && msg.payload.files.length > 0){ console.log('--reading from files') let count = msg.payload.files.length @@ -54,7 +54,7 @@ module.exports = function(RED) { } else if(msg){ console.log('* reading single file from path '+path) try{ - let s = this.storage || storage + let s = this.storage s.bucket(bucket) .file(path).download().then((file)=>{ console.log('storage-read got file') From 7ac88211f7b620db05f054bee5c7d3635b6cb496 Mon Sep 17 00:00:00 2001 From: Peter Svensson Date: Thu, 23 Apr 2020 20:05:02 +0200 Subject: [PATCH 5/8] experimenting with getting credentials as needed instead of setting them at node-creation --- package.json | 2 +- storage/storage-list.js | 5 +++-- storage/storage-read.js | 8 +++++--- storage/storage-write.js | 8 +++++--- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index fcf9d3c..999e314 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-red-contrib-firebase-admin", - "version": "1.1.19", + "version": "1.1.20", "description": "A node-red module that wraps the server-side admin SDK of firebase, firestore, et.c.", "main": "index.js", "scripts": { diff --git a/storage/storage-list.js b/storage/storage-list.js index b0c8bf4..dcb1e6a 100644 --- a/storage/storage-list.js +++ b/storage/storage-list.js @@ -10,8 +10,7 @@ module.exports = function(RED) { if(config.cred){ let c = RED.nodes.getNode(config.cred) this.admin = c.admin - let global = this.context().global - this.storage = c.storage || global.get('cloud-storage') + this.config = c; this.bucket = config.bucket || c.bucket this.path = config.path } @@ -20,6 +19,8 @@ module.exports = function(RED) { //console.log('configuring storage-list to listen for messages') node.on('input', function(msg) { if(msg && msg.payload){ + let global = this.context().global + this.storage = this.config.storage || global.get('cloud-storage') let path = msg.payload.path || this.path let bucket = msg.payload.bucket || this.bucket console.log('storage-list listing files from bucket "'+bucket+'" path "'+path+'"') diff --git a/storage/storage-read.js b/storage/storage-read.js index d86393f..06d8ba8 100644 --- a/storage/storage-read.js +++ b/storage/storage-read.js @@ -9,6 +9,7 @@ module.exports = function(RED) { if(config.cred){ let c = RED.nodes.getNode(config.cred) + this.config = c; this.admin = c.admin let global = this.context().global this.storage = c.storage || global.get('cloud-storage') @@ -18,15 +19,16 @@ module.exports = function(RED) { console.log('config is '+config) } - let global = this.context().global - this.storage = global.get('cloud-storage') - console.log('* storage-read set this.storage to '+this.storage) + //console.log('configuring storage-read to listen for messages') node.on('input', function(msg) { if(msg && msg.payload){ let path = msg.payload.path || msg.path || this.path let bucket = msg.payload.bucket || msg.bucket || this.bucket + let global = this.context().global + this.storage = global.get('cloud-storage') + console.log('* storage-read set this.storage to '+this.storage) console.log('------------------------------ storage-read reading from bucket "'+bucket+'" path "'+path+'" this.storage = '+this.storage) if(msg.payload.files && msg.payload.files.length > 0){ console.log('--reading from files') diff --git a/storage/storage-write.js b/storage/storage-write.js index 595ac8f..4799311 100644 --- a/storage/storage-write.js +++ b/storage/storage-write.js @@ -7,10 +7,10 @@ module.exports = function(RED) { if(config.cred){ - let c = RED.nodes.getNode(config.cred) + let c = RED.nodes.getNode(config.cred); + this.config = c; this.admin = c.admin - let global = this.context().global - this.storage = c.storage || global.get('cloud-storage') + this.bucket = config.bucket || c.bucket this.path = config.path /* @@ -24,6 +24,8 @@ module.exports = function(RED) { node.on('input', function(msg) { if(msg && msg.payload){ + let global = this.context().global + this.storage = this.config.storage || global.get('cloud-storage') 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 From 0ed1f8f38290b616a2ba949e513ff7fc401e7684 Mon Sep 17 00:00:00 2001 From: Peter Svensson Date: Sun, 26 Apr 2020 15:19:43 +0200 Subject: [PATCH 6/8] added storage-delete node --- README.md | 19 ++++++-- package.json | 5 +- storage/storage-read.js | 1 + storage/storage-write.js | 98 +++++++++++++++++++++------------------- 4 files changed, 72 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 9a94563..49923bc 100644 --- a/README.md +++ b/README.md @@ -42,12 +42,12 @@ output: whatever data was at the path "foo/bar" in the rtdb database, when chang ## 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"}} +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"}} +input: {"payload": {"path": "foo/bar", "obj": {"the": "object"}}} ## rtdb-query Set up a reactive query for a path in the rtdb database. @@ -181,7 +181,20 @@ output: An array of google cloud-storage File objects. If you take this output a The storage-read module will read all file contents and output an object of filename keyed Buffer objects instead of the normal one. - +## storage-delete +Deletes a file at a given path under a given cloud storage bucket. The default bucket to be used can be set in the general firebase SDK settings. +If the payload defines an optional bucket property, it will override the default bucket settings. + +input: + + { + "payload": { + "bucket": "xyzzyz123.appspot.com", + "path": "myFile.txt" + } + } + +output: An array of headers returned by the operation if all went well. These are mostly useless but at least a confirmation of success. # Auth nodes diff --git a/package.json b/package.json index 999e314..2408b15 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-red-contrib-firebase-admin", - "version": "1.1.20", + "version": "1.1.21", "description": "A node-red module that wraps the server-side admin SDK of firebase, firestore, et.c.", "main": "index.js", "scripts": { @@ -39,7 +39,8 @@ "rtdb-to-flow": "rtdb/rtdb-to-flow.js", "storage-read": "storage/storage-read.js", "storage-write": "storage/storage-write.js", - "storage-list": "storage/storage-list.js" + "storage-list": "storage/storage-list.js", + "storage-delete": "storage/storage-delete.js" } }, "dependencies": { diff --git a/storage/storage-read.js b/storage/storage-read.js index 06d8ba8..786bd30 100644 --- a/storage/storage-read.js +++ b/storage/storage-read.js @@ -70,6 +70,7 @@ module.exports = function(RED) { }) } catch(ex){ console.log('storage-read caught exception: '+ex) + msg.payload = ex node.send(msg) } } else { diff --git a/storage/storage-write.js b/storage/storage-write.js index 4799311..29db4e6 100644 --- a/storage/storage-write.js +++ b/storage/storage-write.js @@ -1,57 +1,63 @@ - -module.exports = function(RED) { - +module.exports = function (RED) { function FirebaseAdmin(config) { RED.nodes.createNode(this, config); var node = this; - - if(config.cred){ + if (config.cred) { let c = RED.nodes.getNode(config.cred); this.config = c; - this.admin = c.admin - - this.bucket = config.bucket || c.bucket - this.path = config.path - /* - console.log('configuring storage-write to listen for messages. config is') - console.dir(config) - console.log('storage is') - console.dir(this.storage) - */ + this.admin = c.admin; + + this.bucket = config.bucket || c.bucket; + this.path = config.path; } - - node.on('input', function(msg) { - if(msg && msg.payload){ - let global = this.context().global - this.storage = this.config.storage || global.get('cloud-storage') - 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 || 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)) - const myBucket = this.storage.bucket(bucket); - const file = myBucket.file(path); - file.save(contents, options, function(err) { - if (!err) { - // File written successfully. - msg.payload={success:true, filename: path} - } else { - console.log('cloud storage write error: '+JSON.stringify(err)) - msg.payload ={success:false, filename: path} + node.on( + "input", + function (msg) { + try { + if (msg && msg.payload) { + let global = this.context().global; + this.storage = this.config.storage || global.get("cloud-storage"); + 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 || 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 + ); + const myBucket = this.storage.bucket(bucket); + const file = myBucket.file(path); + file.save(contents, options, function (err) { + if (!err) { + // File written successfully. + msg.payload = { success: true, filename: path }; + } else { + console.log( + "cloud storage write error: " + JSON.stringify(err) + ); + msg.payload = { success: false, filename: path }; + } + node.send(msg); + }); } - node.send(msg) - }); - } - }.bind(this)); - - + } catch (ex) { + console.log("storage-read caught exception: " + ex); + msg.payload = ex; + node.send(msg); + } + }.bind(this) + ); } RED.nodes.registerType("storage-write", FirebaseAdmin); -} \ No newline at end of file +}; From a178ddcc762b3b14acbff7392af4ba9a3b7b8d58 Mon Sep 17 00:00:00 2001 From: Peter Svensson Date: Mon, 27 Apr 2020 08:47:22 +0200 Subject: [PATCH 7/8] changing return value for delete-storage to true or false --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2408b15..8c6b6bd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-red-contrib-firebase-admin", - "version": "1.1.21", + "version": "1.1.22", "description": "A node-red module that wraps the server-side admin SDK of firebase, firestore, et.c.", "main": "index.js", "scripts": { From dd7e3e248ed483975155f975820aa120b2be2ee5 Mon Sep 17 00:00:00 2001 From: Kunal Kamble Date: Sun, 10 May 2020 00:41:25 -0400 Subject: [PATCH 8/8] Add merge option while setting value --- firestore/firestore-set.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/firestore/firestore-set.js b/firestore/firestore-set.js index cf74baf..cc7d011 100644 --- a/firestore/firestore-set.js +++ b/firestore/firestore-set.js @@ -14,10 +14,9 @@ module.exports = function(RED) { if(msg && msg.payload){ console.log('firestore-set got input') console.dir(msg) - const path = msg.payload.path - const obj = msg.payload.obj + const {path, obj, merge} = msg.payload console.log('storing '+obj+' at firestore path '+path) - this.admin.firestore().doc(path).set(obj).then((res)=>{ + this.admin.firestore().doc(path).set(obj, { merge }).then((res)=>{ console.log('firestore set result '+res) console.dir(res) }) @@ -27,4 +26,4 @@ module.exports = function(RED) { } RED.nodes.registerType("firestore-set", FirebaseAdmin); -} \ No newline at end of file +}