1

I would like some explanations about data retrieving in firebase. I'm not an expert in NoSql data structure and something is missing in my mind. It's not natural for me. However, I think i've understood the basics of Two-way relationships in this kind of structure.

Here is my data structure :

{
// Two-way relationships between accounts & logements
  "accounts" : {
    "uniqueId1" : {
      "nom" : 'My name 1',
      "email" : 'My email 1',
      "logements" : {
          uniqueIdLogement1 : true,
          // do I have to list relationships with images & geofire here ?
          uniqueIdLogement2 : true
      },
      "favorites" : {
          uniqueIdLogement2 : true,
          uniqueIdLogement25 : true,
          uniqueIdLogement32 : true
      }

    },
    ....
  },
  // Each logement has his own "images" & "geofire" data
  "logements" : {
    "uniqueIdLogement1" : {
      "nom_du_logement" : 'My logement name 1',
      "accounts" : {
          uniqueId1 : true
      },
      "images" : {
          uniqueIdImages1 : true,
          uniqueIdImages2 : true,
          uniqueIdImages3 : true,
      },
      "geofire" : {
          uniqueIdGeofire1 : true
      },
    },
    ...
  },
  "images" : {
    "uniqueIdImages1" : {
      "image" : 'My image URL 1',
      "logements" : {
          uniqueIdLogement1 : true
      }
    },
    ...
  },
  "geofire" : {
    "uniqueIdGeofire1" : {
      "g" : 'My geofire Data,
      "l" : {
          0 : '-44.34',
          1 : '-3.2'
      },
      "logements" : {
          uniqueIdLogement1 : true
      }
    },
    ...
  }
}

I think everyone has his own point of view about data structure but in my opinion (in reference of Firebase Docs) it has to be quite like that. But if you see some updates, don't hesitate !

So, In angularJs, i would like to list each "logements" for account with "uniqueId1" for exemple and display their own "images" & geofire data (and check if they are my user's favorites logements).
Is it possible to do that ?

// list every logements for account ID1
    // for each logement take images data & geofire data & check if favorites
        // push infos in $scope.items & display with ng-repeat

Another question relative to that : When i remove a "logements" for User ID1, i want to remove also all images & geofire references ... ! Is it possible to do that too ?

// remove uniqueIdLogement1 from account ID 1
   // remove images where "logements" = uniqueIdLogement1
   // remove goofier where "logements" = uniqueIdLogement1

I think that if i understand that correctly, it will be okay for me ! I just can't see right now how it works and it's frustrating because i know there is a lot of potential with this kind of database. Can you explain me please some details about that . Thank you very much

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Pablo DelaNoche
  • 677
  • 1
  • 9
  • 28

1 Answers1

2

With any database you'll often need to join data from multiple locations to build your view.

In relational databases, you can get the data from these multiple locations (tables in that case) with a single statement, by using a JOIN clause.

In Firebase (and many other NoSQL databases) there is no built-in way to join data from multiple locations. So you will have to do that in your code.

var ref = firebase.database().ref();
var accountRef = ref.child('accounts/uniqueId1');
accountRef.on('value', function(accountSnapshot) {
  var logementCount = accountSnapshot.child('logements').numChildren;
  var logementLoadedCount = 0;
  accountSnapshot.child('logements').forEach(function(logementKey) {
    var logementRef = ref.child('logements').child(logementKey.key);
    logementRef.once('value', function(logementSnapshot) {
      var logement = logementSnapshot.val();
      logementLoadedCount = logementLoadedCount + 1;
      if (logementLoadedCount == logementCount) {
        console.log('We've loaded all logements');
      }
    });
  });
});

Many developers from a SQL and traditional web development background worry about the performance of all those nested calls. In Firebase that is not needed (for reasonable amounts of data), since Firebase pipelines the requests over a single connection. See Speed up fetching posts for my social network app by using query instead of observing a single event repeatedly.

In general I highly recommend reading this article on NoSQL data modeling for a good introduction to the general topic.

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807