Nicolas Garnier
9 years ago
4 changed files with 102 additions and 0 deletions
@ -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" |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
``` |
||||
|
|
||||
|
|
@ -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 |
||||
|
}; |
@ -0,0 +1,6 @@ |
|||||
|
{ |
||||
|
"main": "index.js", |
||||
|
"dependencies": { |
||||
|
"firebase": "^2.4.0" |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue