gen-express
v1.0.3
Published
Easy to use cli tool to generate files for your express API
Downloads
2
Readme
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
- controllers
Options
This cli has 4 options to specify what to do. You can see this with:
$ gen-express --help
| Flag | Description | Default |
| :---: | :--- | :--- |
| -c, --create
| Type of file to create (entity, controller, router, middleware, service, validator, repository | entity |
| -e, --entity
| Name of the file/entity | index |
| -s, --schema
| Defines the schema to validate requests | {id?:number} |
| -f, --folder
| Root 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()