blueprint-model
v0.2.0
Published
Allow the definition of models.
Downloads
2
Readme
blueprint-model
Database agnostic data modeller. Somewhat inspired by the model component made by TJ Holowaychuck.
Usage and installation
To install the package use:
$ npm install blueprint-model
The package is tested to run in node at least 0.8.x.
API
model#createModel(name, [properties])
Create a new model with the given name and optional properties.
var model = require('blueprint-model');
var User = model.createModel('User');
With properties set during creation:
var model = require('blueprint-model');
var User = model.createModel('User', ['username', 'email', 'password']);
.setProperties(properties)
Add a list of properties to the model.
var model = require('blueprint-model');
var User = model.createModel('User');
User.setProperties(['username', 'email', 'password']);
.setProperty(property)
Add a property to the model.
var model = require('blueprint-model');
var User = model.createModel('User');
User
.setProperty('username')
.setProperty('email')
.setProperty('password');
Property descriptor
A property can be a string, as showed before, or a descriptor, that allows you to add meta information about the property.
The descriptor is an object
with the following keys:
name
: indicate the property name,required
: boolean indicating if the property is required, iftrue
will throwTypeError
if anull
orundefined
value is passed to the property,type
: indicate the property type, ensuring that you can only set the aproppriate value for the property, will throwTypeError
if an invalid value is assigned to the property. For the moment the following types are available:string
number
boolean
date
validators
: accepts an array ofobject
s containing anfn
(function that receives a value and returns aboolean
indicating if the value is valid or not) and amessage
that will be show in case the value isn't valid.
var model = require('blueprint-model');
var properties = [
{
name: 'username',
required: true,
type: 'string',
validators: [
{
fn: function (value) { return value.length >= 30; },
message: 'Set a value with at least 30 chars for '
}
]
},
{
name: 'email',
required: false,
type: 'string'
},
{
name: 'password',
requried: true,
type: 'string',
validators: [
{
fn: function (value) { return value.length >= 8 },
message: 'Set a value with at least 8 chars for '
}
]
}
];
var User = model.createModel('User', properties);
.use(function)
Allow to extend the model with custom behavior through a function.
var model = require('blueprint-model');
var User = model.create('User');
User
.setProperty('username')
.setProperty('email')
.setProperty('password')
.use(function (Model) {
function logger() {
console.log('action => ', arguments.join(' : ');
}
Model.on('construct', logger);
Model.on('change', logger);
});
Events
Constructor
construct
Emitted after an instance is created with attributes set in the constructor.
var User = model.createModel('User', ['username']);
User.on('construct', function (instance, attrs) {
console.log(instance.model.modelName);
console.log(Object.keys(attrs));
});
var instance = new User({username: 'me'});
change
Emitted after any property is set or changed.
var User = mode.createModel('User', ['username', 'email']);
User.on('change', function (instance, attr, value) {
console.log(instance.model.modelName, attr, value);
});
var instance = new User({username: 'me'});
instance.email = '[email protected]';
change <property>
Emitted after the a given property is set or changed.
var User = mode.createModel('User', ['username', 'email']);
User.on('change email', function (instance, attr, value) {
console.log(instance.model.modelName, attr, value);
});
var instance = new User({username: 'me'});
instance.email = '[email protected]';
Instance
change
Emitted after any property is set or changed.
var User = mode.createModel('User', ['username', 'email']);
var instance = new User({username: 'me'});
instance.on('change', function (instance, attr, value) {
console.log(instance.model.modelName, attr, value);
});
instance.email = '[email protected]';
change <property>
Emitted after the a given property is set or changed.
var User = mode.createModel('User', ['username', 'email']);
var instance = new User({username: 'me'});
instance.on('change email', function (instance, attr, value) {
console.log(instance.model.modelName, attr, value);
});
instance.email = '[email protected]';