Notes
Realtime Database
See the Realtime Database docs for web.
Set a ref
function writeUserData(userId, name, email, imageUrl) {
firebase
.database()
.ref('users/' + userId)
.set({
username: name,
email: email,
profile_picture: imageUrl,
});
}Value events
Value events fire with the entire data payload for any and all changes
Listen to ongoing events
var starCountRef = firebase.database().ref('posts/' + postId + '/starCount');
starCountRef.on('value', function(snapshot) {
updateStarCount(postElement, snapshot.val());
});Listen to a single event and stop listening
Multi-path updates
Delete data
Detach listener
Transactions
Child events
child_added: fires once for every existing result and then again for every new result; does not fire for changes or removals, only new records
child_changed: fires when the underlying object or value is changed in any way
child_removed: fires when the entire record is removed
Sort data
orderByChild('childName'): Orders by a child attribute
orderByKey(): Orders by record keys
orderByValue(): Orders by record values; only relevant when values are strings or numbers and not nested objects
Filter data
Assumes that data is ordered by key unless otherwise specified
limitToFirst(count): Sets the maximum number of items to return from the beginning of the ordered list of results.
limitToLast(count): Sets the maximum number of items to return from the end of the ordered list of results.
startAt(value): Return items greater than or equal to the specified key or value, depending on the order-by method chosen.
endAt(value): Return items less than or equal to the specified key or value, depending on the order-by method chosen.
equalTo(value): Return items equal to the specified key or value, depending on the order-by method chosen.
Authenticate Node.js
Full admin privileges
Initialize Node.js with limited privileges
Set auth token variables to limit access
Act as an un-authenticated user
Last updated