minimodel

Minimal, database agnostic Models for Node.js (and the Browser)

Minimodel

Minimal, database agnostic Models for Node.js and the Browser.

The idea is to implement a Domain Model System that is persitence agnostic, to be used only for validation, type casting, transormations, and business logic. By using minimodel, persintence (if relevant) has to be delegated to an external component (e.g. DAO/Services).

Advantages

  • The same model can be retrieved from different data sources
  • Easily reuse the same models in the Browser
  • Custom persitence allows more fine grained and powerful queries (instead of using an imposed ORM style querying system)

Features

  • Define schemas with a syntax similar to Mongoose's
  • Type checking/casting
  • Defaults
  • Validation
  • Virtuals
  • Custom getters/setters
var minimodel = require('minimodel');
 
var Post = minimodel.define({
  id: {
    //the field is a String 
    type: String,
    //the field is required 
    required: true
  },
  createdDate: {
    type: Date,
    //set the default (can be a value or a function) 
    defaultfunction() {
      //the execution context is the field itself 
      this.setRaw(new Date());
    }
  },
  visits: {
    type: Number,
    default: 0,
    //do not include during object export using toJson() 
    includeInJson: false
    //other options include 
    //includeInObject: false 
    //includeInDb: false 
  },
  //a nested object 
  author: {
    //shortcut to define a field 
    name: String,
    surname: String,
    fullname: {
      //Virtuals are by default not exported (e.g. using toObject()) 
      //Virtuals do not have any default getter/setter, neet to define them explicitly 
      type: minimodel.Types.Virtual,
      //a custom getter 
      getfunction() {
        return this.model.author.name + " " + this.model.author.surname;
      },
      //a custom setter 
      setfunction(val) {
        var parts = val.split(' ');
        this.model.setRaw({
          author: {
            name: parts[0],
            surname: parts[1]
          }
        });
      }
    }
  }
});
 
//create a post 
var post = new Post({
  id: Date.now(),
  author: {
    fullname: "John Doe"
  }
});
 
//set a field after creation 
post.set('author.name', 'Joe');
//or 
post.author.name = 'Johnny'
 
//get a field 
console.log(post.get('author.fullname'));
//or 
console.log(post.author.name);
 

1 - Experimental

Please try it out and provide feedback.

  • Breaking changes:
    • validate() is now an async function. A callback could be given as argument otherwise a promise will be returned.