react-firehooks
react-firehooks copied to clipboard
aggregateSpec use unclear
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!
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);
Thank you for the clarification.