Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aggregateSpec use unclear #277

Closed
marekves opened this issue Apr 30, 2024 · 2 comments
Closed

aggregateSpec use unclear #277

marekves opened this issue Apr 30, 2024 · 2 comments

Comments

@marekves
Copy link

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!

@andipaetzold
Copy link
Owner

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);

@marekves
Copy link
Author

Thank you for the clarification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants