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:
- JavaScript
- TypeScript
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:
- JavaScript
- TypeScript
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:
- JavaScript
- TypeScript
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.
- JavaScript
- TypeScript
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.
