W.I.P minimalistic extensible model component.
Create a new model with the given name
.
var model = require('model');
var User = model('User');
Define an attribute name
with optional meta
data object.
var model = require('model');
var Post = model('Post')
.attr('id')
.attr('title')
.attr('body')
.attr('created_at')
.attr('updated_at')
With meta data used by plugins:
var model = require('model');
var Post = model('Post')
.attr('id', { required: true, type: 'number' })
.attr('title', { required: true, type: 'string' })
.attr('body', { required: true, type: 'string' })
.attr('created_at', { type: 'date' })
.attr('updated_at', { type: 'date' })
TODO: validation callback docs
TODO: plugin docs
Return base url, or url to path
.
User.url()
// => "/users"
User.url('add')
// => "/users/add"
Set base path for urls.
Note this is defaulted to '/' + modelName.toLowerCase() + 's'
User.route('/api/u')
User.url()
// => "/api/u"
User.url('add')
// => "/api/u/add"
Sets custom headers for static and method requests on the model.
User.headers({
'X-CSRF-Token': 'some token',
'X-API-Token': 'api token'
});
"Getter" function generated when Model.attr(name)
is called.
var Post = model('Post')
.attr('title')
.attr('body')
var post = new Post({ title: 'Cats' });
post.title()
// => "Cats"
"Setter" function generated when Model.attr(name)
is called.
var Post = model('Post')
.attr('title')
.attr('body')
var post = new Post;
post.title('Ferrets')
post.title()
// => "Ferrets"
- Emits "change" event with
(name, value, previousValue)
. - Emits "change ATTR" event with
(value, previousValue)
.
post.on('change', function(name, val, prev){
console.log('changed %s from %s to %s', name, prev, val)
})
post.on('change title', function(val, prev){
console.log('changed title')
})
Returns true
if the model is unsaved.
Return a JSON representation of the model (its attributes).
Check if attr
is non-null
.
Get attr
's value.
Set multiple attrs
.
user.set({ name: 'Tobi', age: 2 })
Check if the model is "dirty" and return an object
of changed attributes. Optionally check a specific attr
and return a Boolean
.
Define error msg
for attr
.
Run validations and check if the model is valid.
user.isValid()
// => false
user.errors
// => [{ attr: ..., message: ... }]
Return this model's base url or relative to path
:
var user = new User({ id: 5 });
user.url('edit');
// => "/users/5/edit"
Save or update and invoke the given callback fn(err)
.
var user = new User({ name: 'Tobi' })
user.save(function(err){
})
Emits "save" when complete.
Destroy and invoke optional fn(err)
.
Emits "destroy" when successfully deleted.
Register a custom error handler for the model class and/or model instances. If called on the Model class, the error handler is used for static methods as well as the methods of the model instances. If called on a model instance, the error handler is only used for methods of this instance.
var handler1 = function(err, res) {...};
var handler2 = function(err, res) {...};
var Car = model('Car')
.toError(handler1);
var car1 = new Car(); // uses handler1
var car2 = new Car();
car2.toError(handler2); // uses handler2
- Plugins for model
$ npm install
$ make test &
$ open http://localhost:3000
MIT