File tree Expand file tree Collapse file tree 4 files changed +55
-4
lines changed
firestore/solution-aggregation/functions Expand file tree Collapse file tree 4 files changed +55
-4
lines changed Original file line number Diff line number Diff line change 1
1
language : node_js
2
2
node_js :
3
3
- " 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
6
4
install :
7
5
- npm install -g lerna
8
6
- npm install -g eslint
9
7
- lerna bootstrap
10
8
script :
11
- - ./scripts/test.sh
9
+ - ./scripts/test.sh
Original file line number Diff line number Diff line change
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]
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -25,6 +25,9 @@ find . -type f -name "*.js" -not -path "*node_modules*" \
25
25
if [ " $TRAVIS_SECURE_ENV_VARS " = false ]; then
26
26
echo " Could not find secure environment variables, skipping integration tests."
27
27
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
+
28
31
# Run all tests
29
32
lerna run test
30
- fi
33
+ fi
You can’t perform that action at this time.
0 commit comments