0

I have the following Firebase structure with:

A list of bakeries:

firebaseio.com/bakery/:id/stuffAboutTheBakery

And a list of bakers:

firebaseio.com/baker/:id/bakery/bakeryId

Bakers can provide bread to several bakeries so in the baker details I have an object that lists all the bakeries they supply using the bakery ID as a key and a value of true.

In my app I have a $firebaseObject with a baker. I want to get an array with the details of the bakeries they service. I can obviously just go to each end point but surely there is a better way. So my questions are:

  1. Is this data structure what the documentation means by flat structure? Or is there a better way to structure this?
  2. How do I query several specific bakeries into an array?

This is my current code:

self.baker = $firebaseObject(firebase.baker.child($stateParams.id));

self.baker.$loaded()
  .then(function() { 
    self.bakeries = [];
      for (var bakery in self.baker.bakeries) {
        self.bakeries.push($firebaseObject(firebase.bakery.child(bakery)));
      }
    })
    .catch(function(err) {
      console.error(err);
    });

1 Answer 1

1

Firebase team member here.

To answer your questions:

1 - This is exactly what the docs means by flat structure! What wouldn't be flat is if you put the bakery data underneath the baker. Separating the two is what we recommend.

2 - Since you have an object that stores the bakery ids for the baker you can get all the bakeries for that baker. You can synchronize that object and then for each of those bakeries you can create an array given it's id. Be careful though that you're not downloading too much data, as it will make your app slow.

I answered a similar question recently about clients and invoices. That should show you enough code to get your started.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much for your help. I actually saw the other article you wrote. My issue here is that I have a many to many relationship so I can't share IDs like you suggest. Can you kindly edit with how to get an object that is linked to several specific IDs. (I obviously know I can make a request for each and get individual $firebaseObjects.I just wonder if there is a better approach.
By many to many I mean there are bakeries can be linked to many bakers and a baker supplies many different bakeries.
I added my code that I think must be improvable! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.