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, nodee-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)
npm install nodee-model
varModel=require('nodee-model');/* * create employee model, which: * 1. is stored in mongo database * 2. is in tree structure * 3. is orderable * 4. can handle process of changing job - it is not just a simple update, * it has to be confirmed by HR department */varEmployee=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 as submodeladdress: {model: Model('Address')},// or array of submodelsaddresses: {arrayOf: Model('Address')}});// define connection details by extending inherited defaultsEmployee.extendDefaults({connection:{host: 'localhost',port: 27017,database:'myapp',collection:'employees'}});// now, add hookable methodEmployee.prototype.changeJob=Employee.wrapHooks('changeJob',function(newJob,cb){varemployee=this;employee.job=newJob;employee.jobConfirmed=false;employee.update(cb);});// register "beforeChangeJob" listenerEmployee.on('beforeChangeJob',function(next){// notify HR departmentnext();});// init model, this will ensure indexes or do some work to init datastoreEmployee.init();// now we can get employee and change his jobEmployee.collection().find({name:'Chuck',surname:'Norris'}).one(function(err,employee){employee.changeJob('super_agent',function(err){// job changed, and HR department was notified// (but I am sure they can't change Chuck's job - nobody can :)});});// if you need to define a new type of employee, just inherit it from Employee.// It will inherit all methods including registered events like "beforeJobChange".varSuperEmployee=Model.define('SuperEmployee',['Employee']);