simplified models
modella
is a bare bones model. It exposes a few APIs on which plugins can be
built to enhance functionality. Usage of these plugins enables high-powered but
bloat free models.
All modella
definition methods are chainable.
Creates a new model with the name modelName
.
var modella = require('modella'),
User = modella('User');
As mentioned in the philosophy, modella
's goal is to make it easy to extend
models with plugins. This lets you create models that do exactly what you need,
and nothing more.
var modella = require('modella'),
validators = require('modella-validators'),
User = modella('User');
User.use(validators);
Adds attribute attrName
to a model. Additional options
can be passed in as
an object. Modella does not use these options, but plugins may.
var modella = require('modella'),
validators = require('modella-validators'),
User = modella('User');
User.use(validators);
User
.attr('_id')
.attr('username', { required: true })
.attr('email', { required: true, format: 'email' });
Adds a validator to the model. A validator should add error messages for attributes that fail validation. Note that plugins such as modella/validators make extensive use of this.
var User = modella('User');
User.validate(function(user) {
if(!user.username()) {
user.error('username', "is required");
}
});
You can create instances of models with the new
operator. You can also specify
initial values by passing in an object for initialValues
var user = new User();
var bob = new User({username: 'Bob' });
Sets the given attribute to a value.
var user = new User();
user.username("Bob");
Returns the value of the attribute
var user = new User({username: 'Bob'});
user.username()
=> 'Bob'
Returns the value of the attribute
var user = new User({username: 'Bob'});
user.get('username')
=> 'Bob'
Returns whether an instance has an attribute set.
var user = new User({username: 'Bob'});
user.has('email')
=> false
Quickly sets multiple attributes.
var user = new User();
user.set({username: 'Bob', email: 'bob@bobbington.com'});
Returns the value of the primary key attribute. By default, this auto-maps to an
attribute with the name of _id
or id
if it specified.
var User = modella('User').attr('_id');
var user = new User({_id: 123 });
user.primary();
=> 123
Sets the value of the primary key to value
. By default primary key will map to
an attribute with the name of _id
or id
var User = modella('User').attr('_id');
var user = new User({_id: 123 });
user.primary(456);
user.primary();
=> 456
Returns whether the value of Model#primary()
is blank.
var user = new User();
user.isNew()
=> true
var oldUser = new User({_id: 555});
oldUser.isNew()
=> false
Runs all validators on the model and returns whether any validations failed.
var validators = require('modella-validators');
var User = modella('User')
User.use(validators);
User.attr('username', { required: true });
var user = new User();
user.isValid()
=> false
Saves the model using the syncLayer
. Will not attempt to save if
model#isValid()
returns false.
Calls cb(err)
after save.
Deletes the model using the sync layer and marks it as removed
.
Calls cb(err)
after remove.
Marked as true if the model has been deleted.
user.remove()
user.removed
=> true
Points to the base model from which the instance was created.
var user = new User();
user.model === User
=> true
TODO: Write some documentation
All modella models have built in emitters on both instances and the model itself.
TODO: Write some documentation
MIT