Last active
October 25, 2017 02:04
-
-
Save DennisAlund/e7501115baa09f3b8fa47b9c16af135c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export const countReviews = functions.firestore.document("movies/{movieId}/reviews/{reviewId}").onCreate(async (event) => { | |
| console.log(`Got a ${event.data.data().stars} star review`); | |
| const review = event.data.data(); | |
| const movieRef = admin.firestore().collection("movies").doc(event.params.movieId); | |
| const movie = (await movieRef.get()).data(); | |
| const newNumReviews = movie.numReviews + 1; | |
| const newTotalScore = movie.totalReviewScore + review.stars; | |
| console.log(`[${event.data.id}] Got a ${review.stars} star review from ${review.name} (now ${movie.numReviews} total reviews)`); | |
| return movieRef.update({ | |
| numReviews: newNumReviews, | |
| totalReviewScore: newTotalScore, | |
| averageScore: (newTotalScore/newNumReviews).toPrecision(3) | |
| }); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const numReviews = 20; | |
| const promises = []; | |
| for (let i = 0; i < numReviews; i++) { | |
| const numStars = Math.trunc(Math.random() * 5) + 1; | |
| console.log(`#${i} creating a ${numStars} star review.`); | |
| promises.push(admin.firestore().collection("movies").doc("tt0081748").collection("reviews").add({ | |
| name: `Jane Deer #${i}`, | |
| stars: numStars, | |
| comment: "Meh" | |
| })); | |
| } | |
| await Promise.all(promises); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment