added on nodes for rtdb and firestore andmade nodes pass through msg objects with as little changes as possible
This commit is contained in:
@ -47,4 +47,10 @@
|
||||
|
||||
<script type="text/x-red" data-help-name="firestore-add">
|
||||
<p>A node that wraps the firestore-add SDK</p>
|
||||
Adds the new object under the collection the path describes and assigns it a random id
|
||||
<p>
|
||||
input: {"payload": {"path": "foo/bar"}, {"some": object, "foo": 17}}
|
||||
<p>
|
||||
output: The id of the new document
|
||||
|
||||
</script>
|
||||
@ -53,4 +53,9 @@
|
||||
|
||||
<script type="text/x-red" data-help-name="firestore-get">
|
||||
<p>A node that wraps the firestore-get SDK</p>
|
||||
Get data from a document path in the firestore database
|
||||
<p>
|
||||
input: {"payload": {"path": "foo/bar"}}
|
||||
<p>
|
||||
output: <the document at the path "foo/bar" in the firestore database>
|
||||
</script>
|
||||
@ -1,5 +1,5 @@
|
||||
|
||||
|
||||
let msgin
|
||||
let unsub
|
||||
|
||||
module.exports = function(RED) {
|
||||
@ -16,7 +16,7 @@ module.exports = function(RED) {
|
||||
if(unsub){
|
||||
unsub()
|
||||
}
|
||||
this.admin.firestore().doc(path).onSnapshot(cb)
|
||||
this.admin.firestore().doc(path).get().then(cb)
|
||||
}
|
||||
|
||||
const cb = (res)=>{
|
||||
@ -24,11 +24,17 @@ module.exports = function(RED) {
|
||||
console.dir(res)
|
||||
let val = res.data()
|
||||
console.log('val='+val)
|
||||
node.send({payload:val})
|
||||
if(msgin){
|
||||
msgin.payload = val
|
||||
node.send(msgin)
|
||||
} else {
|
||||
node.send({payload: val})
|
||||
}
|
||||
}
|
||||
|
||||
node.on('input', function(msg) {
|
||||
if(msg && msg.payload){
|
||||
msgin = msg
|
||||
const path = msg.payload.path
|
||||
setup(path)
|
||||
|
||||
|
||||
61
firestore/firestore-on.html
Normal file
61
firestore/firestore-on.html
Normal file
@ -0,0 +1,61 @@
|
||||
<script type="text/javascript">
|
||||
RED.nodes.registerType('firestore-on',{
|
||||
|
||||
category: 'firebase-admin',
|
||||
color: '#a6bbcf',
|
||||
defaults: {
|
||||
name: {value:""},
|
||||
path: {value:""},
|
||||
cred: {value: "", type: 'firebase-config'}
|
||||
},
|
||||
inputs:1,
|
||||
outputs:1,
|
||||
icon: "firebase-admin-icon.png",
|
||||
label: function() {
|
||||
return this.name||"firestore-on";
|
||||
},
|
||||
|
||||
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-on">
|
||||
<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-path"><i class="icon-tag"></i> Path</label>
|
||||
<input type="text" id="node-input-path" placeholder="Path">
|
||||
</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="Credentials">
|
||||
</div>
|
||||
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-help-name="firestore-on">
|
||||
<p>A node that wraps the firestore-on SDK</p>
|
||||
Get data from a document path in the firestore database
|
||||
<p>
|
||||
input: {"payload": {"path": "foo/bar"}}
|
||||
<p>
|
||||
output: <the document at the path "foo/bar" in the firestore database>
|
||||
</script>
|
||||
51
firestore/firestore-on.js
Normal file
51
firestore/firestore-on.js
Normal file
@ -0,0 +1,51 @@
|
||||
|
||||
let msgin
|
||||
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 setup = ()=>{
|
||||
if(unsub){
|
||||
unsub()
|
||||
}
|
||||
this.admin.firestore().doc(path).onSnapshot(cb)
|
||||
}
|
||||
|
||||
const cb = (res)=>{
|
||||
console.log('firestore get result '+res)
|
||||
console.dir(res)
|
||||
let val = res.data()
|
||||
console.log('val='+val)
|
||||
if(msgin){
|
||||
msgin.payload = val
|
||||
node.send(msgin)
|
||||
} else {
|
||||
node.send({payload: val})
|
||||
}
|
||||
}
|
||||
|
||||
node.on('input', function(msg) {
|
||||
if(msg && msg.payload){
|
||||
msgin = msg
|
||||
const path = msg.payload.path
|
||||
setup(path)
|
||||
|
||||
}
|
||||
}.bind(this));
|
||||
|
||||
if(config.path){
|
||||
setup(config.path)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
RED.nodes.registerType("firestore-on", FirebaseAdmin);
|
||||
}
|
||||
@ -47,4 +47,25 @@
|
||||
|
||||
<script type="text/x-red" data-help-name="firestore-query">
|
||||
<p>A node that wraps the firestore-query SDK</p>
|
||||
Set up a reactive query for a collection in the firestore database.
|
||||
<p>
|
||||
input:
|
||||
<p>
|
||||
{
|
||||
"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}
|
||||
]
|
||||
}
|
||||
<p>
|
||||
output: An array of the results of the query.
|
||||
|
||||
</script>
|
||||
@ -1,4 +1,4 @@
|
||||
|
||||
let msgin
|
||||
let unsub
|
||||
|
||||
module.exports = function(RED) {
|
||||
@ -16,13 +16,15 @@ module.exports = function(RED) {
|
||||
console.dir(res)
|
||||
let val = res.docs.map((d)=>{return d.data()})
|
||||
console.log('val='+val)
|
||||
node.send({payload:val})
|
||||
msgin.payload = val
|
||||
node.send(msgin)
|
||||
}
|
||||
|
||||
node.on('input', function(msg) {
|
||||
console.log('firestore-query got input')
|
||||
console.dir(msg.payload)
|
||||
if(msg && msg.payload){
|
||||
msgin = msg
|
||||
const path = msg.payload.path
|
||||
if(unsub){
|
||||
unsub()
|
||||
|
||||
@ -47,4 +47,7 @@
|
||||
|
||||
<script type="text/x-red" data-help-name="firestore-set">
|
||||
<p>A node that wraps the firestore-set SDK</p>
|
||||
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.
|
||||
<p>
|
||||
input: {"payload": {"path": "foo/bar"}, {"some": object, "foo": 17}}
|
||||
</script>
|
||||
Reference in New Issue
Block a user