Browse Source

Update the limit-children Functions sample.

Change-Id: I7e80739735fb33c479d19bfd471c87b3e07eb3ba
ryanpbrewster-patch-1
Nicolas Garnier 8 years ago
parent
commit
b2f5fd8152
  1. 25
      limit-children/README.md
  2. 35
      limit-children/functions/index.js
  3. 7
      limit-children/functions/package.json
  4. 6
      limit-children/package.json

25
limit-children/README.md

@ -2,11 +2,11 @@
This template shows how to keep the number of child nodes in a Firebase database below a given number. This can be used to limit the number of lines of a chat history or logs.
## Cloud Function Code
## Functions Code
See file [index.js](index.js) for the code.
See file [index.js](function/index.js) for the code.
The dependencies are listed in [package.json](package.json).
The dependencies are listed in [package.json](function/package.json).
## Sample Database Structure
@ -23,21 +23,4 @@ As an example we'll be using a simple chat database structure:
text: "Hey Mat! What's Up?"
```
## Trigger rules
Below is the trigger rule for the `truncate` function making sure it's triggered when a new chat message is added.
```
"functions": {
".source": "functions",
"truncate": {
"triggers": {
"database": {
"path": "/chat/$messageid"
}
}
}
}
```
Every time a new chat message is added the Function runs. It counts the number of chat messages and removes the old ones if there are too many.

35
limit-children/index.js → limit-children/functions/index.js

@ -1,5 +1,5 @@
/**
* Copyright 2015 Google Inc. All Rights Reserved.
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -15,35 +15,26 @@
*/
'use strict';
// Create an all access Firebase Database reference.
var Firebase = require('firebase');
var env = require('./env');
var ref = new Firebase(env.get('firebase.database.url'), 'admin');
ref.auth(env.get('firebase.database.token'));
const functions = require('firebase-functions');
// Max number of lines of the chat history.
var MAX_LOG_COUNT = 5;
const MAX_LOG_COUNT = 5;
// Removes siblings of the node that element that triggered the function if there are more than MAX_LOG_COUNT
function truncate(context, data) {
var parentRef = ref.child(data.path).parent();
parentRef.once('value').then(function(snapshot) {
// Removes siblings of the node that element that triggered the function if there are more than MAX_LOG_COUNT.
// In this example we'll keep the max number of chat message history to MAX_LOG_COUNT.
exports.truncate = functions.database().path('/chat/$messageid').on('value', event => {
var parentRef = event.data.ref.parent();
return parentRef.once('value').then(snapshot => {
if (snapshot.numChildren() > MAX_LOG_COUNT) {
var childCount = 0;
var updates = {};
snapshot.forEach(function(child) {
if (++childCount < snapshot.numChildren() - MAX_LOG_COUNT) {
updates[child.key()] = null;
updates[child.key] = null;
}
});
// Update the parent. This effectiovely removes the extra children.
parentRef.update(updates).then(context.done);
} else {
context.done();
// Update the parent. This effectively removes the extra children.
return parentRef.update(updates);
}
}).catch(context.done);
}
module.exports = {
truncate: truncate
};
});
});

7
limit-children/functions/package.json

@ -0,0 +1,7 @@
{
"main": "index.js",
"dependencies": {
"firebase": "^3.3.0",
"firebase-functions": "https://storage.googleapis.com/firebase-preview-drop/node/firebase-functions/firebase-functions-preview.latest.tar.gz"
}
}

6
limit-children/package.json

@ -1,6 +0,0 @@
{
"main": "index.js",
"dependencies": {
"firebase": "^2.4.0"
}
}
Loading…
Cancel
Save