ActiveClass

Terminology

In Fireactive, 'ActiveClass' can refer both to:

  1. an ES6 Class that is used to interact with a particular store of entities; and
  2. the library export, ActiveClass that is used in the creation of these ActiveClasses.

More concretely:

import { Schema, ActiveClass } from 'fireactive'
// define a schema for the ActiveClass
const animalSchema = {
name: Schema.string,
age: Schema.number
}
// create an ActiveClass
class Animal extends ActiveClass(animalSchema) {}

In the above code, Animal is an ActiveClass. It represents animal entities: JavaScript objects that have both a name property (as a string) and an age property (as a number).

It has been created by extending the ActiveClass export (passed a Schema).

API

The basic API is simply:

class MyClass extends ActiveClass(schema) {}

Parameters

Examples

Basic ActiveClass

A basic ActiveClass simply extends ActiveClass(schema) {}, without adding any further methods.

This will give it access to the default static and prototype methods.

import { Schema, ActiveClass, initialize } from 'fireactive'
const personSchema = {
firstName: Schema.string,
lastName: Schema.string,
age: Schema.number
}
class Person extends ActiveClass(personSchema) {}
// initialize a database connection
initialize({ databaseURL: 'https://your-project.firebase.io' })
const person = await Person.create({ firstName: 'Elizabeth', lastName: 'Windsor', age: 94 })
person.firstName // => 'Elizabeth'
person.lastName // => 'Windsor'
person.age // => 94
person._id // => will be a string generated by Firebase

Extending: prototype / instance methods

Since we are just creating an ES6 class, you can add your own prototype / instance methods or getters in the same way that you would with a typical ES6 class.

import { Schema, ActiveClass, initialize } from 'fireactive'
const personSchema = {
firstName: Schema.string,
lastName: Schema.string,
age: Schema.number
}
class Person extends ActiveClass(personSchema) {
get name() {
return `${this.firstName} ${this.lastName}`
}
about() {
return `Hello, my name is ${this.firstName}, and I'm ${this.age} years old!`
}
celebrateBirthday() {
this.age += 1
}
}
// initialize a database connection
initialize({ databaseURL: 'https://your-project.firebase.io' })
const person = await Person.create({ firstName: 'Elizabeth', lastName: 'Windsor', age: 94 })
person.name // => "Elizabeth Windsor"
person.about() // => "Hello, my name is Elizabeth, and I'm 94 years old!"
person.celebrateBirthday()
person.age // => 95

Extending: static methods

Since we are just creating an ES6 class, you can also add your own static / class methods that you would with a typical ES6 class.

import { Schema, ActiveClass, initialize } from 'fireactive'
const personSchema = {
firstName: Schema.string,
lastName: Schema.string,
age: Schema.number
}
class Person extends ActiveClass(personSchema) {
static newBirth(surname) {
return this.create({
firstName: 'Baby',
lastName: surname,
age: 0
})
}
}
// initialize a database connection
initialize({ databaseURL: 'https://your-project.firebase.io' })
const baby = await Person.newBirth('Simpson')
baby.firstName // => 'Baby'
baby.lastName // => 'Simpson'
baby.age // => 0