Coder Social home page Coder Social logo

okoyl / get-it-ready Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pankajpatel/get-it-ready

0.0 2.0 0.0 24 KB

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

Home Page: https://runkit.com/pankaj/get-it-ready

License: MIT License

JavaScript 100.00%

get-it-ready's Introduction

get-it-ready

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

Code Climate Test Coverage Issue Count

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.

Let me wrap it for ya!


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

get-it-ready's People

Contributors

pankajpatel avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.