modeling
v0.2.0
Published
Fast and flexible data models for node.js and the browser.
Downloads
9
Maintainers
Readme
Modeling
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.