Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Firebase Database API Cheatsheet
There is no way to store an empty object/array/null value.
There are also no actual arrays. Array values get stored as objects with integer keys.
(If all keys are integers, it will be returned as an array.)
Basically, it's one giant tree of hashes with string keys.
Simply write a value to any location, and the intermediary locations will automatically come into existance.
── Classes ──
DataSnapshot : Container for a subtree of data at a particular location.
The `ref` property stores a Reference to the DataSnapshot's location.
Reference : Represent a location. Returned by: db.ref(path), ref.parent, ref.child(path)
Inherits query and read methods from Query, then adds write methods.
Query : Query objects have query (sorting/filtering) methods and read methods.
Query objects are returned when calling a query method on a Reference.
The `ref` property stores a Reference to the Query's location.
You read from or write to a location using a Reference.
You begin a query at a location using a Reference's query methods, then read from it using the Query's read methods.
Read methods are callback-based, and pass DataSnapshots to the callback.
── Write Methods ──
push ( ) Returns a Reference to a child location with an auto-generated key.
push ( val, cb ) Like `push()`, but also writes val to child location.
remove ( cb ) Remove location.
set ( val, cb ) Writes val to location. Writing null == calling remove().
update ( {vals}, cb ) Writes vals to their respective relative locations. Writing null == calling remove().
── Query Methods ──
orderByChild (key) Order children at location by the value of specified grandchild key.
orderByKey () Order children at location by child keys.
orderByValue () Order children at location by child values.
limitToFirst (num) Max number of children to return from the beginning. (Based on orderBy*.)
limitToLast (num) Max number of children to return from the end. (Based on orderBy*.)
startAt (val, key) Return children who's val (and key, optionally) are >= the specified val (and key, optionally).
endAt (val, key) Return children who's val (and key, optionally) are <= the specified val (and key, optionally).
equalTo (val) Return children who's val is == the specified val.
── Read Methods ──
on ( event, cb ) Invokes callback every time the specified event happens at location.
once ( event, cb ) Like `on`, but only invokes the callback the first time the event happens.
── Events ──
child_added Triggered once for each existing child, then when a new child is added.
child_changed Triggered any time a child (including descendants) is modified.
child_moved See docs.
child_removed Triggered when an immediate child is removed.
value Triggered once when listener attached, then every time the value (including children) changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment