Firebase Reference in Peace (RIP)

David Christopher Johnson
2 min readMay 28, 2021

The first time I saw Firebase data it was weird to say the least. For so many years my mind had been conditioned by interacting with relational data and RDBM’s that the odd, jagged, tree-like structure before me made little sense:

{
"users": {
"alovelace": {
"name": "Ada Lovelace",
"contacts": { "ghopper": true },
},
"ghopper": { ... },
"eclarke": { ... }
}
}

This is a database, I asked?

My mind was unsettled, and a bit skeptical. Old habits die hard, and frankly, mine could not rest in peace.

That was until I discovered something called the Firebase database reference.

A FirebaseReference represents a specific location in your Database and can be used for reading or writing data to that Database location.

To read or write data from the database, you need an instance of firebase.database.Reference:

var database = firebase.database();

All Firebase Realtime Database data is stored as JSON objects. You can think of the database as a cloud-hosted JSON tree. Unlike a SQL database, there are no tables or records. When you add data to the JSON tree, it becomes a node in the existing JSON structure with an associated key. You can provide your own keys, such as user IDs or semantic names, or they can be auto-generated for you.

Building a properly structured database requires quite a bit of forethought. Most importantly, you need to plan for how data is going to be saved and later retrieved to make that process as easy as possible.

You can reference the root or child location in a Firebase Database by calling firebase.database().ref() or firebase.database().ref("child/path").

A reference is your starting node in this JSON tree. From here you can traverse to other nodes and data in your structure. You can also gather data in a “snapshot” or small collection of the larger JSON tree, and then manipulate this data, usually via a built-in method or your own callback function.

If we pop over to the Firebase documentation here’s what we find regarding the database reference:

DatabaseReference

public class DatabaseReference extends Query

A Firebase reference represents a particular location in your Database and can be used for reading or writing data to that Database location.

This class is the starting point for all Database operations. After you’ve initialized it with a URL, you can use it to read data, write data, and to create new DatabaseReferences.

And this is where the real fun begins. With a database reference you can read/write data at a particular path and (more powerfully) listen for changes to the data. This allows clients to be automatically updated when data on the backend changes — often in real-time!

Within a very short period of time working with Firebase, I began to see the benefits of this type of data architecture. Especially for mobile and web apps and situations where persistent information updates are needed.

But it all starts with the connection, with the database reference.

Becoming familiar with this new technology has allowed me to lay old habits to rest and finally say R.I.P. or Reference in Peace.

--

--