blueprint-model

Allow the definition of models.

blueprint-model

Database agnostic data modeller. Somewhat inspired by the model component made by TJ Holowaychuck.

To install the package use:

$ npm install blueprint-model

The package is tested to run in node at least 0.8.x.

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']);

Add a list of properties to the model.

var model = require('blueprint-model');
var User = model.createModel('User');
User.setProperties(['username', 'email', 'password']);

Add a property to the model.

var model = require('blueprint-model');
var User = model.createModel('User');
User
    .setProperty('username')
    .setProperty('email')
    .setProperty('password');

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, if true will throw TypeError if a null or undefined value is passed to the property,
  • type: indicate the property type, ensuring that you can only set the aproppriate value for the property, will throw TypeError 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 of objects containing an fn (function that receives a value and returns a boolean indicating if the value is valid or not) and a message 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: [
            {
                fnfunction (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: [
            {
                fnfunction (value) { return value.length >= 8 },
                message: 'Set a value with at least 8 chars for '
            }
        ]
    }
];
var User = model.createModel('User', properties);

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);
    });

Emitted after an instance is created with attributes set in the constructor.

var User = model.createModel('User', ['username']);
User.on('construct', function (instanceattrs) {
    console.log(instance.model.modelName);
    console.log(Object.keys(attrs));
});
var instance = new User({username: 'me'});

Emitted after any property is set or changed.

var User = mode.createModel('User', ['username', 'email']);
User.on('change', function (instanceattrvalue) {
    console.log(instance.model.modelName, attr, value);
});
var instance = new User({username: 'me'});
instance.email = 'me@me.com';

Emitted after the a given property is set or changed.

var User = mode.createModel('User', ['username', 'email']);
User.on('change email', function (instanceattrvalue) {
    console.log(instance.model.modelName, attr, value);
});
var instance = new User({username: 'me'});
instance.email = 'me@me.com';

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 (instanceattrvalue) {
    console.log(instance.model.modelName, attr, value);
});
instance.email = 'me@me.com';

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 (instanceattrvalue) {
    console.log(instance.model.modelName, attr, value);
});
instance.email = 'me@me.com';