Socket
Socket
Sign inDemoInstall

active-model

Package Overview
Dependencies
0
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    active-model

Data source agnostic opinionated models for Node.js with classic inheritance, basic validations and a clean API


Version published
Weekly downloads
9
Maintainers
1
Install size
17.8 kB
Created
Weekly downloads
 

Readme

Source

ActiveModel

Data source agnostic opinionated models for Node.js with classic inheritance, basic validations and a clean API. Models only keep pre-defined properties on initialization which protect against property leakage and ensure your models remain clean however tey are instantiated.

Api

model(schema)

Create and return a new model based on schema.

Schema
  • attributes: object with allowed attributes as key and validation as value e.g. width: Number
  • constructor: custom constructor for model that will be executed before model initialization
  • methods: methods for the model instances
  • properties: object with properties to define as Object.defineProperty e.g. area: { get: function () {return this.width * this.height } }
Validations

Validations supported at the moment are type validation for Number, String, Boolean, Date, Object, and Array. When values are not of the correct type, the model will try to cast them to the correct one, otherwise it will throw an error.

Example
var model = require('active-model').model;
var assert = require('assert');

var Rectangle = model({
  attributes: {
    height: Number,
    width: Number
  },
  methods: {
    area: function () { return this.width * this.height; }
  },
  properties: {
    perimeter: {
      get: function () {
        return (this.width + this.height) * 2;
      }
    }
  }
});

var rectangle = new Rectangle({ width: 2, height: 3, unknown: true });
assert.equal(rectangle.width, 2);
assert.equal(rectangle.height, 3);
assert.notEqual(rectangle.unknown, true);
assert.equal(rectangle.area(), 6);
assert.equal(rectangle.perimeter, 10);

It is also possible to configure the attributes, methods, and properties individually.

var model = require('active-model').model;
var assert = require('assert');

var Rectangle = model({
    height: Number,
    width: { type: Number } // support object notation
});

Rectangle.methods({
  area: function () { return this.width * this.height; }
});

Rectangle.properties({
  perimeter: {
    get: function () {
      return (this.width + this.height) * 2;
    }
  }
});

var rectangle = new Rectangle({ width: 2, height: 3, unknown: true });
assert.equal(rectangle.width, 2);
assert.equal(rectangle.height, 3);
assert.notEqual(rectangle.unknown, true);
assert.equal(rectangle.area(), 6);
assert.equal(rectangle.perimeter, 10);

Use a custom constructor

var model = require('active-model').model;
var assert = require('assert');

var Rectangle = model({
    height: Number,
    width: Number,
    constructor: function (width, height) {
      this.width = width;
      this.height = height;
    }
});

var rectangle = new Rectangle(2, 3);
assert.equal(rectangle.width, 2);
assert.equal(rectangle.height, 3);

augment(superclass, properties)

Create a new object inherited form superclass with class methods added to object and properties added to object instances.

Example
var augment = require('active-model').augment;
var Square = augment(Rectangle, {
    constructor: function (side) {
        Rectangle.call(this, side, side);
    }
});

var square = new Square(3)
assert.equal(square.width, 3);
assert.equal(square.height, 3);
assert.equal(square.area(), 9);
assert.equal(square instanceof Rectangle, true);

Credits

  • augment method based on jashkenas's comment in Hacker News thread
  • API loosely inspired by mongoose

Keywords

FAQs

Last updated on 15 Apr 2015

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc