Skip to content

Commit f979ed0

Browse files
authored
Add aggregation function (#15)
2 parents a2a7b67 + 4c183f7 commit f979ed0

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

.travis.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
language: node_js
22
node_js:
33
- "7"
4-
before_install:
5-
- openssl aes-256-cbc -K $encrypted_001d217edcb2_key -iv $encrypted_001d217edcb2_iv -in service-account.json.enc -out service-account.json -d
64
install:
75
- npm install -g lerna
86
- npm install -g eslint
97
- lerna bootstrap
108
script:
11-
- ./scripts/test.sh
9+
- ./scripts/test.sh
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const functions = require('firebase-functions');
2+
const admin = require('firebase-admin');
3+
4+
const db = admin.firestore();
5+
6+
// [START aggregate_function]
7+
exports.aggregateRatings = functions.firestore
8+
.document('restaurants/{restId}/ratings/{ratingId}')
9+
.onWrite((change, context) => {
10+
// Get value of the newly added rating
11+
var ratingVal = change.after.data().rating;
12+
13+
// Get a reference to the restaurant
14+
var restRef = db.collection('restaurants').document(context.params.restId);
15+
16+
// Update aggregations in a transaction
17+
return db.runTransaction(transaction => {
18+
return transaction.get(restRef).then(restDoc => {
19+
// Compute new number of ratings
20+
var newNumRatings = restDoc.data('numRatings') + 1;
21+
22+
// Compute new average rating
23+
var oldRatingTotal = restDoc.data('avgRating') * restDoc.data('numRatings');
24+
var newAvgRating = (oldRatingTotal + ratingVal) / newNumRatings;
25+
26+
// Update restaurant info
27+
return transaction.update(restRef, {
28+
avgRating: newAvgRating,
29+
numRatings: newNumRatings
30+
});
31+
});
32+
});
33+
});
34+
// [END aggregate_function]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "solution-aggregation",
3+
"description": "Cloud Functions for Firebase",
4+
"scripts": {
5+
"serve": "firebase serve --only functions",
6+
"shell": "firebase experimental:functions:shell",
7+
"start": "npm run shell",
8+
"deploy": "firebase deploy --only functions",
9+
"logs": "firebase functions:log"
10+
},
11+
"dependencies": {
12+
"firebase-admin": "~5.8.1",
13+
"firebase-functions": "^1.0.1"
14+
},
15+
"private": true
16+
}

scripts/test.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ find . -type f -name "*.js" -not -path "*node_modules*" \
2525
if [ "$TRAVIS_SECURE_ENV_VARS" = false ]; then
2626
echo "Could not find secure environment variables, skipping integration tests."
2727
else
28+
# Decode secure stuff
29+
openssl aes-256-cbc -K $encrypted_001d217edcb2_key -iv $encrypted_001d217edcb2_iv -in service-account.json.enc -out service-account.json -d
30+
2831
# Run all tests
2932
lerna run test
30-
fi
33+
fi

0 commit comments

Comments
 (0)