Indexed fields

Schema.indexed

The typical use case for a Schema.indexed field is to model one-to-many relations.

A field which is typed using Schema.indexed[type] structures an object which has arbitrary keys, each of which has a value that conform to type, e.g.:

  • Schema.indexed.boolean structures an object with boolean values
  • Schema.indexed.enum structures an object with values from a set
  • Schema.indexed.number structures an object with number values
  • Schema.indexed.string structures an object with string values
  • Schema.indexed.true structures an object where every value is true

Creation

import { ActiveClass, Schema } from 'fireactive'
const schema = {
booleanVals: Schema.indexed.boolean,
fooOrBarVals: Schema.indexed.enum(['foo', 'bar']),
numberVals: Schema.indexed.number,
stringVals: Schema.indexed.string,
trueVals: Schema.indexed.true
}
class Dictionary extends ActiveClass(schema) {}
new Dictionary({ booleanVals: { test: 'false' } }) // ActiveClassError: Could not construct Dictionary. The property 'booleanVals' is of the wrong type
new Dictionary({ booleanVals: { test: false } }) // works
new Dictionary({ fooOrBarVals: { test: 'foobar' } }) // ActiveClassError: Could not construct Dictionary. The property 'fooOrBarVals' is of the wrong type
new Dictionary({ fooOrBarVals: { test: 'foo' } }) // works
new Dictionary({ numberVals: { test: '5' } }) // ActiveClassError: Could not construct Dictionary. The property 'numberVals' is of the wrong type
new Dictionary({ numberVals: { test: 5 } }) // works
new Dictionary({ stringVals: { test: 5 } }) // ActiveClassError: Could not construct Dictionary. The property 'stringVals' is of the wrong type
new Dictionary({ stringVals: { test: '5' } }) // works
new Dictionary({ trueVals: { test: false } }) // ActiveClassError: Could not construct Dictionary. The property 'trueVals' is of the wrong type
new Dictionary({ trueVals: { test: true } }) // works

Assignment

import { ActiveClass, Schema } from 'fireactive'
const schema = {
booleanVals: Schema.indexed.boolean,
fooOrBarVals: Schema.indexed.enum(['foo', 'bar']),
numberVals: Schema.indexed.number,
stringVals: Schema.indexed.string,
trueVals: Schema.indexed.true
}
class Dictionary extends ActiveClass(schema) {}
const dictionary = new Dictionary({})
dictionary.booleanVals // => {}
dictionary.fooOrBarVals // => {}
dictionary.numberVals // => {}
dictionary.stringVals // => {}
dictionary.trueVals // => {}
dictionary.booleanVals.someProp = 'false' // ActiveClassError: Dictionary could not accept the value "false" (string) at path 'booleanVals.someProp'. The property 'booleanVals' is of the wrong type
dictionary.booleanVals.someProp = false // works
dictionary.fooOrBarVals.someProp = 'BAR' // ActiveClassError: Dictionary could not accept the value "BAR" (string) at path 'fooOrBarVals.someProp'. The property 'fooOrBarVals' is of the wrong type
dictionary.fooOrBarVals.someProp = 'bar' // works
dictionary.numberVals.someProp = '5' // ActiveClassError: Dictionary could not accept the value "5" (string) at path 'numberVals.someProp'. The property 'numberVals' is of the wrong type
dictionary.numberVals.someProp = 5 // works
dictionary.stringVals.someProp = 5 // ActiveClassError: Dictionary could not accept the value 5 (number) at path 'stringVals.someProp'. The property 'stringVals' is of the wrong type
dictionary.stringVals.someProp = '5' // works
dictionary.trueVals.someProp = false // ActiveClassError: Dictionary could not accept the value false (boolean) at path 'trueVals.someProp'. The property 'trueVals' is of the wrong type
dictionary.trueVals.someProp = true // works