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

hapi-mongoose-handler

v0.0.5

Published

Generic handlers for quickly making RESTful APIs from mongoose schemas with hapi

Downloads

8

Readme

hapi-mongoose-handler Build Status

  • Easly turn your mongoose models into a RESTful API.
  • Converts mongoose validation errors to 402's with an appropriate error message
  • Trys to follow hapi's configuration centric stlye

Install

npm install hapi-mongoose-handler
Or
git clone https://github.com/craveprogramminginc/hapi-mongoose-handler && cd hapi-mongoose-handler
Then Build
coffee -o lib/ -c src/

Example

var Hapi    = require('hapi');

//fist we need to require this package :P
var Handler = require('hapi-mongoose-handler');

//2) we need a mongoose model
var SomeModel = require('path/to/model');

//3) we create a new generic handler with the prevous model
var SomeHandler = new Handler({
  model: SomeModel
});

//4) create some routes for hapi and use the method provided by the handler we created
var routes = [
  {
    method: "POST",
    path: "/something",
    config: {
      handler: SomeHandler.create(),
      auth: true
    }
  }, {
    //use the param _id to delete by
    method: "DELETE",
    path: "/something/{_id}",
    config: {
      handler: SomeHandler.delete(),
      auth: true
    }
  }, {
    //use the param _id + queryString to find by
    method: "GET",
    path: "/something/{_id}",
    config: {
      handler: SomeHandler.findOne()
    }
  }, {
    //uses the queryString querystring to search
    method: "GET",
    path: "/somethings/",
    config: {
      handler: SomeHandler.find()
    }
  }
];

Hapi.routes(routes);

Referance

new Handler([options])

Create a new instance of hapi-mongoose-handler.

Options

The following options are valid.

  • model - the mongoose model we want to create an API for
  • config
    • maxLimit - the max number of results to return
    • defaultOrder the order of the results if no order is specified in the query
  • fields - hash of fields that will be saved to the model. Used by create and update Each field can have the following option
    • validate - a function that is given the field value, params and request and returns a string if invalid else return null
    • transform - a function that is given the field value, params and request and returns the new value for the field
    • rename - a string that the field is renamed to before saving
    • function - if only a function is given it is use a the transform option
  • queries - the same as fields except used to query mongo. Used by update, delete, find and findOne. The values come from the querystring.
  • check - a function that is given the model and the request, return a true/false determining whether to modify the model, runs on update, delete
  • omit - an array of fields to omit
  • after - a function that runs after the model as been found or modified. It is given the results of the mongoose query and the request object. Whatever you return will be the response. You can use this to wrap or modify the results from mongo.

Methods

theses are the method attached to handler instance. Each of the function take a options hash with the same option as the contructor. This enables you to overload the option for each individual handler

  • create create a document using the payload from the request
  • find find documents using the querystring to search by
  • findOne find one document using the querystring ORed with request.param as the condition parameter
  • delete delete one document using the querystring ORed with request.param as the condition parameter

Events

Adds the following events to hapi's server object

  • create - emits an event on creation of a model that is the concatenation of create and the model name with the first letter capitilized. For example if you had a model name dog when you created a new dog the event would be createDog
  • update - emits an event on the modification of a model that is the same format as create
  • delete - emits an event on the deletation of a model that is the same format as create

Examples

Lets add some field to transfrom to the first example

//3) we create a new generic handler with the prevous model
var SomeHandler = new Handler({
  model: SomeModel,
  fields: {
      //someFieldVal comes from request.params.someField
      someField: function(someFieldVal, params, request){
        //return a new value for the field
        return "Cadaverously Quaint ";
      }
  }
});

Here is how you would add some extra validation

//3) we create a new generic handler with the prevous model
var SomeHandler = new Handler({
  model: SomeModel,
  fields: {
      //someFieldVal comes from request.params.someField
      someField:{
        transform: function(someFieldVal, params, request){
        //return a new value for the field
        return "Cadaverously Quaint";
        },
        //validation run after traform, so this will also return true
        validate: function(someFieldVal, params, request){
          //if false hapi will return a 401 error with a error message for someField
          return someFieldVal == "Cadaverously Quaint";
        }
      }
  }
});

If you want to overload an individual handler.

//3) we create a new generic handler with the prevous model
var SomeHandler = new Handler({
  model: someModel
});


var routes = [{
    method: "POST",
    path: "/something",
    config: {
      //create will now act on someThingElse instead of someModel 
      handler: SomeHandler.create({
        model: someThingElse,
     })
    }
}];

More Examples

Tests

Tests are written in mocha To run tests npm test

FAQs

So this shit is broken, now what?

please open an issue.

Can I have X ?

please open an issue.

uhmm its written in coffeesccript and that not 'what the gods intended'

Diversity is good, stop being a racist and join the pagan dance circle

Why am I using mongoose in the first place?

I don't know and thats beyond the scope of this project.