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 🙏

© 2025 – Pkg Stats / Ryan Hefner

component-model

v0.3.2

Published

W.I.P minimalistic extensible model component.

Readme

model

W.I.P minimalistic extensible model component.

API

model(name)

Create a new model with the given name.

var model = require('model');
var User = model('User');

.attr(name, [meta])

Define an attribute name with optional meta data object.

var model = require('model');

var Post = model('Post')
  .attr('id')
  .attr('title')
  .attr('body')
  .attr('created_at')
  .attr('updated_at')

With meta data used by plugins:

var model = require('model');

var Post = model('Post')
  .attr('id', { required: true, type: 'number' })
  .attr('title', { required: true, type: 'string' })
  .attr('body', { required: true, type: 'string' })
  .attr('created_at', { type: 'date' })
  .attr('updated_at', { type: 'date' })

.validate(fn)

TODO: validation callback docs

.use(fn)

TODO: plugin docs

.url([path])

Return base url, or url to path.

User.url()
// => "/users"

User.url('add')
// => "/users/add"

.route(path)

Set base path for urls. Note this is defaulted to '/' + modelName.toLowerCase() + 's'

User.route('/api/u')

User.url()
// => "/api/u"

User.url('add')
// => "/api/u/add"

.headers({header: value})

Sets custom headers for static and method requests on the model.

User.headers({
  'X-CSRF-Token': 'some token',
  'X-API-Token': 'api token' 
});

.ATTR()

"Getter" function generated when Model.attr(name) is called.

var Post = model('Post')
  .attr('title')
  .attr('body')

var post = new Post({ title: 'Cats' });

post.title()
// => "Cats"

.ATTR(value)

"Setter" function generated when Model.attr(name) is called.

var Post = model('Post')
  .attr('title')
  .attr('body')

var post = new Post;

post.title('Ferrets')
post.title()
// => "Ferrets"
  • Emits "change" event with (name, value, previousValue).
  • Emits "change ATTR" event with (value, previousValue).
post.on('change', function(name, val, prev){
  console.log('changed %s from %s to %s', name, prev, val)
})

post.on('change title', function(val, prev){
  console.log('changed title')
})

.isNew()

Returns true if the model is unsaved.

.toJSON()

Return a JSON representation of the model (its attributes).

.has(attr)

Check if attr is non-null.

.get(attr)

Get attr's value.

.set(attrs)

Set multiple attrs.

user.set({ name: 'Tobi', age: 2 })

.changed([attr])

Check if the model is "dirty" and return an object of changed attributes. Optionally check a specific attr and return a Boolean.

.error(attr, msg)

Define error msg for attr.

.isValid()

Run validations and check if the model is valid.

user.isValid()
// => false

user.errors
// => [{ attr: ..., message: ... }]

.url([path])

Return this model's base url or relative to path:

var user = new User({ id: 5 });
user.url('edit');
// => "/users/5/edit"

.save(fn)

Save or update and invoke the given callback fn(err).

var user = new User({ name: 'Tobi' })

user.save(function(err){

})

Emits "save" when complete.

.destroy([fn])

Destroy and invoke optional fn(err).

Emits "destroy" when successfully deleted.

Links

Testing

$ npm install
$ make test &
$ open http://localhost:3000

License

MIT