Creating an ActiveClass

Why use an ActiveClass?

A Fireactive ActiveClass comes with lots of convenient prototype and static methods to make it easy to create, update and sync data to and from your Firebase Realtime Database.

ActiveClass? ActiveDocument?

This documentation distinguishes between:

  1. An ActiveClass, which is an ES6 Class that extends ActiveClass(someSchema); and
  2. An ActiveDocument, which is instance of some ActiveClass.

ES6 Classes with ActiveClass

Extending using your Schema

To create an ActiveClass, you should declare an ES6 Class that extends ActiveClass(someSchema).

In the below example, we'll be using the user Schema defined previously.

import { ActiveClass } from 'fireactive'
import { userSchema } from '../wherever'
class User extends ActiveClass(userSchema) {
// optionally, add your own methods, e.g.
promote() {
this.role = 'admin'
}
}

Instantiating your ActiveClass

We can create an instance of our new User ActiveClass in the same way that we'd create an instance of a typical ES6 class, with the new operator:

const user = new User({ name: 'Richard', role: 'admin' })
user.name // => 'Richard'
user.age // => undefined
user.role // => 'admin'
user.isVerified // => false (default value from schema)

However, notice that using the new keyword like this means that:

  • the data has not yet been inserted into the database; and that
  • by default, therefore, there is no syncing to or from the database for this User.

We can confirm this by checking the _id property and using the default ActiveClass prototype .syncOpts method:

user._id // => undefined (but all users in the database will have one)
user.syncOpts() // => { fromDb: false, toDb: false }

Protip: remember to initialize

Trying to save our user to the database using the default ActiveClass prototype .save method will fail without initializing a connection to the Firebase Realtime Database:

await user.save()
/*
ActiveClassError: Failed to save User into database.
Could not connect to your Firebase database.
This might be because you have not initialized your Firebase app.
Try calling Fireactive.initialize
*/

So, let's try following the error and doing just that: initializing a connection via Fireactive.initialize!