Modern idiomatic models for the browser and node.js.
- Restrict enumerable properties to defined model attributes.
- Events emitted for initialization, attribute changes, errors, etc.
- Attribute validators and defaults.
- Computed properties (accessors) and sealed models.
- Usable seamlessly across the browser and server.
- Plugins specific to environment.
- Tests and MIT license.
Using npm:
npm install mio
Using component:
component install alexmingoia/mio
Using bower:
bower install mio
var mio = require('mio');
var User = mio.createModel('user');
User
.attr('id', {
primary: true
})
.attr('name', {
required: true
})
.attr('created_at', {
required: true,
default: function() {
return new Date();
}
});
var user = new User({ name: 'alex' });
Create new model constructor with given name
.
Exported array of validators shipped with mio.
Define an attribute with given name
and options
.
User.attr('created_at', {
type: 'date',
required: true,
default: function() {
return new Date();
}
});
Use a plugin function that extends the model. Function is called with Model
as
the context and Model
as the argument.
User
.use(require('example-plugin'))
.browser(function() {
this.use(require('mio-ajax'));
})
.server(function() {
this.use(require('mio-mysql'));
});
Called when installed using bower or component.
Called when installed using npm.
var User = mio.createModel('user');
console.log(User.type);
// => "User"
Storage adapter plugin.
Array of validator functions. Validation plugins should add their validator function(s) here.
Plugins should use this object to store options.
User.find(123, function(err, user) {
// ...
});
User.findAll({
approved: true
}, function(err, collection) {
console.log(collection);
// => [user1, user2, user3, ...]
});
User.count(function(err, count) {
console.log(count);
// => 47
});
User.removeAll({
created: '2013-11-01'
}, function(err) {
// ...
});
Define a "has many" relationship.
User.hasMany(Post, {
as: 'posts',
foreignKey: 'user_id'
});
user.posts.all(function(err, posts) {
// ...
});
user.posts.create(function(body, function(err, post) {
// ...
});
Specify a model using params.through
to relate the other two models:
Post.hasMany(Tag, {
as: 'tags',
through: PostTag, // model with "tag_id" and "post_id" properties
throughKey: 'tag_id',
foreignKey: 'post_id'
});
Define a "belongs to" relationship.
Post.belongsTo(User, {
as: 'author',
foreignKey: 'user_id'
});
post.author.get(function(err, user) {
// ...
});
Define a "has one" relationship.
User.hasOne(Subscription, {
as: 'subscription',
foreignKey: 'subscription_id'
});
Specify a model using params.through
to relate the other two models:
User.hasOne(Group, {
as: 'group',
through: Membership,
throughKey: 'group_id',
foreignKey: 'user_id'
});
user.save(function(err) {
// ...
});
user.remove(function(err) {
// ...
});
Runs validators, repopulates model.errors array with any validation errors encountered, and returns a boolean of whether the model validated.
Whether the model has attributes that have changed since last sav.
Return attributes changed since last save.
Generate and add error to model.errors
array, and emit "error" event.
Array of validation or other errors the model has encountered.
A mutable object for saving extra information pertaining to the model instance.
Query methods specific to relation
, where relation
is the options.as
parameter defined with Model.hasMany()
, .belongsTo()
, or .hasOne()
.
User.hasMany(Post, { as: 'posts', foreignKey: 'user_id' });
user.posts.all(function(err, posts) {
// ...
});
// Add post instance(s)
user.post.add(post1, post2, function(err) {});
// Add array of post instances
user.post.add([post1, post2], function(err) {});
// Add posts by post id
user.post.add(1, 3, function(err) {});
user.post.all({
created_at: '2013-10-31'
},
function(err, collection) {
console.log(collection);
// => [post1, post2, post3, ...]
});
user.post.count({
created_at: '2013-10-31'
},
function(err, count) {
console.log(count);
// => 64
});
// Create and save related post from attributes
user.post.create({
title: 'Hello World',
content: 'My first post.',
},
function(err, post) {
// ...
});
// Create multiple related posts by padding multiple attributes objects.
user.post.create(
{ title: 'Post 1' },
{ title: 'Post 2' },
function(err, post1, post2) {
// ...
}
);
post.author.get(function(err, user) {
// ...
});
user.post.has(post, function(err, isRelated) {
console.log(isRelated);
// => true
});
// Remove post instance(s)
user.post.remove(post1, post2, function(err) {});
// Remove array of post instances
user.post.remove([post1, post2], function(err) {});
// Remove posts by post id
user.post.remove(1, 3, function(err) {});
Receives arguments model
and attributes
.
Receives argument model
.
Receives arguments model
and attributes
.
Receives arguments model
, name
, value
, and prev
.
Receives arguments model
, value
, and prev
.
Receives arguments name
and params
.
Receives arguments model
and error
.
Receives argument attributes
.
Receives arguments name
, value
, and prev
.
Receives arguments value
, and prev
.
Receives argument error
.