Supermodeler
Node.js Library for creating models, mapping between types and validating model instances.
There are a lot of NPM modules out there to provide models for database persistence, ORMs, and the like, however there seems to be a shortage of anything targeting more a more generic approach to models.
Supermodeler primarily targets the ability to create domain, or business logic layer models.
In addition it provides easy mapping between model types, and validation of models.
Defining Models
var definition = properties: 'firstName' 'lastName' methods: { return thisfirstName + ' ' + thislastName; } validate: true;supermodeler;
Schema
"properties"
- name - The property or field name.
- readOnly - The properties value will not be able to be modified after constructor complete.
- private - The property will not be exposed in enumeration, and thus will not show up in serialization or output.
- type - Defines this property as being of a sub-type.
- default - Default value if none supplied
- get - Allows for the definition of a getter
- validation - A validate.js validation object
Validation Attributes
- validate - if true then .validate() will be called on the module before completion of constructor.
- validation - if defined then allows for a customer validator method. Note: this will equal the instance on the validation method.
"methods"
Creating Instances
var instance = supermodeler;console;// {firstName: undefined, lastName: undefined} instance = supermodeler;console;// {firstName: "John", lastName: "Smith"} instance = supermodeler;console;// {firstName: "John", lastName: undefined} // Properties not defined on models are either:// a) Ignored// b) Throw an error if 'use strict' is definedinstance = supermodeler;console;// {firstName: "John", lastName: undefined}
Mapping
supermodeler.map(source, sourceType, targetType)
Source can be either:
- An object of type
sourceType
, in which case map() will return an object of typetargetType
. - An array of type
sourceType
, in which case map() will return an array of typetargetType
. - A promise that resolves to either an array or an object as above, in which case map() will return a promise (specifically a Q promise) that resolves with the results mapped to
targetType
.
Validation
Validation rules are defined on the schema as a validate
property. See validate.js for available validation rules.
properties: name: 'name' validate:presence:true
Each model instance has a $validate
method on it that can be called to validate the current state of the instance.
var user = supermodeler;user;
Also, if the schema has validate=true
set, then this method will be called in the constructor.
supermodeler
Finally, you can validate an anonymous object against the validation rules by calling $validate
on the Contructor.
var user = name: 'test';var User = supermodeler;User;
Example:
See the demo/user.js file for a running version of the following:
// uncomment use strict to throw errors, otherwise things will fail silently// 'use strict'; var modeler = ; // alternatively:// var Supermodeler = new require('../lib').Supermodeler;// var modeler = new Supermodeler(); // define the modelsmodeler; // models can have submodels... see "group" in DbUsermodeler; // the primary intent of the module is to use with domain modelsmodeler; modeler; modeler; // we can define maps between model types.// NOTE: the source type does not need to exist as a model, but the target type doesmodeler; modeler; // use .create to create a new instance of a modelvar dbmodel = modeler; // alternatively we can get the constructorvar DbUser = modeler;var dbuser = given_name:'Dennis' surname:'Williams' user_id:'xyzabcd' user_role:'initial' group: id: 'id' name: 'name' ; // NOTE: user_id is read only, so we can't alter it's value after construction//... the following will faildbmodeluser_id = '9999999';console;console;console;console; var dommodel = modeler;console;console;console;console; var apimodel = modeler;console;console;console;console;