angularjs - AngularFire - How do I query denormalised data? -


ok im starting out fresh firebase. i've read this: https://www.firebase.com/docs/data-structure.html , i've read this: https://www.firebase.com/blog/2013-04-12-denormalizing-is-normal.html

so i'm suitably confused 1 seems contradict other. can structure data hierarchically, if want scalable don't. that's not actual problem.

i have following structure (please correct me if wrong) blog engine:

"authors" : {   "-jhvwke8jhuhevzyrj3o" : {     "useruid" : "simplelogin:7",     "email"   : "myemail@domain.com"   } }, "posts" : {   "-jhvwkj3zozanteniqfy" : {     "state" : "draft",     "body" : "this first post",     "title" : "my first blog",     "authorid" : "-jhvwke8jhuhevzyrj3o"   } } 

a list of authors , list of posts. first of want author useruid equals current user's uid. want posts authorid 1 provided query.

but have no idea how this. appreciated! i'm using angularfire if makes difference.

firebase nosql data store. it's json hierarchy , not have sql queries in traditional sense (these aren't compatible lightning-fast real-time ops; tend slow , expensive). there plans map reduce style functionality (merged views , tools assist this) primary weapon @ present proper data structure.

first of all, let's tackle tree hierarchy vs denormalized data. here's few things should denormalize:

  • lists want able iterate (a list of user names without having download every message user ever wrote or other meta info user)
  • large data sets view portions of, such list of rooms/groups user belongs (you should able fetch list of rooms given user without downloading groups/rooms in system, put index 1 place, master room data somewhere else)
  • anything more 1,000 records (keep lean speed)
  • children under path contain 1..n (i.e. possibly infinite) records (example chat messages chat room meta data, way can fetch info chat room without grabbing messages)

here's few things may not make sense denormalize:

  • data fetch en toto , never iterate (if use .child(...).on('value', ...) fetch record , display in record, never referring parent list, there's no reason optimize iterability)
  • lists shorter hundred or records whole (e.g. list of groups user belongs might fetched user , average 5-10 items; no reason keep split apart)

fetching author simple adding id url:

var userid = 123; new firebase('https://instance.firebaseio.com/users/'+userid); 

to fetch list of posts belonging user, either maintain index of users' posts:

/posts/$post_id/... /my_posts/$user_id/$post_id/true  var fb = new firebase('https://instance.firebaseio.com'); fb.child('/my_posts/'+userid).on('child_added', function(indexsnap) {    fb.child('posts/'+indexsnap.name()).once('value', function(datasnap) {        console.log('fetched post', indexsnap.name(), datasnap.val());    }); }); 

a tool firebase.util can assist normalizing data has been split storage until firebase's views , advanced querying utils released:

/posts/$post_id/... /my_posts/$user_id/$post_id/true  var fb = new firebase('https://instance.firebaseio.com'); var ref = firebase.util.intersection( fb.child('my_posts/'+userid), fb.child('posts') ); ref.on('child_added', function(snap) {    console.log('fetched post', snap.name(), snap.val(); }); 

or store posts user id (depending on use case how data fetched later):

/posts/$user_id/$post_id/...  new firebase('https://instance.firebaseio.com/posts/'+userid).on('child_added', function(snap) {    console.log('fetched post', snap.name(), snap.val()); }); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -