Softhinker.com
Softhinker.com

Reputation: 895

Angularfire2: how to do group by

I'm using Firebase to store the data, and want to display statistics information. As it seems Firebase doesn't support functions like groupBy, may I have advices on how to achieve it? Should it be done on client side?

Upvotes: 0

Views: 576

Answers (3)

user663031
user663031

Reputation:

You have three basic options, two of which were mentioned in another answer.

  1. Do the computation locally.
  2. Each time the underlying data changes, redo the calculation locally, and store the aggregated/grouped data somewhere in the database, retrieving it as necessary.
  3. Write a Firebase function which watches the data which may change, does the calculation, and stores the result into the database somewhere. This could be the best solution if you are dealing with a lot of data.

Upvotes: 1

Bergur
Bergur

Reputation: 4067

You can use $extend to create new features that use the base classes, so you can transform your data.

They even have an example of creating a getTotal function at: https://github.com/firebase/angularfire/blob/master/docs/guide/extending-services.md#extending-firebasearray

The example

app.factory("ListWithTotal", ["$firebaseArray",
  function($firebaseArray) {
    // create a new service based on $firebaseArray
    var ListWithTotal = $firebaseArray.$extend({
      getTotal: function() {
        var total = 0;
        // the array data is located in this.$list
        angular.forEach(this.$list, function(rec) {
          total += rec.amount;
        });
        return total;
      }
    });

    return function(listRef) {
      // create an instance of ListWithTotal (the new operator is required)
      return new ListWithTotal(listRef);
    }
  }
]);

Upvotes: 0

Ploppy
Ploppy

Reputation: 15363

Either do it client-side or if possible, store the total value in the db.

Upvotes: 0

Related Questions