A data storage and manipulation library for javascript
Data models with automatic schema enforcement.
- Model
- new Model(schema)
- instance
- .schema ⇒
Schema
- .apply(object) ⇒
object
- .extend(model) ⇒
Model
- .errorLevel(errorLevel) ⇒
MODEL_ERROR_LEVEL
- .onChange(callback) ⇒
Queue
- .onError(callback) ⇒
Queue
- .schema ⇒
- static
Once the Model is instantiated the schema can't be changed.
The Model class uses the on-change library (uses the
Proxy
API) to detect changes and enforce the schema.
Param | Type |
---|---|
schema | Schema |
Example
import { Model } from 'hord';
const Person = new Model({
first: String,
last: String,
age: 'integer',
hobbies: {
type: Array,
content: String
}
});
const johnDoe = Person.apply({
first: 'John',
last: 'Doe',
age: 21
});
johnDoe.hobbies = ['programming', 10];
console.log(johnDoe);
// => { first: 'John', last: 'Doe', age: 21, hobbies: ['programming'] }
Get the schema for this model.
Apply this model to an object.
Param | Type | Description |
---|---|---|
object | object |
The object to apply this model to. |
model.extend(model) ⇒ Model
Returns a new Model with a new extended Schema. Retains the errorLevel from the calling Model.
Param | Type | Description |
---|---|---|
model | Model , Schema , Schema.SchemaDefinition |
The model to superimpose on this one. |
model.errorLevel(errorLevel) ⇒ MODEL_ERROR_LEVEL
🔗 Chainable
How to handle errors on this model. Overrides Model.defaultErrorLevel()
Default: MODEL_ERROR_LEVEL.UNSET
Param | Type |
---|---|
errorLevel | MODEL_ERROR_LEVEL |
Called when a change is observed on an object applied to this model
See: Queue
Param | Type | Description |
---|---|---|
callback | function |
Provides four args: path, value, previous value, and the name of the method that produced the change. Context is the model that changed. |
Called when an error is returned from Schema validation
See: Queue
Param | Type | Description |
---|---|---|
callback | function |
Provides one arg: an array of errors. Context is the model that changed. |
Model.defaultErrorLevel(errorLevel) ⇒ MODEL_ERROR_LEVEL
How to handle errors on all models
Default: MODEL_ERROR_LEVEL.WARN
Param | Type |
---|---|
errorLevel | MODEL_ERROR_LEVEL |
How to handle Schema validation errors in models.
Properties
Name | Description |
---|---|
UNSET | Errors are ignored. |
WARN | Console.warn |
ERROR | Console.error |
THROW | Throw an exception |