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

mongoose-voting

v0.3.0

Published

Mongoose plugin to upvote/downvote stuff. Extends any model with handy methods for voting.

Downloads

37

Readme

mongoose-voting

Mongoose plugin to upvote/downvote stuff. Extends any model with handy methods for voting.

Build Status

Install

  $ npm install mongoose-voting

Usage example

  var CommentSchema = new Schema({..});

  // Default voter is `User` model
  CommentSchema.plugin(voting);

  // Or you can tell `mongoose-voting`
  // which model references
  CommentSchema.plugin(voting, { ref: 'Author' });

  // ...

  var author = new Author({});
  var comment = new Comment({});

  // upvote and check
  comment.upvote(author);
  comment.upvoted(author);      // true
  comment.downvoted(author);    // false

  // downvote with save
  comment.downvote(author, function(err, doc) {
    assert.equal(doc, comment);  // true
    doc.downvoted(author);      // true
  });

  comment.voted(author);        // true

API

.upvote(user)

Upvotes document by user. user can be either a model instance (like User), an ObjectId or even the hex string from ObjectId.

  comment.upvote(author);
  comment.voted(author);    // true
  comment.upvoted(author);  // true

.upvote(user, fn)

Same as .upvote(user) but calls save on model with fn function as callback.

  comment.upvote(author, function(err, doc) {
    doc.voted(author);    // true
    doc.upvoted(author);  // true
  });

.downvote(user)

Downvotes document by user. user can be either a model instance (like User), an ObjectId or even the hex string from ObjectId.

  comment.upvote(author);
  comment.voted(author);    // true
  comment.upvoted(author);  // true

.downvote(user, fn)

Same as .downvote(user) but calls save on model with fn function as callback.

  comment.downvote(author, function(err, doc) {
    doc.voted(author);      // true
    doc.downvoted(author);  // true
  });

.unvote(user)

Cancels any vote cast by user. user can be either a model instance (like User), an ObjectId or even the hex string from ObjectId.

  comment.upvote(author);
  comment.voted(author);    // true
  comment.unvote(author);
  comment.voted(author);    // false

.unvote(user, fn)

Same as .unvote(user) but calls save on model with fn function as callback.

  comment.upvote(author);
  comment.voted(author);    // true
  comment.unvote(author);
  comment.voted(author);    // false

.upvoted(user)

Returns true if document was 'upvoted' by user. false otherwise.

  comment.upvote(user);
  comment.upvoted(user);    // true
  comment.downvoted(user);  // false

.downvoted(user)

Returns true if document was 'downvoted' by user. false otherwise.

  comment.downvote(user);
  comment.upvoted(user);    // false
  comment.downvoted(user);  // true

.voted(user)

Returns true if document was 'upvoted' or 'downvoted' by user. false otherwise.

  comment.downvote(user);
  comment.voted(user);    // true
  comment.upvote(user);
  comment.voted(user);    // true

.upvotes()

Returns Number of upvotes count.

  comment.downvote(user);
  comment.upvotes();      // 0
  comment.upvote(user);
  comment.upvotes();      // 1

.downvotes()

Returns Number of downvotes count.

  comment.downvote(user);
  comment.upvotes();      // 1
  comment.upvote(user);
  comment.upvotes();      // 0

.votes()

Returns Number of votes count.

  comment.downvote(user);
  comment.votes();          // 1
  comment.upvote(user);
  comment.votes();          // 1
  comment.downvote(user2);
  comment.votes();          // 2

Test

  $ npm install --dev
  $ make test

License

MIT