Diogo Barroso
Diogo Barroso

Reputation: 915

Firebase data Structure

Im trying to develop little todo app using firebase and angular. Im struggling a bit with the structure of the data since Im not used of not dealing with arrays.
Firebase structure

The "random letters" are the unique id generated by $push() in firebase. Would this be a correct way of structuring my data in firebase?

Upvotes: 0

Views: 339

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

There is no such thing as "the correct way", unless we go through all of your use-cases. That said: your approach sounds reasonable.

I would make one change thought and change your data structure to:

{
  "lists": {
    "jhgyftdr": {
      "feref3f344f":"Item 1",
      "fewfw4":"Item 2"
    }
  },
  "users":{
    "user1":{
      "name":"john doe"
      "lists":
        "jhgyftdr": true
    }
  }
}

So this simply uses the (push-generated) name() under users -> lists.

With this structure you could load the lists for a user with something like this:

var ref = new Firebase('https://your.firebaseio.com/');

ref.child('users/user1/lists').on('value', function(lists) {
  lists.forEach(function(listSnapshot) {
    ref.child('lists/'+listSnapshot.name()).once('value', function(listSnapshot) {
      console.log(listSnapshot.val());
    });
  });
});

Please don't include screenshots of text. I had to now go and capture your data; time which I could've spent answering questions.

Upvotes: 2

Related Questions