Tipe is an experiment in building a very simple library for building javascript business models. These models are inteded to have very little logic, and act mostly as simple data containers. You are expected to validate incoming property values in setter functions and do transformations and business logic in other objects.
You can install Tipe with NPM:
% npm install tipe-model
Then you can require
it in your code:
var Tipe = require('tipe');
See the annotated source code.
You can define models by extending Tipe.Model
and defining your model schema
in a callback function:
var Person = Tipe.Model.extend(function(attr) {
attr.accessor('firstName');
attr.accessor('lastName');
attr.property('fullName', {
serialize: false,
get: function() {
return this.firstName + ' ' + this.lastName;
}
});
attr.accessor('age', {
default: 20,
serialize: function(val) {
return val * 2;
},
set: function(val) {
this.attributes.age = parseInt(val);
}
});
});
This Person
model has four attributes:
firstName
lastName
fullName
(read-only, computed based onfirstName
andlastName
and not included in any JSON representation)age
(defaults to 20, parsed to integer when set and doubled in JSON representation)
You can use it as follows:
var john = new Person({ firstName: 'John' });
john.lastName = 'Cleese';
console.log(john.firstName);
// => 'John'
console.log(john.fullName);
// => 'John Cleese'
console.log(john.age);
// => 20
console.log(john.toJSON());
// => { firstName: 'John', lastName: 'Cleese', age: 40 }
Author: Arjan van der Gaag
Email: arjan@arjanvandergaag.nl
URL: http://github.com/avdgaag/tipe
Released under a MIT license. See LICENSE for more information.