react-firehooks icon indicating copy to clipboard operation
react-firehooks copied to clipboard

aggregateSpec use unclear

Open marekves opened this issue 1 year ago • 1 comments

Hello could you please provide a clear explanation how aggregateSpec in useAggregateFromServer is to be defined? Its currently very unclear what the input is. Thx for the and help and the lib!

marekves avatar Apr 30 '24 08:04 marekves

Hey 👋

the best documentation is probably from firebase: https://firebase.google.com/docs/firestore/query-data/aggregation-queries#calculate_multiple_aggregations_in_a_query

The argument list of firestore's getAggregateFromServer and react-firehooks' useAggregateFromServer are identical.

// Example from firestore docs
const coll = collection(firestore, 'cities');
const snapshot = await getAggregateFromServer(coll, {
  countOfDocs: count(),
  totalPopulation: sum('population'),
  averagePopulation: average('population')
});

console.log('countOfDocs: ', snapshot.data().countOfDocs);
console.log('totalPopulation: ', snapshot.data().totalPopulation);
console.log('averagePopulation: ', snapshot.data().averagePopulation);
// Usage with react-firehooks
const coll = collection(firestore, 'cities');
const [data] = useAggregateFromServer(coll, {
  countOfDocs: count(),
  totalPopulation: sum('population'),
  averagePopulation: average('population')
});

console.log('countOfDocs: ', data?.countOfDocs);
console.log('totalPopulation: ', data?.totalPopulation);
console.log('averagePopulation: ', data?.averagePopulation);

andipaetzold avatar Apr 30 '24 16:04 andipaetzold

Thank you for the clarification.

marekves avatar May 25 '24 18:05 marekves