New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

get-it-ready

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

get-it-ready

Generate Joi Validation, Mongoose Model and basic API endpoint routes for hapijs

  • 0.1.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-57.14%
Maintainers
1
Weekly downloads
 
Created
Source

get-it-ready

Generate Joi Validation, Mongoose Model and basic API endpoint routes for hapijs

Inspiration

While doing hapijs app with mongoose, there was a problem with Mongoose schemas and Joi validations; they were most of the times same. And the REST API was surely going to have few predefined routes; why not have a constructor to do all this at once.

Once constructor will bridge the problem of multiple configurations for Mongoose and Joi. And also quicky return the controller methods and Routes to easily plug into Hapijs app.

Description

This lib can be used to generate the schema, model, necessary controllers and routes that can be directly plugged into Hapijs app.

Few restrictions:

  • Mongoose models and schemas can/will be used
  • Output controllers and routes are for Hapijs
  • Controllers are named as
    • getAll
    • getOne
    • create
    • update
    • remove
  • Routes
    • GET all
    • GET one
    • POST one
    • PUT one
    • DELETE one

Usage

Automatic

For automatic/quick usage, the method will need all following four parameters.

  • object schemaDefinitionObject This object is a mixture of Mongoose Schema Definition and Joi validation object. The keys which you wanna put in Joi validation, create a joi named key in the value object
  • string routeBaseName Route base in plurals
  • string modelName Model name
  • string singularRouteName Route base in singular

It returns a Collection object containing ingredients of REST which are ready to be plugged to hapijs

Example
var Joi = require('joi');
var getItReady = require('get-it-ready');

var personDefinition = {
  name: { 
    type: String, 
    required: true 
  },
  firstName: { 
    type: String, 
    required: true, 
    joi: Joi.string() 
  },
  lastName: { 
    type: String, 
    required: true, 
    joi: Joi.string() 
  },
  createdOn: { 
    type: Date, 
    required: false, 
    default: Date.now, 
    joi: Joi.date() 
  }
};

var person = getItReady(personDefinition, 'persons', 'Person', 'person');

console.log(person.validations, person.schema, person.model, person.controller, person.routes);

See above code in action at https://runkit.com/pankaj/get-it-ready

Manual

For manual opration of this lib, the order of execution of methods is very important. The order of execution should be

  • separateJoiValidationObject
    • @param {object} config The mixture of Schema Config and Joi config object
    • @return {object}
  • getSchema
    • @param {object} schema definition object
    • @return {object} mongoose schema
  • getModel
    • @param {string} modelName The Mongoose Model name
    • @param {object} schema The Mongoose Schema object
    • @param {object} db The Mongoose DB conection object, if pased, use this otherwise use Mongoose. The connection object should be created with Mongoose.createConnection
    • @return {object} model The Mongoose model
  • getControllers
    • @param {object} model The Mongoose model object
    • @param {object} joiValidationObject The Joi validation objects
    • @return {object} object containing controller methods
  • getRoutes
    • @param {object} controllers The object containing controller methods
    • @param {string} routeBaseName The string which should be used for routebase
    • @param {string} singularRouteName The singular entity name for routes
    • @return {object} The routes object which can be plugged in hapijs or can be extended more
Example
var Joi = require('joi');
var getItReady = require('get-it-ready');

var personDefinition = {
  firstName: { 
    type: String, 
    required: true, 
    joi: Joi.string() 
  },
  lastName: { 
    type: String, 
    required: true, 
    joi: Joi.string() 
  },
  createdOn: { 
    type: Date, 
    required: false, 
    default: Date.now, 
    joi: Joi.date() 
  }
};

var validations = getItReady.separateJoiValidationObject(personDefination);
var schema      = getItReady.getSchema(validations.schema);
var model       = getItReady.getModel(modelName, schema);
var controllers = getItReady.getControllers(model, validations);
var routes      = getItReady.getRoutes(controllers, routeBaseName, singularRouteName);

console.log(validations, schema, model, controller, routes);

Built With

  • Joi - For repharsing the validatons on POST and PUT requests
  • Boom - Errors of Hapijs
  • Mongoose - MongoDB Schema and Models for routes

Keywords

FAQs

Package last updated on 20 Jan 2017

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