How to join denormalized data that goes two+ levels deep in Firebase
Date : March 29 2020, 07:55 AM
help you fix your problem See a working example hereThe gist of this sort of denormalization is to fetch the users as you grab posts. It's nothing more complex than it sounds. Just go grab them. <h3>Normalizing user profiles into posts</h3>
<ul ng-controller="ctrl">
<li ng-repeat="post in posts | orderByPriority" ng-init="user = users.$load(post.user)">
{{user.name}}: {{post.title}}
</li>
</ul>
var app = angular.module('app', ['firebase']);
var fb = new Firebase(URL);
app.controller('ctrl', function ($scope, $firebase, userCache) {
$scope.posts = $firebase(fb.child('posts'));
$scope.users = userCache(fb.child('users'));
});
app.factory('userCache', function ($firebase) {
return function (ref) {
var cachedUsers = {};
cachedUsers.$load = function (id) {
if( !cachedUsers.hasOwnProperty(id) ) {
cachedUsers[id] = $firebase(ref.child(id));
}
return cachedUsers[id];
};
cachedUsers.$dispose = function () {
angular.forEach(cachedUsers, function (user) {
user.$off();
});
};
return cachedUsers;
}
});
|
Firebase - Indexing Data Two Levels Deep
Date : March 29 2020, 07:55 AM
Does that help I have a Firebase data structure that looks like: , You can specify dynamic .indexOn like this: {
"rules" : {
"messages" : {
"$userId": {
".indexOn" : ["message", "direction"]
}
}
}
}
|
Access to Deep data via Firebase Admin
Date : March 29 2020, 07:55 AM
Does that help How Can I Access to Deep data via Firebase Admin? , To get the keyboard children: const ref = db.ref('keyboards/SecendKeyboard/childs');
ref.on("value", function (snapshot) {
console.log(snapshot.val());
});
const ref = db.ref('keyboards/SecendKeyboard');
ref.on("value", function (snapshot) {
console.log(snapshot.child("childs").val());
});
const ref = db.ref('keyboards');
ref.on("value", function (snapshot) {
snapshot.forEach(function(childSnapshot) {
console.log(snapshot.val()); // prints StartKeyboard and SecendKeyboard
if (snapshot.child("SecendKeyboard").exists()) {
console.log(snapshot.child("SecendKeyboard").val());
}
})
});
|
How to target specific field in Firebase / AngularFire when more than 1 level deep
Date : March 29 2020, 07:55 AM
|
Is there any way to auto increment a field in firebase
Date : March 29 2020, 07:55 AM
Any of those help I want to auto increment a stock number for each new item added to the firestore database. (Not using the push() method) , You can do it easily using Cloud Functions // Change '/COLLECTION/{DOC}' to which document do you want to increment the counter when it's created
exports.counter = functions.firestore.document('/COLLECTION/{DOC}').onCreate((snap, context) => {
const db = admin.firestore();
// Change 'counter/ref' to where do you want to increment
const countRef = db.doc('counter/ref');
return db.runTransaction(t => {
return t.get(countRef).then(doc => {
const counter = (doc.data().counter || 0) + 1;
t.update(countRef, {counter: counter});
});
});
});
|