Advanced, database agnostic ODM
Sometimes your application is not a simple to-do list and you need to write complex business logic.
Rather than strictly predefined CRUD methods and hooks, nis-model is a set of tools which you can use to write your own logic, data sources and reusable behaviours.
- Scheme (inheritable, with nested models)
- Validations (extendable validations and sanitizers)
- Defaults (inheritable default settings)
- Methods (inheritable instance and constructor methods)
- Queries (inheritable, extendable and cacheable query builder methods)
- Hooks (inheritable and extendable)
- Relations with integrity maintaining hooks
- Data sources with optimistic locks (Memory, Json file, Mongo, Rest, Elastic search)
- Caching (synchronize workers' cache across nodejs cluster)
- Behaviours (Orderable, Tree)
Installation
npm install nis-model
Usage
var Model = require('nis-model');
var Employee = Model.define('Employee', [ 'MongoDataSource', 'Orderable', 'Tree' ], {
name:{ isString:true },
surname:{ isString:true },
salary:{ isNumber:true, round:2 },
job:{ isIn:[ 'project_manager', 'sales', 'support'] },
jobConfirmed:{ isBool:true },
address: { model: Model('Address') },
addresses: { arrayOf: Model('Address') }
});
Employee.extendDefaults({
connection:{
host: 'localhost',
port: 27017,
database:'myapp',
collection:'employees'
}
});
Employee.prototype.changeJob = Employee.wrapHooks('changeJob', function(newJob, cb){
var employee = this;
employee.job = newJob;
employee.jobConfirmed = false;
employee.update(cb);
});
Employee.on('beforeChangeJob', function(next){
next();
});
Employee.init();
Employee.collection().find({ name:'Chuck', surname:'Norris' }).one(function(err, employee){
employee.changeJob('super_agent', function(err){
});
});
var SuperEmployee = Model.define('SuperEmployee', ['Employee']);