From 782299982c6ef046b0647becaa46a39fce8e580d Mon Sep 17 00:00:00 2001 From: Peter Svensson Date: Sun, 9 Jun 2019 19:17:30 +0200 Subject: [PATCH] added basic firestore nodes for get,set,add and query --- README.md | 49 +++++++++++++++++++++++++++++-- firestore-add.html | 50 +++++++++++++++++++++++++++++++ firestore-add.js | 30 +++++++++++++++++++ firestore-get.html | 50 +++++++++++++++++++++++++++++++ firestore-get.js | 37 +++++++++++++++++++++++ firestore-query.html | 50 +++++++++++++++++++++++++++++++ firestore-query.js | 70 ++++++++++++++++++++++++++++++++++++++++++++ firestore-set.html | 50 +++++++++++++++++++++++++++++++ firestore-set.js | 30 +++++++++++++++++++ package.json | 4 +++ rtdb-get.html | 2 +- rtdb-get.js | 2 -- rtdb-push.html | 2 +- rtdb-push.js | 2 -- rtdb-query.html | 2 +- rtdb-query.js | 2 -- rtdb-set.html | 2 +- rtdb-set.js | 2 -- 18 files changed, 421 insertions(+), 15 deletions(-) create mode 100644 firestore-add.html create mode 100644 firestore-add.js create mode 100644 firestore-get.html create mode 100644 firestore-get.js create mode 100644 firestore-query.html create mode 100644 firestore-query.js create mode 100644 firestore-set.html create mode 100644 firestore-set.js diff --git a/README.md b/README.md index d006997..25dde49 100644 --- a/README.md +++ b/README.md @@ -21,12 +21,17 @@ input: {payload: {path: 'foo/bar'}} output: ## rtdb-set -Set data at a path in the rtdb database. Use 'on' snapshot so will fire everytime the data at the path changes and so drive flow executin 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'}, {some: 'object', foo: 17}} + +## rtdb-push +Pushes the new object onto an array under the path input: {payload: {path: 'foo/bar'}, {some: 'object', foo: 17}} ## rtdb-query -Set up a reactive wuery for a path in the rtdb database. +Set up a reactive query for a path in the rtdb database. input: {payload: {path: 'foo/bar', queries:[], on: 'value}} @@ -46,7 +51,45 @@ output: [an array of results for the query] # Firestore nodes -TBD +## firestore-get +Get data from a document path in the firestore database + +input: {payload: {path: 'foo/bar'}} + +output: + +## 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. + +input: {payload: {path: 'foo/bar'}, {some: 'object', foo: 17}} + +## firestore-add +Adds the new object under the collection the path describes and assigns it a random id + +input: {payload: {path: 'foo/bar'}, {some: 'object', foo: 17}} + +## firestore-query +Set up a reactive query for a collection in the firestore database. + +input: + + { + payload: { + path: 'foo/bar', + queries:[], + limit: 7, + startAt: 4000, // follows the orderBy property + endAt: 4050, // follows the orderBy property + orderBy: 'shoeSize', + orderDirection: 'asc', //default is 'desc' + queries:[ + {"company", "==", "ACME"}, + {"createdAt", ">", 1560099394242} + ] + } + +output: An array of the results of the query. + # Storage nodes diff --git a/firestore-add.html b/firestore-add.html new file mode 100644 index 0000000..152b98a --- /dev/null +++ b/firestore-add.html @@ -0,0 +1,50 @@ + + + + + \ No newline at end of file diff --git a/firestore-add.js b/firestore-add.js new file mode 100644 index 0000000..2b9407b --- /dev/null +++ b/firestore-add.js @@ -0,0 +1,30 @@ + + +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('firestore-add got input') + console.dir(msg) + const path = msg.payload.path + const obj = msg.payload.obj + console.log('storing '+obj+' at rtdb path '+path) + this.admin.firestore().collection(path).add(obj).then((res)=>{ + console.log('firestore set result '+res) + console.dir(res) + }) + } + }.bind(this)); + + + } + RED.nodes.registerType("firestore-add", FirebaseAdmin); +} \ No newline at end of file diff --git a/firestore-get.html b/firestore-get.html new file mode 100644 index 0000000..4ac2ee6 --- /dev/null +++ b/firestore-get.html @@ -0,0 +1,50 @@ + + + + + \ No newline at end of file diff --git a/firestore-get.js b/firestore-get.js new file mode 100644 index 0000000..07de54b --- /dev/null +++ b/firestore-get.js @@ -0,0 +1,37 @@ + + +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 cb = (res)=>{ + console.log('firestore get result '+res) + console.dir(res) + let val = res.data() + console.log('val='+val) + node.send({payload:val}) + } + + node.on('input', function(msg) { + if(msg && msg.payload){ + const path = msg.payload.path + if(unsub){ + unsub() + } + this.admin.firestore().doc(path).onSnapshot(cb) + + } + }.bind(this)); + + + } + RED.nodes.registerType("firestore-get", FirebaseAdmin); +} \ No newline at end of file diff --git a/firestore-query.html b/firestore-query.html new file mode 100644 index 0000000..c0673f7 --- /dev/null +++ b/firestore-query.html @@ -0,0 +1,50 @@ + + + + + \ No newline at end of file diff --git a/firestore-query.js b/firestore-query.js new file mode 100644 index 0000000..d771fc0 --- /dev/null +++ b/firestore-query.js @@ -0,0 +1,70 @@ + +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 cb = (res)=>{ + console.log('firestore query result '+res) + console.dir(res) + let val = res.docs.map((d)=>{return d.data()}) + console.log('val='+val) + node.send({payload:val}) + } + + node.on('input', function(msg) { + console.log('firestore-query got input') + console.dir(msg.payload) + if(msg && msg.payload){ + const path = msg.payload.path + if(unsub){ + unsub() + } + let ref = this.admin.firestore().collection(path) + + let dir = msg.payload.orderDirection ? msg.payload.orderDirection : 'desc' + + if(msg.payload.orderBy){ + console.log('orderBy dir = '+dir) + ref = ref.orderBy(msg.payload.orderBy, dir) + } + if(msg.payload.limit){ + ref = ref.limit(msg.payload.limit) + } + if(msg.payload.startAt){ + console.log('startAt '+msg.payload.startAt) + ref = ref.startAt(msg.payload.startAt) + } + if(msg.payload.endAt){ + console.log('endAt '+msg.payload.endAt) + ref = ref.endAt(msg.payload.endAt) + } + + // Decorate with queries + if(msg.payload.queries && msg.payload.queries.length > 0){ + console.log('found queries') + msg.payload.queries.forEach((query)=>{ + console.dir(query) + ref = ref.where(query[0], query[1], query[2]) + }) + } + + console.log('finished firetore query is') + console.dir(ref) + + unsub = ref.onSnapshot(cb) + + } + }.bind(this)); + + + } + RED.nodes.registerType("firestore-query", FirebaseAdmin); +} \ No newline at end of file diff --git a/firestore-set.html b/firestore-set.html new file mode 100644 index 0000000..ac01a87 --- /dev/null +++ b/firestore-set.html @@ -0,0 +1,50 @@ + + + + + \ No newline at end of file diff --git a/firestore-set.js b/firestore-set.js new file mode 100644 index 0000000..cf74baf --- /dev/null +++ b/firestore-set.js @@ -0,0 +1,30 @@ + + +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('firestore-set got input') + console.dir(msg) + const path = msg.payload.path + const obj = msg.payload.obj + console.log('storing '+obj+' at firestore path '+path) + this.admin.firestore().doc(path).set(obj).then((res)=>{ + console.log('firestore set result '+res) + console.dir(res) + }) + } + }.bind(this)); + + + } + RED.nodes.registerType("firestore-set", FirebaseAdmin); +} \ No newline at end of file diff --git a/package.json b/package.json index 321e438..e7ed9c1 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,10 @@ "rtdb-push": "rtdb-push.js", "rtdb-get": "rtdb-get.js", "rtdb-query": "rtdb-query.js", + "firestore-set": "firestore-set.js", + "firestore-add": "firestore-add.js", + "firestore-get": "firestore-get.js", + "firestore-query": "firestore-query.js", "firebase-config": "firebase-config.js" } }, diff --git a/rtdb-get.html b/rtdb-get.html index 51a753a..7de3d5b 100644 --- a/rtdb-get.html +++ b/rtdb-get.html @@ -1,7 +1,7 @@