Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hapi-forest

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hapi-forest

A hapi plugin to generate routes based on mongoose models

  • 0.1.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7
increased by40%
Maintainers
1
Weekly downloads
 
Created
Source

hapi-forest

package on npm Travis branch node 6+ required GitHub license

A FOREST

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

You can already play around with hapi-forest, but this is missing:

  • RESTfull PUT route
  • A lot more test coverage
  • More ways to customize the generated queries.

Plugin usage

// register hapi-forest
server.register({
  register: require('hapi-forest'),
  options: {
    bootstrap: [ require('./models/user-model') ]
  }
});

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

Handlers are dynamically generated based on your route definition.

GET, POST, PATCH & PUT routes will generate an according REST handler. You can also overwrite the handlers by setting the type option for every handler.

The model option is required for every handler.

getOne

Returns all documents from the specified model.

  • Only custom fields can be selected using select.
  • you can select a custom property idKey to be used for lookup instead of _id. The path key should be the same name.
server.route({
  method: 'GET',
  path: '/users/{name}', // {name} should be the same as the idKey
  handler: {
    forest: {
      model: User,
      idKey: 'name', // custom mongoose field to use as the id
    }
  }
});


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)
server.route({
  method: 'GET',
  path: '/users',
  handler: {
    forest: {
      model: User,
      select: 'firstName group lastName birthday',
    }
  }
});

post

Creates a new document.

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.
  • you can specify an idKey. In that case the _id field will be autogenerated and not set by the user.
server.route({
  method: 'PUT',
  path: '/users/{name}',
  handler: {
    forest: {
      model: User,
      allowUpsert: true,
      idKey: 'name',
    }
  }
});

delete

Deletes a document.

  • you can specify an idKey that will be used for lookup instead of _id.
server.route({
  method: 'DELETE',
  path: '/users/{name}',
  handler: {
    forest: {
      model: User,
      idKey: 'name',
    }
  }
});

Keywords

FAQs

Package last updated on 14 Nov 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc