Skip to content
This repository has been archived by the owner on Feb 8, 2020. It is now read-only.

avdgaag/tipe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tipe: a very simple model library

Tipe is an experiment in building a very simple library for building javascript business models. These models are inteded to have very little logic, and act mostly as simple data containers. You are expected to validate incoming property values in setter functions and do transformations and business logic in other objects.

Installation

You can install Tipe with NPM:

% npm install tipe-model

Then you can require it in your code:

var Tipe = require('tipe');

Documentation

See the annotated source code.

Usage

You can define models by extending Tipe.Model and defining your model schema in a callback function:

var Person = Tipe.Model.extend(function(attr) {
  attr.accessor('firstName');
  attr.accessor('lastName');
  attr.property('fullName', {
    serialize: false,
    get: function() {
      return this.firstName + ' ' + this.lastName;
    }
  });
  attr.accessor('age', {
    default: 20,
    serialize: function(val) {
      return val * 2;
    },
    set: function(val) {
      this.attributes.age = parseInt(val);
    }
  });
});

This Person model has four attributes:

  • firstName
  • lastName
  • fullName (read-only, computed based on firstName and lastName and not included in any JSON representation)
  • age (defaults to 20, parsed to integer when set and doubled in JSON representation)

You can use it as follows:

var john = new Person({ firstName: 'John' });
john.lastName = 'Cleese';
console.log(john.firstName);
// => 'John'
console.log(john.fullName);
// => 'John Cleese'
console.log(john.age);
// => 20
console.log(john.toJSON());
// => { firstName: 'John', lastName: 'Cleese', age: 40 }

Credits

Author: Arjan van der Gaag
Email: arjan@arjanvandergaag.nl
URL: http://github.com/avdgaag/tipe

Released under a MIT license. See LICENSE for more information.

About

A simple javascript model library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published