mongoose-plugin-votes

1.2.3 • Public • Published

mongoose-plugin-votes

Codeship Status for CentralPing/mongoose-plugin-votes Code Climate for CentralPing/mongoose-plugin-votes Dependency Status for CentralPing/mongoose-plugin-votes

A mongoose.js plugin that provides vote and unvote methods for model instances. The method names are configurable (e.g. like and unlike).

Note: document changes are not persisted until document is saved.

Installation

npm i --save mongoose-plugin-votes

API Reference

Example

var votesPlugin = require('mongoose-plugin-votes');
var schema = Schema({...});
schema.plugin(votesPlugin[, OPTIONS]);

mongoose-plugin-votes~options

Kind: inner property of mongoose-plugin-votes

Param Type Default Description
[options] object
options.path string "votes" the path to create the propterty for storing votes.
options.options object property options to set (type will always be Array). (e.g. {select: false})
options.voteMethodName string "vote" the method name to set a vote.
options.unvoteMethodName string "unvote" the method name to unset a vote.
options.votes object
options.votes.ref string the reference model to use (e.g. {votes: {ref: 'ModelRefName'}})
options.votes.options object votes property options to set (type will always be String). (e.g. {votes: {options: {select: false}}})

mongoose-plugin-votes~vote(voter)

The vote method appends the passed in value to the votes path array

Kind: inner method of mongoose-plugin-votes

Param Type Description
voter * If using a reference pass in the ObjectId or the document

mongoose-plugin-votes~unvote(voter)

The unvote method removes the passed in value from the votes path array

Kind: inner method of mongoose-plugin-votes

Param Type Description
voter * If using a reference pass in the ObjectId or the document

Examples

With Strings

var votesPlugin = require('mongoose-plugin-votes');
var schema = Schema({foo: String});
schema.plugin(votesPlugin);
 
var Foo = mongoose.model('Foo', schema);
var foo = Foo(); // foo.votes --> []
foo.vote('candy'); // foo.votes --> ['candy']
foo.vote('candy'); // foo.votes --> ['candy']
foo.vote('ice cream'); // foo.votes --> ['candy', 'ice cream']
foo.unvote('candy'); // foo.votes --> ['ice cream']

With References

var votesPlugin = require('mongoose-plugin-votes');
var schema = Schema({foo: String});
schema.plugin(votesPlugin, {votes: {ref: 'UserModel'}});
 
var Foo = mongoose.model('Foo', schema);
var foo = Foo(); // foo.votes --> []
foo.vote(userA); // foo.votes --> [{_id: '507f191e810c19729de860ea'}]
foo.vote(userA.id); // foo.votes --> [{_id: '507f191e810c19729de860ea'}]
foo.vote(userB); // foo.votes --> [{_id: '507f191e810c19729de860ea'}, {_id: '507f191e810c19729de970fb'}]
foo.unvote(userA); // foo.votes --> [{_id: '507f191e810c19729de970fb'}]

License

Apache 2.0

Package Sidebar

Install

npm i mongoose-plugin-votes

Weekly Downloads

0

Version

1.2.3

License

Apache 2.0

Last publish

Collaborators

  • centralping-admin
  • jasoncust