1.0.3 • Published 4 years ago

gen-express v1.0.3

Weekly downloads
8
License
MIT
Repository
github
Last release
4 years ago

express-cli

Easy to use cli tool to generate files for your express API

Instalation

Via npm

$ npm i -g gen-express

Usage

Generate a full entity file structure

$ gen-express -c entity -e person -s '{id:number,name:string}' -folder code

This will generate a file structure like this:

  • code
    • controllers
      • person.controller.js
    • middlewares
      • person.middleware.js
    • repositories
      • person.repository.js
    • routers
      • person.router.js
    • services
      • person.service.js
    • validators
      • person.validator.js

Options

This cli has 4 options to specify what to do. You can see this with:

$ gen-express --help
FlagDescriptionDefault
-c, --createType of file to create (entity, controller, router, middleware, service, validator, repositoryentity
-e, --entityName of the file/entityindex
-s, --schemaDefines the schema to validate requests{id?:number}
-f, --folderRoot folder to put all the files./ (current dir)

Schema

Generates a Joi schema validator, i.e.:

$ gen-express -c user -e dog -s '{id:number, name:string, password:string, roleId:number, phone?: string}'

Will generete a file names user.validator.js with the folowing contents:

'use strict'
const Joi = require('@hapi/joi')

const joiObject = {
	id: Joi.number().required(),
	name: Joi.string().required(),
	password: Joi.string().required(),
	roleId: Joi.number().required(),
	phone: Joi.string().optional(),
}
const id = { id: Joi.number().positive().required() }
class UserValidator {
	get() {
		return Joi.object()
			.keys({
				id: Joi.number().required(),
				name: Joi.string().required(),
				password: Joi.string().required(),
				roleId: Joi.number().required(),
				phone: Joi.string().optional(),
			})
			.options({ allowUnknown: true, stripUnknown: true })
	}

	create() {
		return Joi.object()
			.keys({
				id: Joi.number().required(),
				name: Joi.string().required(),
				password: Joi.string().required(),
				roleId: Joi.number().required(),
				phone: Joi.string().optional(),
			})
			.options({ allowUnknown: true, stripUnknown: true })
	}

	update() {
		return Joi.object()
			.keys({
				id: Joi.number().required(),
				name: Joi.string().required(),
				password: Joi.string().required(),
				roleId: Joi.number().required(),
				phone: Joi.string().optional(),
				...id,
			})
			.options({ allowUnknown: true, stripUnknown: true })
	}

	delete() {
		return Joi.object()
			.keys(id)
			.options({ allowUnknown: true, stripUnknown: true })
	}
}

module.exports = new UserValidator()
1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago