I'm developing a mobile application in Angular. The home screen of the app is a contests page where the user chooses which of the two items they like better.
On initialization, this page gets calls backend code that retrieves the contest data from Cloud Firestore.
I have a solution that works already, which is: looping over all the contests that were created after a given date. But I'm concerned about efficiency here. I want 2 things...
- Minimize the data that the user is downloading (reduce lag)
- Minimize the number of queries and document reads (reduce cost)
Example of TypeScript that returns exactly what I'm looking for:
import * as firebase from 'firebase/app';
contestsRef = firebase.firestore().collection('Contests');
async getAllContestsForUser() {
const rightNow = new Date(Date.now());
const contests = new Array<Contest>();
await this.contestsRef
.where('closeDateTime', '>', rightNow.toISOString())
.get()
.then(querySnapshot => {
querySnapshot.forEach(contest => {
this.contestsRef
.doc(contest.id)
.collection('Voters')
.doc(this.userId)
.get()
.then(voter => {
if (!voter.exists) {
// this user did not vote on contest
contests.push({ ...contest.data(), id: contest.id });
}
});
});
});
return contests;
}
The results of this code is exactly what I want: an array of Contests that the user has not seen before, but I wonder if there is a better, more efficient way to do it using Firestore queries?