funky-firebase
v0.1.1
Published
Simple functional wrapper for Firebase.
Downloads
4
Readme
funky-firebase
Simple functional wrapper for Firebase.
Why? Because point-free composition > pointless method chaining :-)
All functions curried to taste (using the excellent ramda library)
Uses promises for async operations
Poorly documented and far from complete :-)
Core functions
Operations (asynchronous)
get(ref)
Resolves with value
update(ref, data)
set(ref, data)
push(ref, data)
transaction(ref, fn)
Examples
# Get and print value at ref
get(ref).then(console.log);
# Set value at ref
set(ref, {key: 'value'});
# Partially apply 'set' function
updateScore = set(scoreRef);
# Use partially applied 'set' function
updateScore(10)
References (synchronous)
child(ref, path)
Returns ref
url(ref)
Returns url string
Examples
# Get ref to grandchild of base reference
grandchild = child(baseRef, '/child/grandchild');
# Get absolute url and print to console
console.log(url(grandchild)); # e.g. 'https://test.firebaseio.com/child/grandchild'
Events (asynchronous/callback)
Functions take callback as second argument
onValue(ref, callback(value))
onChildAdded(ref, callback(value, [prevChildKey]))
onChildChanged(ref, callback(value, [prevChildKey]))
onChildMoved(ref, callback(value, [prevChildKey]))
onChildRemoved(ref, callback(value, [prevChildKey]))
# Print value at ref on change
onValue(ref, function(value) {
console.log(value)
});
Queries (synchronous)
To be documented
orderByKey(ref, key)
orderByValue(ref, value)
orderByChild(ref, path, value)
startAt(value, ref)
endAt(value, ref)
equalTo(value, ref)
limitToFirst(num, ref)
limitToLast(num, ref)
First order compositions
Mathematical operations (asynchronous)
Functions return promises that resolve when operation complete
add(ref, term)
subtract(ref, term)
multiply(ref, factor)
divide(ref, divisor)
Examples
add(ref, 10) # add 10 to value at ref
divide(ref, 10) # divide value at ref by 10
Queries (asynchronous)
Functions return promises that resolve with corresponding values
getByKey(ref, key)
getByValue(ref, value)
getByChild(ref, path, value)
Examples
### Get user by email address ###
# Get ref
users = child(ref, 'users');
# Set value at ref
set(users, [
{name: 'Thor', email: '[email protected]'},
{name: 'Odin', email: '[email protected]'}
}]);
# Partially apply 'getByChild' function
getUserByEmail = getByChild(users, 'email');
# Use partially applied 'getByChild' function
getUserByEmail('[email protected]')
.then(console.log)
# Result (approx.)
> {name: 'Odin', email: '[email protected]'}
Second order compositions
Mathematical operations
increment(ref)
Equivalent to add(ref, 1)
Resolves when operation complete
decrement(ref)
Equivalent to subtract(ref, 1)
Resolves when operation complete
Examples
increment(ref) # increment value at ref by 1