firebase - Why use an object when denormalising data? -
in recent blog post on denormalising data, suggests logging of user's comments beneath each user so:
comments: { comment1: true, comment2: true }
why not list so:
comments: [ "comment1", "comment2", ]
what advantages? there difference @ all? while i'm @ it, how go generating unique references these comments distributed app? imagining list i'd push them onto end , let array take care of index.
firebase ever stores objects. js client converts arrays objects using index key. so, instance if store following array using set
:
comments: [ "comment1", "comment2" ]
in forge (the graphical debugger), show as:
comments: 0: comment1 1: comment2
given this, storing id of comment directly key has advantage can refer directly in security rules, example, expression like:
root.child('comments').haschild($comment)
in order generate unique references these comments, please use push
(https://www.firebase.com/docs/managing-lists.html):
var commentsref = new firebase("https://<example>.firebaseio.com/comments"); var id = commentsref.push({content: "hello world!", author: "alice"}); console.log(id); // unique identifier comment added.
Comments
Post a Comment