Schema

Schema is one of the main exports from Fireactive.

You should use Schema to provide a predictable structure to your ActiveClasses.

Schemas are used by ActiveClasses to:

  • provide runtime type-checking on instances; and
  • provide static type-checking in TypeScript (without additional type declarations).

Examples

Basic usage

Let's suppose we're storing user data.

For the sake of argument, let's suppose that users all:

  • have a name, which should be a string;
  • have an age, which should be a number;
  • have a role, which must be either the string 'admin' or 'basic'
    • (i.e. as a type it should satisfy an enum of the two);
  • have a verified status, which should be a boolean.
import { Schema, ActiveClass } from 'fireactive'
const userSchema = {
name: Schema.string,
age: Schema.number,
role: Schema.enum(['admin', 'basic']),
isVerified: Schema.boolean
}
class User extends ActiveClass(userSchema) {}
new User({
name: 'Joe Bloggs',
age: 42
})
// ActiveClassError: Could not construct User. The required property 'role' is missing
new User({
name: 'Joe Bloggs',
age: 42,
role: 'elite hacker',
isVerified: 'true'
})
// ActiveClassError: Could not construct User. The property 'role' is of the wrong type
new User({
name: 'Joe Bloggs',
age: 42,
role: 'admin',
isVerified: true
})
// works

Simple configuration

Schema fields are required (and therefore must always exist on a related ActiveDocument) unless configured otherwise.

Suppose we want to tweak our schema in the following way therefore:

  • age should be optional;
  • role should default to 'basic' on initialization; and
  • isVerified should be optional but also default to false on initialization.
import { Schema, ActiveClass } from 'fireactive'
const userSchema = {
name: Schema.string,
age: Schema.number({ optional: true }),
role: Schema.enum(['admin', 'basic'], { default: 'basic' }),
isVerified: Schema.boolean({ required: false, default: false })
}
class User extends ActiveClass(userSchema) {}
new User({}) // ActiveClassError: Could not construct User. The required property 'name' is missing
const user = new User({ name: 'Richard' }) // works
user.name // => 'Richard'
user.age // => undefined
user.role // => 'basic'
user.isVerified // => false

Note that optional: true and required: false achieve the same thing, so you can use whichever you prefer the semantics of.

Available types