Browse Source

Added the limit-children sample.

ryanpbrewster-patch-1
Nicolas Garnier 9 years ago
parent
commit
c8dc09f589
  1. 4
      README.md
  2. 43
      limit-children/README.md
  3. 49
      limit-children/index.js
  4. 6
      limit-children/package.json

4
README.md

@ -32,6 +32,10 @@ This can be useful to keep tack of the number of "likes" or "followers" of a som
Integrates the Google Translate API to perform automatic text translation across any number of languages. Language codes can be stored in Firebase for on the fly changes. Integrates the Google Translate API to perform automatic text translation across any number of languages. Language codes can be stored in Firebase for on the fly changes.
### [Limit number of child nodes](/limit-children)
Make sure to keep the number of child nodes below a certain threshold. For instance this can be useful to limit the number of lines of logs or chat history below a given number.
### [Full-text search via Algolia](/fulltext-search) TODO import from go/hearth-codesamples ### [Full-text search via Algolia](/fulltext-search) TODO import from go/hearth-codesamples
... ...

43
limit-children/README.md

@ -0,0 +1,43 @@
# Limit number of child nodes.
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
See file [index.js](index.js) for the code.
The dependencies are listed in [package.json](package.json).
## Sample Database Structure
As an example we'll be using a simple chat database structure:
```
/functions-project-12345
/chat
/key-123456
user: "Mat",
text: "Hey Bob!"
/key-123457
user: "Bob",
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 message is added.
```
"functions": {
".source": "functions",
"truncate": {
"triggers": {
"database": {
"path": "/chat/$messageid"
}
}
}
}
```

49
limit-children/index.js

@ -0,0 +1,49 @@
/**
* Copyright 2015 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'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'));
// Max number of lines of the chat history.
var 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', function(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;
}
});
// Update the parent. This effectiovely removes the extra children.
parentRef.update(updates, context.done);
} else {
context.done();
}
});
}
module.exports = {
truncate: truncate
};

6
limit-children/package.json

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