mongoose-voting
Mongoose plugin to upvote/downvote stuff. Extends any model with handy methods for voting.
Install
$ npm install mongoose-voting
Usage example
var CommentSchema = ; // Default voter is `User` model CommentSchema; // Or you can tell `mongoose-voting` // which model references CommentSchema; // ... var author = {}; var comment = {}; // upvote and check comment; comment; // true comment; // false // downvote with save comment; comment; // 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; comment; // true comment; // true
.upvote(user, fn)
Same as .upvote(user)
but calls save
on model with fn
function as callback.
comment;
.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; comment; // true comment; // true
.downvote(user, fn)
Same as .downvote(user)
but calls save
on model with fn
function as callback.
comment;
.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; comment; // true comment; comment; // false
.unvote(user, fn)
Same as .unvote(user)
but calls save
on model with fn
function as callback.
comment; comment; // true comment; comment; // false
.upvoted(user)
Returns true
if document was 'upvoted' by user. false
otherwise.
comment; comment; // true comment; // false
.downvoted(user)
Returns true
if document was 'downvoted' by user. false
otherwise.
comment; comment; // false comment; // true
.voted(user)
Returns true
if document was 'upvoted' or 'downvoted' by user. false
otherwise.
comment; comment; // true comment; comment; // true
.upvotes()
Returns Number of upvotes
count.
comment; comment; // 0 comment; comment; // 1
.downvotes()
Returns Number of downvotes
count.
comment; comment; // 1 comment; comment; // 0
.votes()
Returns Number of votes
count.
comment; comment; // 1 comment; comment; // 1 comment; comment; // 2
Test
$ npm install --dev
$ make test
License
MIT