Skip to content

Instantly share code, notes, and snippets.

@katowulf
katowulf / example_rules.js
Created February 10, 2016 00:25
A list of example patterns for security rules validations
{
"rules": {
// REQUIRED FIELDS
"example1" : {
"$record": {
// foo and bar are required fields that must always exist
// this will reject writes to $record, as well as directly to $record/foo or $record/bar if they are null
".validate": "newData.hasChildren(['foo', 'bar'])"
}
@katowulf
katowulf / archive_data.js
Created January 4, 2016 20:06
Archive data in Firebase 1.x
var Firebase = require('firebase');
var rootRef = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/");
var archiveRef = rootRef.child('path/to/archive');
var dataRef = rootRef.child('path/to/data');
var archiveTimestamp = Date.now() - 3600; // everything older than an hour
dataRef.orderByChild('<date field>').endAt(archiveTimestamp).once('value', function(snap) {
snap.forEach(function(childSnap) {
archiveRef.child( childSnap.key() ).set( childSnap.val(), function(err) {
@katowulf
katowulf / FirebaseQueryWrapper.js
Created July 31, 2015 20:10
Wrap a Firebase Query to include a toString with the parameters.
// Not all API methods are implemented in this proof
// See https://www.firebase.com/docs/web/api/query/
var FirebaseQueryWrapper = (function() {
function FirebaseQueryWrapper(url) {
this.ref = new Firebase(url);
this.parms = {};
}
var proto = FirebaseQueryWrapper.prototype;
@katowulf
katowulf / fetch_manually.js
Created May 12, 2015 19:50
Firebase: Join an index of companies a user belongs to the data for the companies and display the results
var fb = new Firebase('https://<INSTANCE>.firebaseio.com');
var idxRef = fb.child('users/kato/companies');
var dataRef = fb.child('companies');
idxRef.on('child_added', function(userSnap) {
dataRef.child(userSnap.key()).once('value', function(snap) {
console.log('user ' + userSnap.key() + ' belongs to company ' + snap.key());
});
});
@katowulf
katowulf / filter_using_extend.js
Last active November 7, 2019 21:10
Filter records loaded into AngularFire based on some criteria.
// this will be much more efficient than $watch()
app.factory('FilteredArray', function($firebaseArray) {
function FilteredArray(ref, filterFn) {
this.filterFn = filterFn;
return $firebaseArray.call(this, ref);
}
FilteredArray.prototype.$$added = function(snap) {
var rec = $firebaseArray.prototype.$$added.call(this, snap);
if( !this.filterFn || this.filterFn(rec) ) {
return rec;
@katowulf
katowulf / rules.json
Last active October 31, 2017 16:41
Validating client-only auction bids in Firebase using security rules and transactions.
{
"auctions": {
"$auction_id": {
"current_bid": {
".validate": "newData.hasChildren(['uid', 'amount'])",
"uid": {
".validate": "newData.val() === auth.uid"
},
"amount": {
".validate": "newData.isNumber() && newData.val() > data.val() && newData.val() === root.child('auctions/'+$auction_id+'/bids/'+auth.uid+'/amount').val()"
@katowulf
katowulf / NestedPathMonitor.js
Last active August 29, 2015 14:18
Watching child of child in Flashlight for Firebase.
var DynamicPathMonitor = require('./DynamicPathMonitor');
// call this instead of new PathMonitor inside PathMonitor.process
function NestedPathMonitor(ref, factory) {
this.factory = factory;
this.paths = {};
ref.on('child_added', this._add, this);
ref.on('child_removed', this._remove, this);
}
@katowulf
katowulf / app.js
Last active February 24, 2022 06:51
Simple paginate example in AngularFire with Firebase.util (http://firebase.github.io/firebase-util/)
var app = angular.module('app', ['firebase']);
app.controller('ctrl', function($scope, $pageArray) {
$scope.pageItems = $pageArray(ref, 'number');
});
app.factory('$pageArray', function($firebaseArray) {
return function(ref, field) {
// create a Paginate reference
var pageRef = new Firebase.util.Paginate(ref, field, {maxCacheSize: 250});
@katowulf
katowulf / app.js
Last active September 15, 2017 01:22
Simple infinite scroll example in AngularFire with Firebase.util (http://firebase.github.io/firebase-util/)
var app = angular.module('app', ['firebase']);
app.controller('ctrl', function($scope) {
var ref = new Firebase('https://fbutil.firebaseio.com/paginate');
$scope.scrollItems = $scrollArray(ref, 'number');
});
app.factory('$scrollArray', function($firebaseArray) {
return function(ref, field) {
// create a special scroll ref
@katowulf
katowulf / app.js
Created March 21, 2015 15:34
Example of infinite scroll using ui-grid and Firebase (uses http://firebase.github.io/firebase-util/)
var app = angular.module('app', ['firebase', 'ui.grid', 'ui.grid.infiniteScroll']);
app.controller('ctrl', function($scope, $firebaseArray) {
var baseRef = new Firebase('https://fbutil.firebaseio.com/paginate');
var scrollRef = new Firebase.util.Scroll(baseRef, 'number');
//$scope.data = $firebaseArray(scrollRef);
$scope.opts = {
columnDefs: [
{name: '$id', displayName: 'ID'},