-
Notifications
You must be signed in to change notification settings - Fork 20
/
firebase_query.js
25 lines (21 loc) · 913 Bytes
/
firebase_query.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
module.exports = function(RED) {
"use strict";
var Firebase = require("firebase");
function sendMessageFromSnapshot(msg, snapshot) {
msg.href = snapshot.ref().toString();
msg.payload = snapshot.val();
this.send(msg);
}
function FirebaseQuery(n) {
RED.nodes.createNode(this,n);
this.firebaseurl = n.firebaseurl;
this.child = n.child;
this.firebase = new Firebase(this.firebaseurl);
this.on('input', function(msg) {
var childpath = (this.child) ? String(msg[this.child]) : ""; // get path from msg or default to /
childpath = (childpath.indexOf("/") == 0) ? childpath : "/" + childpath; // make sure the path starts with /
this.firebase.child(childpath).once('value', sendMessageFromSnapshot.bind(this, msg));
});
}
RED.nodes.registerType("firebase query", FirebaseQuery);
}