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

get-it-ready

v0.1.6

Published

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

Downloads

13

Readme

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