npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

blueprint-model

v0.2.0

Published

Allow the definition of models.

Downloads

2

Readme

blueprint-model

Build Status Coverage Status

browser support

Database agnostic data modeller. Somewhat inspired by the model component made by TJ Holowaychuck.

Usage and installation

To install the package use:

$ npm install blueprint-model

The package is tested to run in node at least 0.8.x.

API

model#createModel(name, [properties])

Create a new model with the given name and optional properties.

var model = require('blueprint-model');
var User = model.createModel('User');

With properties set during creation:

var model = require('blueprint-model');
var User = model.createModel('User', ['username', 'email', 'password']);

.setProperties(properties)

Add a list of properties to the model.

var model = require('blueprint-model');
var User = model.createModel('User');
User.setProperties(['username', 'email', 'password']);

.setProperty(property)

Add a property to the model.

var model = require('blueprint-model');
var User = model.createModel('User');
User
    .setProperty('username')
    .setProperty('email')
    .setProperty('password');

Property descriptor

A property can be a string, as showed before, or a descriptor, that allows you to add meta information about the property.

The descriptor is an object with the following keys:

  • name: indicate the property name,
  • required: boolean indicating if the property is required, if true will throw TypeError if a null or undefined value is passed to the property,
  • type: indicate the property type, ensuring that you can only set the aproppriate value for the property, will throw TypeError if an invalid value is assigned to the property. For the moment the following types are available:
    • string
    • number
    • boolean
    • date
  • validators: accepts an array of objects containing an fn (function that receives a value and returns a boolean indicating if the value is valid or not) and a message that will be show in case the value isn't valid.
var model = require('blueprint-model');
var properties = [
    {
        name: 'username',
        required: true,
        type: 'string',
        validators: [
            {
                fn: function (value) { return value.length >= 30; },
                message: 'Set a value with at least 30 chars for '
            }
        ]
    },
    {
        name: 'email',
        required: false,
        type: 'string'
    },
    {
        name: 'password',
        requried: true,
        type: 'string',
        validators: [
            {
                fn: function (value) { return value.length >= 8 },
                message: 'Set a value with at least 8 chars for '
            }
        ]
    }
];
var User = model.createModel('User', properties);

.use(function)

Allow to extend the model with custom behavior through a function.

var model = require('blueprint-model');
var User = model.create('User');
User
    .setProperty('username')
    .setProperty('email')
    .setProperty('password')
    .use(function (Model) {
        function logger() {
            console.log('action => ', arguments.join(' : ');
        }
        Model.on('construct', logger);
        Model.on('change', logger);
    });

Events

Constructor

construct

Emitted after an instance is created with attributes set in the constructor.

var User = model.createModel('User', ['username']);
User.on('construct', function (instance, attrs) {
    console.log(instance.model.modelName);
    console.log(Object.keys(attrs));
});
var instance = new User({username: 'me'});

change

Emitted after any property is set or changed.

var User = mode.createModel('User', ['username', 'email']);
User.on('change', function (instance, attr, value) {
    console.log(instance.model.modelName, attr, value);
});
var instance = new User({username: 'me'});
instance.email = '[email protected]';

change <property>

Emitted after the a given property is set or changed.

var User = mode.createModel('User', ['username', 'email']);
User.on('change email', function (instance, attr, value) {
    console.log(instance.model.modelName, attr, value);
});
var instance = new User({username: 'me'});
instance.email = '[email protected]';

Instance

change

Emitted after any property is set or changed.

var User = mode.createModel('User', ['username', 'email']);
var instance = new User({username: 'me'});
instance.on('change', function (instance, attr, value) {
    console.log(instance.model.modelName, attr, value);
});
instance.email = '[email protected]';

change <property>

Emitted after the a given property is set or changed.

var User = mode.createModel('User', ['username', 'email']);
var instance = new User({username: 'me'});
instance.on('change email', function (instance, attr, value) {
    console.log(instance.model.modelName, attr, value);
});
instance.email = '[email protected]';