Connecting to Firebase

Why connect to Firebase?

By connecting your ActiveClasses to Firebase, you gain the power of default realtime sync to and from your Firebase Realtime Database for your ActiveClass and all its instances.

Basic operations with Firebase

Connecting with initialize

To connect your ActiveClasses to Firebase, call the initialize export from Fireactive with a config object that includes your Firebase Realtime Database URL:

import { initialize } from 'fireactive'
initialize({
// put your database url below
databaseURL: 'https://some-database-url.firebaseio.com'
}) // => returns Firebase app instance

Saving data with .save

Now you have made a connection to your Firebase Realtime Database, you can save a given ActiveDocument without throwing the error we saw before:

const richard = new User({ name: 'Richard', role: 'admin' })
richard._id // => undefined
await richard.save()
richard._id // => "-JhLeOlGIEjaIOFHR0xd" (or similar)
/*
When our User is saved into the Firebase
Realtime Database, it gains an `_id`
if it didn't already have one before.
*/

Inserting data with .create

Rather than using the new operator to instantiate an ActiveDocument and then separately calling .save, you can use .create to achieve both in one line:

/*
`.create` returns a promise that resolves when
the data has been written to the database
*/
const chuck = await User.create({ name: 'Chuck Norris', role: 'admin' })
/*
Our User has an `_id` because it is already
saved in the Firebase Realtime Database.
*/
chuck._id // => "-JhQ76OEK_848CkIFhAq" (or similar)

Fetching data with .find

You can read data from your database with the static .find method on an ActiveClass.

For example, we've created two different users who are both admins - let's retrieve them both.

// `.find` returns an array of matches
const admins = User.find({ role: 'admin' })
admins[0].name // => 'Richard'
admins[1].name // => 'Chuck Norris'

There are also static methods .findOne or .findById, which return a single ActiveDocument.

Realtime syncing to and from the database

By default, ActiveDocuments created through static ActiveClass methods (like .create, .find, .findOne...) sync realtime changes both to and from the Firebase Realtime Database.

Fireactive Realtime Syncing