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-forest

v0.9.0

Published

A hapi plugin to generate routes based on mongoose models

Downloads

45

Readme

hapi-forest

package on npm Travis branch node 8+ required GitHub license

A FOREST

Provides REST handlers for mongoose models. Can also generate ready to use routes, for fast bootstrapping.

Quickstart

  1. Install it
npm i --save hapi-forest # or yarn add hapi-forest if you prefer
  1. Register the plugin.
// register hapi-forest
server.register({
  register: require('hapi-forest'),
  options: {
    // add your models here for auto route generation
    bootstrap: [ require('./models/user-model') ]
  }
});
  1. Test your dynamically generated REST endpoints. hapi-swagger works nicely with hapi-forest.

Take a look at the example directory for a full example.

Plugin options

  • bootstrap: [ MongooseModel, MongooseModel, … ]

    Will generate ready to use CRUD routes. hapi-forest will attempt to generate a basic joi schema based on the model.

Handlers

You can use the forest handler and define your own routes, instead of auto-generating them. This is useful if you need more control over your endpoints or want custom validation.

The forest handler behaves differently based on your route definition. GET, POST, PATCH & PUT are supported.

URL parameter

For routes like GET /collection/{name}, the first URL parameter (name in this case) is used as the "id". It will be used in the condition of the mongoose query.

{id} will translate to _id for convenience.

Generic Options for all methods

Option | Description :------------------ | :-------------------------------------------------------------------------------------- model | required – The mongoose Model for this route. type | Overwrites the auto selected handler. Can be one of getOne, getAll, post, put, delete preQuery | A Function that gets passed the current mongoose query, that was generated by forest. transformResponse | A Function that gets passed the response. You have to return the modified response.

getOne

Returns all documents from the specified model.

  • Only custom fields can be selected using select.
  • the first parameter in the path (name in this example) will be used to query
server.route({
  method: 'GET',
  path: '/users/{name}',
  handler: {
    forest: {
      model: User,
    }
  }
});

getAll

Returns all documents from the specified model. The result will be streamed.

  • Only custom fields can be selected using select.
  • The filterByQuery option allows basic filtering of the results by sending a query with the request. (?group=nodejs&role=developer)
    • the query that results from the user input can be modified with the transformQuery option. You can specify a function that has to return the updated query.
  • The allowLimit option gives the client the ability to limit the number of results by adding $limit=x to the query parameters.
server.route({
  method: 'GET',
  path: '/users',
  handler: {
    forest: {
      model: User,
      select: 'firstName group lastName birthday',
    }
  }
});

post

Creates a new document.

  • skipMongooseHooks will use a faster mongoose create implementation, skipping all hooks.
server.route({
  method: 'POST',
  path: '/users',
  handler: {
    forest: {
      model: User
    }
  }
});

put

Updates an existing document or creates a new document if it does not exist. For now an update will not overwrite and existing document but only update it, like patch does.

  • you can disallow creating new documents with allowUpsert: false. PUT will behave like PATCH in that case.
server.route({
  method: 'PUT',
  path: '/users/{name}',
  handler: {
    forest: {
      model: User,
      allowUpsert: true,
    }
  }
});

delete

Deletes a document.

  • the first parameter in the path (name in this example) will be used to query
server.route({
  method: 'DELETE',
  path: '/users/{name}',
  handler: {
    forest: {
      model: User,
    }
  }
});