modeljs-model
v0.1.0
Published
WIP.
Downloads
2
Readme
Model.js
WIP.
Data models that can be stored in any database or remote service using adapters.
Installation
node:
npm install modeljs-model
browser:
component install modeljs/model
Examples
var model = require('modeljs-model');
Attributes
model('post')
.attr('title') // defaults to 'string'
.attr('body', 'text')
.attr('published', 'boolean', false);
Validations
model('user')
.attr('email')
.validate('presence')
.validate('isEmail')
.validate('emailProvider', { in: [ 'gmail.com' ] }) // some hypothetical one
.attr('username')
.validator(function(val){
return !!val.match(/[a-zA-Z]/);
});
There are two DSL methods for validation.
validate
: for using predefined validations (see tower-validator), purely to clean up the API.validator
: for defining custom validator functions right inline. If you want to reuse your custom validator function across models, just move the function into tower-validator.
Queries
model('post')
.where('published').eq(true)
.select(function(err, posts){
});
model('post').query().sort('title', -1).select();
Actions
There are 4 main actions for models (which are just delegated to query().action(name)
:
- create
- all
- update
- remove
model('post').create();
model('post').select();
model('post').update({ published: true });
model('post').remove();
Under the hood, when you execute one of these actions, they get handled by a database-/service-specific adapter (mongodb, cassandra, facebook, etc.). Those adapters can perform optimizations such as streaming query results back.
License
MIT