A simple roles plugin for Mongoose.js models.
- Use npm to install by running
npm install --save rolo
in your project - Register rolo as a plugin in your Mongoose models:
// ./models/user.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var rolo = require('rolo');
var schema = new Schema({
name: String
});
schema.plugin(rolo);
module.exports = mongoose.model('User', schema);
- Use Rolo's methods to manage and query your model's roles.
var User = require('./models/user');
User.find({name: 'Tyler'}, function (err, user) {
if (err) throw err;
// Assign a role to a model
user.addRole('admin', function(err, added) {
if (err) throw err;
if (added) {
// Role was added successfully, do stuff
}
});
// Query a model for a specific role
if ( user.hasRole('admin') ) {
// user is an admin
}
// etc...
})
Copyright (c) 2014, Tyler Hughes [email protected]
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.