0.1.0 • Published 3 years ago

typemodel v0.1.0

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

TypeModel

Model definition utility for node.js and the browser

codecov Build Status

Full documentation - Generated with TypeDoc

Typed models for JavaScript

TypeModel lets consumers of the library to define their own typed JavaScript models. Each model will define the shape of its fields using any of the library provided fields.

The consumers will also be able to define their own field by extending any of the library fields, ranging from AnyField, which accepts all values, to NumberField, which accepts only numbers (i.e., it would be quite easy to define a PrimeNumberField or a SquareNumberField). The fields can be defined as many and they will be treated as array-like objects.

The defined models will expose the named fields as their own properties. Value validation will be triggered when setting values to the fields or setting/updating the contents of the array-like objects when the field is defined as many. Finally, the fields will store knowledge of how they should be serialized, making it easy to convert the modelled object into a JSON to be sent to the database. The shape of the serialization is up to the consumer who can override the serialize methods of each field.

Installation

npm install --save typemodel

Usage

Define your own models by extending AbstractModel.

Define your own fields by extending any of the library fields:

  • AnyField: accepts any value in any format, with the exception of undefined.
  • TypedField: accepts any value of the specified type, should be considered the base of the other typed fields
  • BooleanField: accepts any boolean value
  • InstanceField: accepts any instance of the specified class
  • NumberField: accepts any numeric value
  • StringField: accepts any string value

License

TypeModel is released under the MIT license.