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

activer

v0.11.2

Published

Base class for your JavaScript models that adds useful 'hasOne', 'hasMany', 'belongsTo', 'hasAndBelongsToMany', 'attributes' and 'delegate' static methods (as well as 'save', 'update' and 'destroy' instance methods and a few callbacks)

Downloads

113

Readme

activer

Build Status

Base class for your JavaScript models that adds useful hasOne, hasMany, belongsTo, hasAndBelongsToMany, attributes and delegate static methods (as well as save, update and destroy instance methods and a few callbacks).

Usage

in your user.js:

import Model from 'activer';
import Post from './post'

class User extends Model {
  sayAnotherThing() { console.log('User'); }
}

User.attributes('name', 'description');
User.hasOne('Post');
User.delegate('saySomething', 'Post');

export default User

in your comment.js:

import Model from 'activer';
import Post from './post';

class Comment extends Model {
  saySomething() { console.log('Comment'); }
}

Comment.attributes('name', 'description');
Comment.belongsTo('Post');

export default Comment

in your post.js:

import Model from 'activer';
import User from './user';
import Comment from './comment';

class Post extends Model {
  saySomething() { console.log('Post'); }
}

Post.attributes('name', 'description');
Post.belongsTo('User');
Post.hasMany('Comment', { dependent: 'destroy' });

export default Post

in your main.js:

import User from './user';
import Comment from './comment';

var user = User.create({
  name: "User name",
  description: "User description"
});
var post = user.createPost({
  name: "Post name",
  description: "Post description"
});
var comment1 = post.comments().create({
  name: "Comment 1 name",
  description: "Comment 1 description"
});
var comment2 = post.comments().create({
  name: "Comment 2 name",
  description: "Comment 2 description"
});

user.saySomething(); // 'Post'
console.log(user.post().comments().length); // 2
console.log(Comment.all().length); // 2
console.log(user.post().comments()[0].name); // "Comment 1 name"
user.post().destroy();
console.log(Comment.all().length); // 0
console.log(user.post()); // undefined

See tests for details.

hasMany { through: 'SomeClass' }

To be able to use the hasMany through association the join collection model should be implemented. See example below.

in your post.js:

import Model from 'activer';
import Category from './category';
import CategoryPost from './category_post';

class Post extends Model {}

Post.attributes('name', 'description');
Post.hasMany('CategoryPost');
Post.hasMany('Category', { through: 'CategoryPost' });

export default Post

in your category.js:

import Model from 'activer';
import Post from './post';
import CategoryPost from './category_post';

class Category extends Model {}

Category.attributes('name');
Category.hasMany('CategoryPost');
Category.hasMany('Post', { through: 'CategoryPost' });

export default Category

in your category_post.js (please note class names in the join collection are sorted alphabetically, and they should be):

import Model from 'activer';
import Post from './post';
import Category from './category';

class CategoryPost extends Model {}

CategoryPost.belongsTo('Post');
CategoryPost.belongsTo('Category');

export default CategoryPost

HABTM

To be able to use the hasAndBelongsToMany association the join collection model should be implemented. See example below.

in your post.js:

import Model from 'activer';
import Tag from './tag';
import PostTag from './post_tag';

class Post extends Model {}

Post.attributes('name', 'description');
Post.hasAndBelongsToMany('Tag');

export default Post

in your tag.js:

import Model from 'activer';
import Post from './post';
import PostTag from './post_tag';

class Tag extends Model {}

Tag.attributes('name');
Tag.hasAndBelongsToMany('Post');

export default Tag

in your post_tag.js (please note class names in the join collection are sorted alphabetically, and they should be):

import Model from 'activer';
import Post from './post';
import Tag from './tag';

class PostTag extends Model {}

PostTag.belongsTo('Post');
PostTag.belongsTo('Tag');

export default PostTag

Store

Activer uses in-memory storage by default but you can specify your own data access object to use whatewer you want using the static collection method. DAO should implement some methods:

dataAccessObject = {
  create(props) { /**/ }
  update(id, props) { /**/ }
  remove(id) { /**/ }
  removeAll(props) { /**/ }
  get(id) { /**/ }
  getAll(props) { /**/ }
}

class User extends Model {}
User.collection(dataAccessObject)

See default implementation in src/dao.coffee.

Changelog

0.10.0: Model static methods all and where now return Collection instance. Collection instance method where now returns new Collection instance. One can do User.all().where({ something: 'something' }).where({ anotherThing: 'Another thing' }).deleteAll() now.

0.11.0: hasAndBelongsToMany association implemented. hasMany { through } association implemented.