【HH原创】Firebase 相关操作及代码示例
今天需要对 Firebase 增加删除功能,代码精简如下:
var admin = require('firebase-admin');
var config = require('./config.json');
var defaultAppConfig = {
credential: admin.credential.cert(config.firebase.cert),
databaseURL: config.firebase.databaseURL
};
var defaultAppName = 'GoPeople-NodeJS-Admin';
var defaultApp = admin.initializeApp(defaultAppConfig, defaultAppName);
var signaturesRef = defaultApp.database().ref('signatures');
signaturesRef.orderByChild("isChecked").equalTo(true).limitToLast(10).once("value")
.then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var key = childSnapshot.key;
var childData = childSnapshot.val();
var now = new Date();
var date = new Date(childData.date);
var dayDiff = parseInt((now - date) / (1000 * 60 * 60 * 24)); // day diff
if(dayDiff >30){
signaturesRef.child(key).remove(function(error) {
console.log(key);
console.log(dayDiff);
console.log(error ? ("Uh oh! " + error) : "Success!");
});
}else{
console.log(key);
console.log(dayDiff);
}
});
}); Firebase 修改节点:
function finishJobSync(jobGuid) {
var signaturesRef = defaultApp.database().ref('signatures').child(jobGuid);
signaturesRef.update({isChecked: true},function(error) {
if (error) {
logger.error(error);
} else {
logger.info('Job ' + jobGuid + ' signature has been synced.');
}
});
} Firebase 监听:
var signaturesRef = defaultApp.database().ref('signatures');
signaturesRef.orderByChild("isChecked").equalTo(false).on("child_added", function(snapshot, prevChildKey) {
// TODO:
}); admin.database.DataSnapshot
>> key
// Assume we have the following data in the Database:
{
"name": {
"first": "Ada",
"last": "Lovelace"
}
}
var ref = admin.database().ref("users/ada");
ref.once("value")
.then(function(snapshot) {
var key = snapshot.key; // "ada"
var childKey = snapshot.child("name/last").key; // "last"
}); >> child
var rootRef = admin.database().ref();
rootRef.once("value")
.then(function(snapshot) {
var key = snapshot.key; // null
var childKey = snapshot.child("users/ada").key; // "ada"
}); >> exists
// Assume we have the following data in the Database:
{
"name": {
"first": "Ada",
"last": "Lovelace"
}
}
// Test for the existence of certain keys within a DataSnapshot
var ref = admin.database().ref("users/ada");
ref.once("value")
.then(function(snapshot) {
var a = snapshot.exists(); // true
var b = snapshot.child("name").exists(); // true
var c = snapshot.child("name/first").exists(); // true
var d = snapshot.child("name/middle").exists(); // false
}); >> foreach
// Assume we have the following data in the Database:
{
"users": {
"ada": {
"first": "Ada",
"last": "Lovelace"
},
"alan": {
"first": "Alan",
"last": "Turing"
}
}
}
// Loop through users in order with the forEach() method. The callback
// provided to forEach() will be called synchronously with a DataSnapshot
// for each child:
var query = admin.database().ref("users").orderByKey();
query.once("value")
.then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
// key will be "ada" the first time and "alan" the second time
var key = childSnapshot.key;
// childData will be the actual contents of the child
var childData = childSnapshot.val();
});
}); >> hasChildren
// Assume we have the following data in the Database:
{
"name": {
"first": "Ada",
"last": "Lovelace"
}
}
var ref = admin.database().ref("users/ada");
ref.once("value")
.then(function(snapshot) {
var a = snapshot.hasChildren(); // true
var b = snapshot.child("name").hasChildren(); // true
var c = snapshot.child("name/first").hasChildren(); // false
}); >> numChildren
// Assume we have the following data in the Database:
{
"name": {
"first": "Ada",
"last": "Lovelace"
}
}
var ref = admin.database().ref("users/ada");
ref.once("value")
.then(function(snapshot) {
var a = snapshot.numChildren(); // 1 ("name")
var b = snapshot.child("name").numChildren(); // 2 ("first", "last")
var c = snapshot.child("name/first").numChildren(); // 0
}); admin.database.Query
>> startAt, endAt
// Find all dinosaurs that are at least three meters tall.
var ref = admin.database().ref("dinosaurs");
ref.orderByChild("height").startAt(3).on("child_added", function(snapshot) {
console.log(snapshot.key)
});
// Find all dinosaurs whose names come before Pterodactyl lexicographically.
var ref = admin.database().ref("dinosaurs");
ref.orderByKey().endAt("pterodactyl").on("child_added", function(snapshot) {
console.log(snapshot.key);
}); >> limitToFirst, limitToLast
// Find the two shortest dinosaurs.
var ref = admin.database().ref("dinosaurs");
ref.orderByChild("height").limitToFirst(2).on("child_added", function(snapshot) {
// This will be called exactly two times (unless there are less than two
// dinosaurs in the Database).
// It will also get fired again if one of the first two dinosaurs is
// removed from the data set, as a new dinosaur will now be the second
// shortest.
console.log(snapshot.key);
});
// Find the two heaviest dinosaurs.
var ref = admin.database().ref("dinosaurs");
ref.orderByChild("weight").limitToLast(2).on("child_added", function(snapshot) {
// This callback will be triggered exactly two times, unless there are
// fewer than two dinosaurs stored in the Database. It will also get fired
// for every new, heavier dinosaur that gets added to the data set.
console.log(snapshot.key);
}); ......