1.2.3 • Published 9 years ago

mongoose-plugin-votes v1.2.3

Weekly downloads
4
License
Apache 2.0
Repository
github
Last release
9 years ago

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

ParamTypeDefaultDescription
optionsobject
options.pathstring"votes"the path to create the propterty for storing votes.
options.optionsobjectproperty options to set (type will always be Array). (e.g. {select: false})
options.voteMethodNamestring"vote"the method name to set a vote.
options.unvoteMethodNamestring"unvote"the method name to unset a vote.
options.votesobject
options.votes.refstringthe reference model to use (e.g. {votes: {ref: 'ModelRefName'}})
options.votes.optionsobjectvotes 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

ParamTypeDescription
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

ParamTypeDescription
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