added basic firestore nodes for get,set,add and query

This commit is contained in:
Peter Svensson
2019-06-09 19:17:30 +02:00
parent 32a2fd439b
commit 782299982c
18 changed files with 421 additions and 15 deletions

View File

@ -21,12 +21,17 @@ input: {payload: {path: 'foo/bar'}}
output: <whatever data was at the path 'foo/bar' in the rtdb database>
## 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: <the document at the path 'foo/bar' in the firestore database>
## 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

50
firestore-add.html Normal file
View File

@ -0,0 +1,50 @@
<script type="text/javascript">
RED.nodes.registerType('firestore-add',{
category: 'firebase-admin',
color: '#a6bbcf',
defaults: {
name: {value:""},
cred: {value: "", type: 'firebase-config'}
},
inputs:1,
outputs:0,
icon: "firebase-admin-icon.png",
label: function() {
return this.name||"firestore-add";
},
oneditsave: function() {
let type = $('#apitype-select').val()
console.log('type is set to '+type)
},
oneditprepare: function() {
$('#apitype-select').change(function () {
$("#node-input-apitype").val($(this).find('option:selected').val())
});
$("#apitype-select").val($("#node-input-apitype").val())
$('#apitype-select').trigger('change');
}
});
</script>
<script type="text/x-red" data-template-name="firestore-add">
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-input-cred"><i class="icon-tag"></i> Credentials</label>
<input type="text" id="node-input-cred" placeholder="Name">
</div>
</script>
<script type="text/x-red" data-help-name="firestore-add">
<p>A node that wraps the firestore-add SDK</p>
</script>

30
firestore-add.js Normal file
View File

@ -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);
}

50
firestore-get.html Normal file
View File

@ -0,0 +1,50 @@
<script type="text/javascript">
RED.nodes.registerType('firestore-get',{
category: 'firebase-admin',
color: '#a6bbcf',
defaults: {
name: {value:""},
cred: {value: "", type: 'firebase-config'}
},
inputs:1,
outputs:1,
icon: "firebase-admin-icon.png",
label: function() {
return this.name||"firestore-get";
},
oneditsave: function() {
let type = $('#apitype-select').val()
console.log('type is set to '+type)
},
oneditprepare: function() {
$('#apitype-select').change(function () {
$("#node-input-apitype").val($(this).find('option:selected').val())
});
$("#apitype-select").val($("#node-input-apitype").val())
$('#apitype-select').trigger('change');
}
});
</script>
<script type="text/x-red" data-template-name="firestore-get">
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-input-cred"><i class="icon-tag"></i> Credentials</label>
<input type="text" id="node-input-cred" placeholder="Name">
</div>
</script>
<script type="text/x-red" data-help-name="firestore-get">
<p>A node that wraps the firestore-get SDK</p>
</script>

37
firestore-get.js Normal file
View File

@ -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);
}

50
firestore-query.html Normal file
View File

@ -0,0 +1,50 @@
<script type="text/javascript">
RED.nodes.registerType('firestore-query',{
category: 'firebase-admin',
color: '#a6bbcf',
defaults: {
name: {value:""},
cred: {value: "", type: 'firebase-config'}
},
inputs:1,
outputs:1,
icon: "firebase-admin-icon.png",
label: function() {
return this.name||"firestore-query";
},
oneditsave: function() {
let type = $('#apitype-select').val()
console.log('type is set to '+type)
},
oneditprepare: function() {
$('#apitype-select').change(function () {
$("#node-input-apitype").val($(this).find('option:selected').val())
});
$("#apitype-select").val($("#node-input-apitype").val())
$('#apitype-select').trigger('change');
}
});
</script>
<script type="text/x-red" data-template-name="firestore-query">
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-input-cred"><i class="icon-tag"></i> Credentials</label>
<input type="text" id="node-input-cred" placeholder="Name">
</div>
</script>
<script type="text/x-red" data-help-name="firestore-query">
<p>A node that wraps the firestore-query SDK</p>
</script>

70
firestore-query.js Normal file
View File

@ -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);
}

50
firestore-set.html Normal file
View File

@ -0,0 +1,50 @@
<script type="text/javascript">
RED.nodes.registerType('firestore-set',{
category: 'firebase-admin',
color: '#a6bbcf',
defaults: {
name: {value:""},
cred: {value: "", type: 'firebase-config'}
},
inputs:1,
outputs:0,
icon: "firebase-admin-icon.png",
label: function() {
return this.name||"firestore-set";
},
oneditsave: function() {
let type = $('#apitype-select').val()
console.log('type is set to '+type)
},
oneditprepare: function() {
$('#apitype-select').change(function () {
$("#node-input-apitype").val($(this).find('option:selected').val())
});
$("#apitype-select").val($("#node-input-apitype").val())
$('#apitype-select').trigger('change');
}
});
</script>
<script type="text/x-red" data-template-name="firestore-set">
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-input-cred"><i class="icon-tag"></i> Credentials</label>
<input type="text" id="node-input-cred" placeholder="Name">
</div>
</script>
<script type="text/x-red" data-help-name="firestore-set">
<p>A node that wraps the firestore-set SDK</p>
</script>

30
firestore-set.js Normal file
View File

@ -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);
}

View File

@ -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"
}
},

View File

@ -1,7 +1,7 @@
<script type="text/javascript">
RED.nodes.registerType('rtdb-get',{
category: 'function',
category: 'firebase-admin',
color: '#a6bbcf',
defaults: {
name: {value:""},

View File

@ -7,8 +7,6 @@ module.exports = function(RED) {
RED.nodes.createNode(this, config);
var node = this;
console.log('config is..')
console.dir(config)
if(config.cred){
let c = RED.nodes.getNode(config.cred)
this.admin = c.admin

View File

@ -1,7 +1,7 @@
<script type="text/javascript">
RED.nodes.registerType('rtdb-push',{
category: 'function',
category: 'firebase-admin',
color: '#a6bbcf',
defaults: {
name: {value:""},

View File

@ -5,8 +5,6 @@ module.exports = function(RED) {
RED.nodes.createNode(this, config);
var node = this;
console.log('config is..')
console.dir(config)
if(config.cred){
let c = RED.nodes.getNode(config.cred)
this.admin = c.admin

View File

@ -1,7 +1,7 @@
<script type="text/javascript">
RED.nodes.registerType('rtdb-query',{
category: 'function',
category: 'firebase-admin',
color: '#a6bbcf',
defaults: {
name: {value:""},

View File

@ -8,8 +8,6 @@ module.exports = function(RED) {
RED.nodes.createNode(this, config);
var node = this;
console.log('config is..')
console.dir(config)
if(config.cred){
let c = RED.nodes.getNode(config.cred)
this.admin = c.admin

View File

@ -1,7 +1,7 @@
<script type="text/javascript">
RED.nodes.registerType('rtdb-set',{
category: 'function',
category: 'firebase-admin',
color: '#a6bbcf',
defaults: {
name: {value:""},

View File

@ -5,8 +5,6 @@ module.exports = function(RED) {
RED.nodes.createNode(this, config);
var node = this;
console.log('config is..')
console.dir(config)
if(config.cred){
let c = RED.nodes.getNode(config.cred)
this.admin = c.admin