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

hunt-mongoose-rest

v0.0.1

Published

HuntJS RESTfull API for mongoosejs models

Downloads

4

Readme

Hunt-Mongo-REST

Build Status

Abstract Mongoose to REST interface CRUD. In MVC paradigm this module is a way to generate view (as JSON object) and controller for this particular model. It is worth mention, that access control checks are performed via static Active Record methods and instance Active Record methods mongoose model against the current authenticated user. It is nodejs implementation of awesome module of (http://www.symfony-project.org/plugins/sfDoctrineRestGeneratorPlugin) This work is inspired by http://www.restapitutorial.com/. This module is intended to work with HuntJS framework of v 0.1.x branch.

Usage


    var Hunt = require('hunt'),
      hrw = require('hunt-mongo-rest'),
      hunt = Hunt({
        'disableCsrf': true,
        'huntKey': true,
        'mongoUrl': 'mongodb://localhost/hrw_dev'
      });
      
    hunt.extendModel('Articles', function(core){
      var ArticleSchema = new core.mongoose.Schema({
        'name': { type: String, unique: true },
        'content': String,
        'author': { type: core.mongoose.Schema.Types.ObjectId, ref: 'User' }
      });
    
      ArticleSchema.index({
        'name': 1,
        'author': 1
      });
    
    //some statics method, corresponding to Active Record Collection
      ArticleSchema.statics.doSmth = function (user, payload, callback) {
        callback(null, {
          'user': user,
          'body': payload
        });
      };
    
    //some instance method, corresponding to this particular item of Active Record collection
      ArticleSchema.methods.doSmth = function (user, payload, callback) {
        callback(null, {
          'article': this,
          'user': user,
          'body': payload
        });
      };
    
    //ACL check for what fields can user list and filter     
      ArticleSchema.statics.canCreate = function (user, callback) {
        if (user) { 
    //only authorized user can create new article, the setter of `author` with current user's id is set
          callback(null, true, 'author');
        } else {
          callback(null, false);
        }
      };
    
    //ACL check for what fields can user list and filter 
      ArticleSchema.statics.listFilter = function (user, callback) {
        if (user) {
          if (user.root) {
    //root can list all documents!
            callback(null, {}, ['id', 'name', 'content', 'author'], ['author']);
          } else {
    //non root user can see documents, where he/she is an owner
            callback(null, {'author': user._id}, ['id', 'name', 'content']); 
          }
        } else {
    //non authorized user cannot list anything!
          callback(null, false); 
        }
      };

    //ACL check for readable fields
      ArticleSchema.methods.canRead = function (user, callback) {
        if (user) {
          if (user.root) {
    //root can list all documents and all document fields, with populating author
            callback(null, true, ['id', 'name', 'content', 'author'], ['author']);
          } else {
    //non root user can see documents, where he/she is an owner
            callback(null, (this.author == user.id), ['id', 'name', 'content']);
          }
        } else {
          callback(null, false); //non authorized user cannot read anything!
        }
      };
      
    //ACL check for ability to update some fields in this current document    
      ArticleSchema.methods.canUpdate = function (user, callback) {
        if (user) {
          if (user.root) {
    //root can edit all documents and all document fields
            callback(null, true, ['name', 'content', 'author']);
          } else {
    //non root user can edit `name` and `content` of
    //documents, where he/she is an owner
            callback(null, this.author == user.id, ['name', 'content']);
          }
        } else {
          callback(null, false); //non authorized user cannot edit anything!
        }
      };

    //ACL check for ability to delete this particular document
      ArticleSchema.methods.canDelete = function (user, callback) {
        var document = this;
        if (user) {
          if (user.root) {
    //root can delete every document
            callback(null, true); 
          } else {
    //non root user can delete documents, where he/she is an owner
            callback(null, document.author == user.id);
          }
        } else {
          callback(null, false); //non authorized user cannot edit anything!
        }
      };
      
      //some validations      
      ArticleSchema.path('author').validate(function (value, respond) {
        return core.model.User.findById(value, function (error, authorFound) {
          if (error) {
            throw error;
          } else {
            respond(authorFound ? true : false);
          }
        });
      }, 'Unable to find Author!');
        
      //this step is very important - bind mongoose model to current mongo database connection
      // and assign it to collection in mongo database
      return core.mongoConnection.model('Article', ArticleSchema);
    });

    //do some magic
    
    hrw(hunt, { 
      'mountPount' : '/api/v1/articles',
      'modelName': 'Article',
      'statics': ['doSmth'],
      'methods':['doSmth']
    });
    
    Hunt.startWebServer();
    

Configuration parameters