nodee-model
Advanced, database agnostic ODM
Last updated a year ago by nodee .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install nodee-model 
SYNC missed versions from official npm registry.

MIT License Build Status

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, 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)

Installation

npm install nodee-model

Usage

var Model = 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
 */
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 as submodel
    address: { model: Model('Address') },
    // or array of submodels
    addresses: { arrayOf: Model('Address') }
});

// define connection details by extending inherited defaults
Employee.extendDefaults({
    connection:{
        host: 'localhost',
        port: 27017,
        database:'myapp',
        collection:'employees'
    }
});

// now, add hookable method
Employee.prototype.changeJob = Employee.wrapHooks('changeJob', function(newJob, cb){
    var employee = this;
    employee.job = newJob;
    employee.jobConfirmed = false;
    employee.update(cb);
});

// register "beforeChangeJob" listener
Employee.on('beforeChangeJob', function(next){
    // notify HR department
    next();
});

// init model, this will ensure indexes or do some work to init datastore
Employee.init();

// now we can get employee and change his job
Employee.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".
var SuperEmployee = Model.define('SuperEmployee', ['Employee']);

Current Tags

  • 1.2.3                                ...           latest (a year ago)

29 Versions

  • 1.2.3                                ...           a year ago
  • 1.2.2                                ...           a year ago
  • 1.2.1                                ...           a year ago
  • 1.2.0                                ...           a year ago
  • 1.1.1                                ...           3 years ago
  • 1.0.7                                ...           3 years ago
  • 1.0.6                                ...           3 years ago
  • 1.0.5                                ...           3 years ago
  • 1.0.4                                ...           4 years ago
  • 1.0.3                                ...           4 years ago
  • 1.0.2                                ...           4 years ago
  • 1.0.1                                ...           4 years ago
  • 1.0.0                                ...           4 years ago
  • 0.9.14                                ...           4 years ago
  • 0.9.13                                ...           4 years ago
  • 0.9.12                                ...           4 years ago
  • 0.9.11                                ...           4 years ago
  • 0.9.10                                ...           4 years ago
  • 0.9.9                                ...           4 years ago
  • 0.9.8                                ...           4 years ago
  • 0.9.7                                ...           4 years ago
  • 0.9.6                                ...           4 years ago
  • 0.9.5                                ...           4 years ago
  • 0.9.4                                ...           4 years ago
  • 0.9.3                                ...           4 years ago
  • 0.9.2                                ...           4 years ago
  • 0.9.1                                ...           4 years ago
  • 0.9.0                                ...           5 years ago
  • 0.8.10                                ...           5 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (2)
Dev Dependencies (0)
None
Dependents (1)

Copyright 2014 - 2016 © taobao.org |