All Projects → codemix → modeling

codemix / modeling

Licence: MIT license
Fast and flexible data models for node.js and the browser.

Programming Languages

javascript
184084 projects - #8 most used programming language

Modeling

Build Status

Usage

var Modeling = require('modeling');

var Thing = Modeling("Thing", {
  name: {
    label: 'Name',
    description: 'The name of the item',
    type: 'string',
    rules: [
      ['required']
    ]
  },
  description: {
    label: 'Description',
    description: 'A short description of the item.',
    type: 'string',
    rules: [
      {name: 'length', max: 255}
    ]
  },
  url: {
    label: 'URL',
    description: 'A URL identifiying the item.',
    type: 'string',
    rules: [
      ['url']
    ]
  }
});

var Person = Thing.extend("Person", {
  dateOfBirth: {
    label: 'Date of Birth',
    description: "The person's date of birth.",
    type: Date,
    rules: [
      ['date']
    ]
  },
  age: {
    label: 'Age',
    description: "The person's age.",
    type: Number,
    get: function () {
      var birthday = this.dateOfBirth;
      if (!birthday) {
        return false;
      }
      var ageDifMs = Date.now() - birthday.getTime();
      var ageDate = new Date(ageDifMs); // miliseconds from epoch
      return Math.abs(ageDate.getFullYear() - 1970);
    }
  }
});

var person = new Person({
  name: 'Bob',
  url: 'http://codemix.com/',
  dateOfBirth: '1980-01-01'
});

console.log(person.age);

console.log(JSON.stringify(person, null, 2));

Installation

Via npm:

npm install --save modeling

or bower:

bower install --save modeling

Running the tests

First, npm install, then npm test. Code coverage generated with npm run coverage.

License

MIT, see LICENSE.md.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].