Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I always add new objects to list of objects via $push()?

I'm creating a data structure for Firebase and AngularFire consisting of Users, Posts, and Comments. I was under the impression that the key/id for users would be the username, and that the key/id for comments and posts would be the auto-generated firebase key.

I've been working my through the angularfire documentation and am confused about the auto-generated keys (name()) that is added to an object when the $push() method is used.

Looking at some examples on the firebase website I see that an example of a Users object does not have the auto-generated key -- the key for an individual user is the username -- but at the same time a key is added whenever you add an object to the array via $push

My question is:

1) Should I always be using the firebase auto-generated keys? And if not, then how do I add a new user since $push() automatically creates the key, and $set() would reset all of my users?

2) What is the relationship between $id and name()?

Example Data

From https://www.firebase.com/docs/web/guide/saving-data.html

The docs show the following Users object:

{
  "users": {
    "alanisawesome": {
      "date_of_birth": "June 23, 1912",
      "full_name": "Alan Turing"
    },
    "gracehop": { 
      "date_of_birth": "December 9, 1906",
      "full_name": "Grace Hopper"
    }
  }
}

How would I add more users without resetting my current users with $set() or adding the angularfire id with push()?

And then a Posts object with the generated id:

{
  "posts": {
    "-JRHTHaIs-jNPLXOQivY": {
      "author": "gracehop",
      "title": "Announcing COBOL, a New Programming Language"
    },
    "-JRHTHaKuITFIhnj02kE": {
      "author": "alanisawesome",
      "title": "The Turing Machine"
    }
  }
}

Thanks very much.

like image 471
reknirt Avatar asked Feb 12 '23 07:02

reknirt


2 Answers

The short answer: you probably don't want to use push to store your users.

If you're getting your key from another source, like a uid from Simple Login, you will almost certainly want to use the uid to organize your users and their data in your firebase.

This is because, your users' ongoing sessions always provide you with that same uid which you can use to look up their user data and their stuff.

And you can safely use set in this case without resetting all of your users if you set based on that known user id.

But what I think you're getting at is, So in general, when do you set vs push?

A typical blog might look something like this in Firebase:

{ 
  'users' : {

    // uid from Simple Login, that you used with set()
    'google-1234' : {  
        'displayName' : 'Jane Smith',
        ...
    } 
    , ...
  },
  'posts' : {

    // a blog post ID you pick and use for set()
    'blog-post-id-i-use-in-the-url' : { 
      'title' : 'Blog Post Title',
      'contents' : 'Four score and seven...'
     }, ...
   }
  'postComments' {
    'blog-post-id-i-use-in-the-url' : { 
      // Firebase generated ID done with push()
      '_fe31ca1' : { 

        // uid from simple login (assuming comments require auth)
        'commenterUserId': 'google-5678', 
        'commentBody': 'cats back for everyone!'
      } ... other comments ...
    }
  }
}

In this example we use set when inserting new users and posts because we get a good unique ID from another source. These IDs are good because they allow us to easily recall the content later based on that ID.

We use push for comments, though. We don't have a good ID from another source, and order does matter, so we let Firebase generate a key for us. This works out OK because most of the time we're working with comments relative to an entry, so we can just grab them all as needed.

like image 51
mimming Avatar answered Mar 02 '23 17:03

mimming


Following what mimmming said, I found a solution to this.

Have your add user function take an id as a parameter. this will be the authData.uid for the user you want to save.

Then append that id to the firebase link to make a new user using set. Any other user you add using set will not wipe this since it is an entire new branch of your database under users. No firebase unique id too.

$scope.addUSer = function(id){
//pass the id in, andd append it to the end of your url link
var usersRef = new Firebase("https//<your fire base>.firebaseio.com/Users/"+id);
usersRef.set($scope.newUserData);

};

like image 32
Darlington Uzoma Avatar answered Mar 02 '23 18:03

Darlington Uzoma