Skip to content

Commit 07de8a0

Browse files
author
Nicolas Garnier
committed
Updating the child-count Functions sample.
Change-Id: Idc7a3b8732b0aae201271715498d5e73efe35579
1 parent 1f97cfa commit 07de8a0

File tree

4 files changed

+17
-43
lines changed

4 files changed

+17
-43
lines changed

child-count/README.md

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
# Tracking the number of elements in a list
22

3-
This template shows how to keep track of the number of elements in a Firebase Database list. This can be useful to keep tack of the number of "likes" or "followers" of a somethings shares through social media.
3+
This template shows how to keep track of the number of elements in a Firebase Database list. For instance this can be useful to keep tack of the number of "likes" or "followers" of something shared on a social platform.
44

5-
## Cloud Function Code
5+
## Functions Code
66

7-
See file [index.js](index.js) for the code.
7+
See file [index.js](functions/index.js) for the code.
88

99
This is done by simply updating a `likes_count` attribute on the parent of the list node which is tracked.
1010

1111
The dependencies are listed in [package.json](package.json).
1212

1313
## Sample Database Structure
1414

15-
As an example we'll be using the database structure shown below. It keeps tracks of the list of users who liked an image and the count of these likes:
15+
As an example we'll be using the database structure shown below. It keeps tracks of the list of users who liked a post and the count of these likes:
1616

1717
```
1818
/functions-project-12345
19-
/images
19+
/posts
2020
/key-123456
2121
likes_count: 32
2222
/likes
@@ -25,22 +25,3 @@ As an example we'll be using the database structure shown below. It keeps tracks
2525
user786245: true
2626
...
2727
```
28-
29-
## Trigger rules
30-
31-
Below is the trigger rule for the `countlikes` function making sure it's triggered when a new like is added/removed.
32-
33-
```
34-
"functions": {
35-
".source": "functions",
36-
"countlikes": {
37-
"triggers": {
38-
"database": {
39-
"path": "/images/$image/likes",
40-
}
41-
}
42-
}
43-
}
44-
```
45-
46-
Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2015 Google Inc. All Rights Reserved.
2+
* Copyright 2016 Google Inc. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,17 +15,9 @@
1515
*/
1616
'use strict';
1717

18-
var Firebase = require('firebase');
19-
var env = require('./env');
20-
var ref = new Firebase(env.get('firebase.database.url'), 'admin');
21-
ref.auth(env.get('firebase.database.token'));
18+
var functions = require('firebase-functions');
2219

2320
// Keeps track of the length of the 'likes' child list in a separate attribute.
24-
exports.countlikes = function(context, data) {
25-
var nodeRef = ref.child(data.path);
26-
console.log('Authenticated successfully with admin rights');
27-
nodeRef.once('value').then(function(likesData) {
28-
nodeRef.parent().child('likes_count').set(likesData.numChildren())
29-
.then(context.done).catch(context.done);
30-
}).catch(context.done);
31-
};
21+
exports.countlikes = functions.database().path('/posts/$postid/likes').on('value', event => {
22+
return event.adminRef.parent().child('likes_count').set(event.data.numChildren());
23+
});

child-count/functions/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"main": "index.js",
3+
"dependencies": {
4+
"firebase": "^3.3.0",
5+
"firebase-functions": "https://storage.googleapis.com/firebase-preview-drop/node/firebase-functions/firebase-functions-preview.latest.tar.gz"
6+
}
7+
}

child-count/package.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)