Merge pull request #1 from psvensson/master

pull from original repo
This commit is contained in:
Kunal Kamble
2020-07-28 03:04:04 -04:00
committed by GitHub
7 changed files with 90 additions and 63 deletions

View File

@ -42,12 +42,12 @@ output: whatever data was at the path "foo/bar" in the rtdb database, when chang
## rtdb-set ## 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. 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 ## rtdb-push
Pushes the new object onto an array under the path 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 ## rtdb-query
Set up a reactive query for a path in the rtdb database. 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. 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 # Auth nodes

View File

@ -12,7 +12,7 @@ module.exports = function(RED) {
this.admin = c.admin this.admin = c.admin
} }
const setup = ()=>{ const setup = (path)=>{
if(unsub){ if(unsub){
unsub() unsub()
} }
@ -48,4 +48,4 @@ module.exports = function(RED) {
} }
RED.nodes.registerType("firestore-get", FirebaseAdmin); RED.nodes.registerType("firestore-get", FirebaseAdmin);
} }

View File

@ -14,10 +14,9 @@ module.exports = function(RED) {
if(msg && msg.payload){ if(msg && msg.payload){
console.log('firestore-set got input') console.log('firestore-set got input')
console.dir(msg) console.dir(msg)
const path = msg.payload.path const {path, obj, merge} = msg.payload
const obj = msg.payload.obj
console.log('storing '+obj+' at firestore path '+path) 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.log('firestore set result '+res)
console.dir(res) console.dir(res)
}) })
@ -27,4 +26,4 @@ module.exports = function(RED) {
} }
RED.nodes.registerType("firestore-set", FirebaseAdmin); RED.nodes.registerType("firestore-set", FirebaseAdmin);
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "node-red-contrib-firebase-admin", "name": "node-red-contrib-firebase-admin",
"version": "1.1.16", "version": "1.1.22",
"description": "A node-red module that wraps the server-side admin SDK of firebase, firestore, et.c.", "description": "A node-red module that wraps the server-side admin SDK of firebase, firestore, et.c.",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
@ -39,7 +39,8 @@
"rtdb-to-flow": "rtdb/rtdb-to-flow.js", "rtdb-to-flow": "rtdb/rtdb-to-flow.js",
"storage-read": "storage/storage-read.js", "storage-read": "storage/storage-read.js",
"storage-write": "storage/storage-write.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": { "dependencies": {

View File

@ -10,7 +10,7 @@ module.exports = function(RED) {
if(config.cred){ if(config.cred){
let c = RED.nodes.getNode(config.cred) let c = RED.nodes.getNode(config.cred)
this.admin = c.admin this.admin = c.admin
this.storage = c.storage this.config = c;
this.bucket = config.bucket || c.bucket this.bucket = config.bucket || c.bucket
this.path = config.path this.path = config.path
} }
@ -19,6 +19,8 @@ module.exports = function(RED) {
//console.log('configuring storage-list to listen for messages') //console.log('configuring storage-list to listen for messages')
node.on('input', function(msg) { node.on('input', function(msg) {
if(msg && msg.payload){ 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 path = msg.payload.path || this.path
let bucket = msg.payload.bucket || this.bucket let bucket = msg.payload.bucket || this.bucket
console.log('storage-list listing files from bucket "'+bucket+'" path "'+path+'"') console.log('storage-list listing files from bucket "'+bucket+'" path "'+path+'"')

View File

@ -9,25 +9,27 @@ module.exports = function(RED) {
if(config.cred){ if(config.cred){
let c = RED.nodes.getNode(config.cred) let c = RED.nodes.getNode(config.cred)
this.config = c;
this.admin = c.admin this.admin = c.admin
this.storage = c.storage let global = this.context().global
storage = this.storage this.storage = c.storage || global.get('cloud-storage')
this.bucket = config.bucket || c.bucket this.bucket = config.bucket || c.bucket
this.path = config.path this.path = config.path
console.log('config is '+config) 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') //console.log('configuring storage-read to listen for messages')
node.on('input', function(msg) { node.on('input', function(msg) {
if(msg && msg.payload){ if(msg && msg.payload){
let path = msg.payload.path || msg.path || this.path let path = msg.payload.path || msg.path || this.path
let bucket = msg.payload.bucket || msg.bucket || this.bucket 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) 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){ if(msg.payload.files && msg.payload.files.length > 0){
console.log('--reading from files') console.log('--reading from files')
let count = msg.payload.files.length let count = msg.payload.files.length
@ -54,7 +56,7 @@ module.exports = function(RED) {
} else if(msg){ } else if(msg){
console.log('* reading single file from path '+path) console.log('* reading single file from path '+path)
try{ try{
let s = this.storage || storage let s = this.storage
s.bucket(bucket) s.bucket(bucket)
.file(path).download().then((file)=>{ .file(path).download().then((file)=>{
console.log('storage-read got file') console.log('storage-read got file')
@ -68,6 +70,7 @@ module.exports = function(RED) {
}) })
} catch(ex){ } catch(ex){
console.log('storage-read caught exception: '+ex) console.log('storage-read caught exception: '+ex)
msg.payload = ex
node.send(msg) node.send(msg)
} }
} else { } else {

View File

@ -1,54 +1,63 @@
module.exports = function (RED) {
module.exports = function(RED) {
function FirebaseAdmin(config) { function FirebaseAdmin(config) {
RED.nodes.createNode(this, config); RED.nodes.createNode(this, config);
var node = this; var node = this;
if (config.cred) {
let c = RED.nodes.getNode(config.cred);
this.config = c;
this.admin = c.admin;
if(config.cred){ this.bucket = config.bucket || c.bucket;
let c = RED.nodes.getNode(config.cred) this.path = config.path;
this.admin = c.admin
this.storage = c.storage
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)
*/
} }
node.on(
node.on('input', function(msg) { "input",
if(msg && msg.payload){ function (msg) {
let path = msg.payload.path || msg.path|| this.path try {
let bucket = msg.payload.bucket || msg.bucket || this.bucket if (msg && msg.payload) {
let contents = msg.payload.contents || msg.payload let global = this.context().global;
let options ={ this.storage = this.config.storage || global.get("cloud-storage");
contentType: msg.payload.contentType || msg.contentType || 'auto', let path = msg.payload.path || msg.path || this.path;
metadata: msg.payload.metadata || msg.metadata || {}, let bucket = msg.payload.bucket || msg.bucket || this.bucket;
private: msg.payload.private || msg.private || true, let contents = msg.payload.contents || msg.payload;
public: msg.payload.public || msg.public || true, let options = {
} contentType: msg.payload.contentType || msg.contentType || "auto",
console.log('storage-write writing file to bucket "'+bucket+'" path "'+path+'" contents is of type '+(typeof contents)) metadata: msg.payload.metadata || msg.metadata || {},
const myBucket = this.storage.bucket(bucket); private: msg.payload.private || msg.private || true,
const file = myBucket.file(path); public: msg.payload.public || msg.public || true,
file.save(contents, options, function(err) { };
if (!err) { console.log(
// File written successfully. 'storage-write writing file to bucket "' +
msg.payload={success:true, filename: path} bucket +
} else { '" path "' +
console.log('cloud storage write error: '+err) path +
msg.payload ={success:false, filename: 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) } catch (ex) {
}); console.log("storage-read caught exception: " + ex);
} msg.payload = ex;
}.bind(this)); node.send(msg);
}
}.bind(this)
);
} }
RED.nodes.registerType("storage-write", FirebaseAdmin); RED.nodes.registerType("storage-write", FirebaseAdmin);
} };