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

joi-sequelize-support

v0.3.0

Published

A lib to automaticaly create joi validation schemas from sequelize models.

Downloads

1

Readme

joi-sequelize

A lib to automaticaly create joi validation schemas from sequelize models.

Build Status

A lots of Hapi projects uses Sequelize to handle database conection, data modeling and manipulation, and Joi to validate its requests and responses. In this case is common to use quite the same schema for both libraries. Hence that it is a fertile scenario to create inconsistency between both schemas, that will be likely rearranged every time the database models change. So why not use the database to auto generate joi schemas, and remove the unnecessary validation in especifica routes. This is the main idea behind joi-sequelize.

Usage

Model example

'use strict'; // jshint ignore:line

var config = require('../../config/config'),
    bcrypt = require('bcrypt'),
    salt   = config.saltGen;

module.exports = function(sequelize, DataTypes) {
  var User = sequelize.define('User', {
    id: {
      allowNull: false, /* will generate a .required() on joi schema */
      autoIncrement: true,
      primaryKey: true,
      type: DataTypes.INTEGER,
      description: 'User`s identifier' /* will generate a .description() on joi schema tha can be used by swagger */
    },
    firstname: {
      type: DataTypes.STRING(64), /* will generate .string().max(64) */
      allowNull: false, 
      description: 'User`s first name'
    },
    lastname: {
      type: DataTypes.STRING(64), /* will generate .string().max(64) */
      allowNull: false,
      description: 'User`s last name'
    },
    email: {
      type: DataTypes.STRING(64), /* will generate .string().max(64) */
      allowNull: false,
      description: 'User`s email'
    },
    password: {
      type: DataTypes.STRING, /* will generate .string() */
      allowNull: false,
      description: 'User`s password'
    },
    role: {
      type: DataTypes.ENUM('admin', 'common user'), /* will generate .valid('admin', 'common user') */
      allowNull: false,
      description: 'User`s role'
    },
    active: {
      type: DataTypes.BOOLEAN, /* will generate .boolean() */
      allowNull: false
    }
  }, {
    classMethods: {
      associate: function(models) {
        // associations can be defined here
      }
    }
  });
  return User;
};

Loading models

'use strict';

var fs        = require('fs'),
    path      = require('path'),
    Sequelize = require('sequelize'),
    JoiSequelize = require('joi-sequelize'),
    basename  = path.basename(module.filename),
    env       = process.env.NODE_ENV || 'development',
    log       = (!process.env.LOG || process.env.LOG === 'false') ? false : true,
    config    = require(__dirname + '/../../config/database.json')[env],
    db,
    sequelize;

function init() {
  db = {};
  config.logging = (env === 'development' && log) ? console.log : false;

  if (config.use_env_variable) {
    sequelize = new Sequelize(process.env[config.use_env_variable]);
  } else {
    sequelize = new Sequelize(config.database, config.username, config.password, config);
  }

  db.sequelize = sequelize;
  db.Sequelize = Sequelize;
  db.JS = {};
  fs
    .readdirSync(__dirname)
    .filter(file => (
        (file.indexOf('.') !== 0) &&
        (file !== basename) &&
        (file.slice(-3) === '.js')
      )
    )
    .forEach(function (file) {
      var model = sequelize['import'](path.join(__dirname, file));
      db[model.name] = model;
      db.JS[model.name] = new JoiSequelize(require(path.join(__dirname, file)));
    });
    
  Object.keys(db).forEach(function (modelName) {
        if (db[modelName].associate) {
          db[modelName].associate(db);
        }
      });

  Object.keys(db).forEach(function (modelName) {
    if (db[modelName].addScopes) {
      db[modelName].addScopes(db);
    }

    if (db[modelName].addHooks) {
      db[modelName].addHooks(db);
    }
  });
  
  return db;
}

module.exports = db || init();

Apply joi-sequelize generated schemas on routes

'use strict';

const Hapi = require('hapi');
const db = require('./model');
const JS = db.JS;

const server = new Hapi.Server();
server.connection({ port: 3000 });

server.route({
  method:  'POST',
  path:    '/hello',
  handler: (request, reply) => reply(request.payload),
  config:  {
    validate: {
      payload: JS.User.joi()
    }
  }
});

server.start((err) => {
  if (err) throw err;
  console.log('Server running at:', server.info.uri);
});

Other functions

Omit

Return a joi object with all items except the ones passed as arguments

JS.User.omit('role', ...);

Pick

Return a joi object with all fields passed as arguments

JS.User.pick('active', ...);

Include

Return a joi object with like JS.User.joi() but with field picture of joi type .any()

JS.User.include({picture: joi.any()});

withRequired

Get joi objects that have property allowNull:false as required

JS.User.withRequired();

withRequiredOmit

Same as withRequired but omiting the fields passed as arguments

JS.User.withRequiredOmit('id', 'created_at', 'updated_at', 'deleted_at'); // useful on create routes payloads

withRequiredPick

Same as withRequired but omiting the fields passed as arguments

JS.User.withRequiredPick('id'); // useful on get, update, delete routes with id param