From ae90a7c22564a4a5c09887d729a6bcdfe57e9dba Mon Sep 17 00:00:00 2001 From: Peter Svensson Date: Tue, 2 Jul 2019 19:45:37 +0200 Subject: [PATCH] added global firebase context settings --- README.md | 9 +++++++++ firebase-config.js | 2 ++ package.json | 2 +- storage-read.js | 27 ++++++--------------------- 4 files changed, 18 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index b9e8e19..26acbfd 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,15 @@ This means that the nodes can run outside of the normal security rules, in admin The configuration needs two parameters, the first is the text of the service account credentials private key file. This can be found by going to the firebase console, then to the cogwheel icon at the top left then to project settings and then "Service Accounts". Click "Generate new private key)". The second parameter is the database url for the database, and th url has the format of "https://myprojectname.firebaseio.com" and can also be fpund in the above place in the firebase console. +After initializing, the reference to the firebase SDK is stored in the global context variable 'firebase', which can be used +in any function like this; + + let fb = global.get('firebase') + fb.firestore().doc('foo/bar').get().then((ref)=>{ + let d = ref.data() + node.send( {payload: {data: d}}); + }) + # Realtime Database (rtdb) Nodes ## rtdb-get diff --git a/firebase-config.js b/firebase-config.js index 48cdc72..02f45df 100644 --- a/firebase-config.js +++ b/firebase-config.js @@ -28,6 +28,8 @@ module.exports = function(RED) { credential: credobj, databaseURL: this.dburl }); + let global = this.context().global + global.set('firebase', _admin) console.log('setting storage....') s = new Storage({ diff --git a/package.json b/package.json index 263df51..851a610 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-red-contrib-firebase-admin", - "version": "1.0.65", + "version": "1.0.7", "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-read.js b/storage-read.js index 1dd2403..febafe1 100644 --- a/storage-read.js +++ b/storage-read.js @@ -26,29 +26,14 @@ module.exports = function(RED) { let path = msg.payload.path || this.path let bucket = msg.payload.bucket || this.bucket console.log('storage-read reading from bucket "'+bucket+'" path "'+path+'"') - - this.storage.getBuckets().then((results)=>{ - const [buckets] = results; - - console.log('Buckets:'); - buckets.forEach(bucket => { - console.log(bucket.name); - }); - - - this.storage - .bucket(bucket) - .file(path).download().then((file)=>{ - console.log('storage-read got file') - //console.dir(file) - node.send({payload:file}) - - }) - + this.storage + .bucket(bucket) + .file(path).download().then((file)=>{ + console.log('storage-read got file') + //console.dir(file) + node.send({payload:file}) }) - - } }.bind(this));