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

mongoose-restify

v1.0.3

Published

Restify mongo collections

Downloads

5

Readme

mongoose-restify

Mongoose-Restify is a restify for mongodb collections though mongoose.

Build Status

Use

We can overide methods listed in #methods and attache to expressjs router

var router = require('express').Router();
var API = require('mongoose-restify');
var Game = require('./model');

class GameAPI extends API {
    constructor(ops) {
        super(ops);
    }
    
    /**
	 * request start
	 */
    begin(req, res, next) {
        next();
    }
	
	/**
	 * request end but before send response
	 * */
    end(req, res, next) {
        next();
    }

	/**
	 * same as above for 
	 *  onBeforeSelect(req, filter, next) { next(null, filter); }
	 *  onAfterSelect(req, docs, next) { next(null, docs); }
	 *  onBeforeSelectOne(req, filter, next) { next(null, filter); }
	 * onAfterSelectOne(req, docs, next) { next(null, docs); }
	 *  onBeforeCount(req, filter, next) { next(null, filter); }
	 *  onAfterCount(req, docs, next) { next(null, docs); }
	 *  onBeforeCreate(req, body, next) { next(null, body); }
	 *  onAfterCreate(req, doc, numAffected, next) { next(null, doc); }
	 *  onBeforeUpdate(req, doc, data, next) { next(null, doc, data); }
	 *  onAfterUpdate(req, doc, numAffected, next) { next(null, doc); }
	 *  onBeforePartialUpdate(req, doc, data, next) { next(null, doc, data); }
	 *  onAfterPartialUpdate(req, doc, numAffected, next) { next(null, doc); }
	 *  onBeforeDelete(req, doc, next) { next(null, doc); }
	 *  onAfterDelete(req, doc, next) { next(null, doc); }
     *  onQueryPipe(req, queryPipe, next) { next(null, queryPipe); }
     * */
}

var gameApi = new GameAPI({
    path: '/games',
    model: Game
});

gameApi.install(router);

API Options Fields

path

path - path to api


    var Game = mongoose.model('Game');

    var GameApi = new API({
        path: '/games',
        model: Game
    });

model

Mongoose model


    var Game = mongoose.model('Game');

    var GameApi = new API({
        path: '/games',
        model: Game
    });

qFields

Allowed query fields. qFields is array. If you spesify query fields, it will be mapped to q query parameter in the request(query string). The request to the mongodb would be made through RegExp. It useful for autocomplition or some kind of serch.


    var Game = mongoose.model('Game');

    var GameApi = new API({
        path: '/games',
        model: Game,
        // for example it will create: { developer: /ab/i }
        qFields: ['developer']
    });

sFields

Allowed query fields to select. fields is string and you have to spesify the fields with space( ) seperator, otherwise will be allowed all fields. fields will be mapped to fields query parameter in the request(query string) and return only selected fields or uses defaults.


    var Game = mongoose.model('Game');

    var GameApi = new API({
        path: '/games',
        model: Game,
        sFields: 'name developer released'
    });

sort

Default sort option. sort is object and you have to spesify it as to mongoose sort option. By default null. You can also override default sort option with query string parameter named sort

    
    var Game = mongoose.model('Game');

    var GameApi = new API({
        path: '/games',
        model: Game,
        /*
            request `/api/games?sort[name]=1` will override default sort options
        */
        sort: { released:-1 }
    });

partialOps

Allowed mongo partial update operators. $inc, $mul, $setOnInsert, $set, $min, $max, $currentDate, $addToSet, $pop, $pullAll, $pull, $push and $bit allowed by default to overried those spesify this parameter as string array.

queryPipe

Default query pipe. queryPipe should be function with query argument. You can use this option to spesify default fields population.

    
    var api = new API({
        path: '/comments',
        model: Comment,
        queryPipe: function (query) {
            return query.populate('user', 'name avatar');
        }
    });

Methods

The methods listed bellow are allowed to overide.

begin

Request start.

    class CommentAPI extends API{
        begin: function (req, res, next) { 
            // todo some logic here
            next(); 
        }
    };
    
    var api = new CommentAPI({
        path: '/comments',
        model: Comment
    });

end

Request end

    class CommentAPI extends API{
         end: function (req, res, next) { 
            next(); 
        }
    };
    
    var api = new CommentAPI({
        path: '/comments',
        model: Comment
    });

onBeforeSelect

Fires only for select documents with GET request method.

    
    class CommentAPI extends API{
        onBeforeSelect: function (req, filter, next) { 
            /*
             todo some logic here
             filter is obect which hold 
              { query - select condition, 
                fields - select fields, 
                sort  - sort fields
                limit - number of documents to fetch, 
                skip - number of documents to skip
                lean - mongoose options
            */
            
            next(null, filter); 
        }
    };
    
    var api = new CommentAPI({
        path: '/comments',
        model: Comment
    });

onAfterSelect

Fires only for select documents with GET request method after onBeforeSelect method.


    class CommentAPI extends API{
        onAfterSelect: function (req, docs, next) { 
            // here is some logic
            next(null, docs); 
        }
    };
    
    var api = new CommentAPI({
        path: '/comments',
        model: Comment
    });

onBeforeSelectOne

function (req, filter, next) { next(null, filter); };

onAfterSelectOne

function (req, docs, next) { next(null, docs); };

onBeforeCount

function (req, filter, next) { next(null, filter); };

onAfterCount

function (req, docs, next) { next(null, docs); };

onBeforeCreate

function (req, body, next) { next(null, body); };

onAfterCreate

function (req, doc, numAffected, next) { next(null, doc); };

onBeforeUpdate

function (req, doc, data, next) { next(null, doc, data); };

onAfterUpdate

function (req, doc, numAffected, next) { next(null, doc); };

onBeforePartialUpdate

function (req, doc, data, next) { next(null, doc, data); };

onAfterPartialUpdate

function (req, doc, numAffected, next) { next(null, doc); };

onBeforeDelete

function (req, doc, next) { next(null, doc); };

onAfterDelete

function (req, doc, next) { next(null, doc); };

onQueryPipe

function (req, queryPipe, next) { next(null, queryPipe); };

onError

function (req, res, err) {
    if (err.httpStatus) {
        return res.json(err.httpStatus, { message: err.message });
    }
    
    // send internal server error
    return res.json(500, { message: 'internal server error' });
};