@yoirchalk/express-mongoose-cli-tool
v1.2.5
Published
A CLI tool to generate boilerplate code for express routers and mongoose models with joi validation.
Downloads
9
Readme
express-boilerplate-cli-tool
A CLI tool to generate boilerplate code for express routers and mongoose models with joi validation.
Contents
Not sure why links aren't working on npmjs.com. They work in GitHub
Features
- Automatically generates Mongoose models with Joi validation.
- Generates Express routes.
- Handles both singular and plural forms.
- Places generated files in appropriate models and routes directories.
- Creates models or routes folders in the root directory if they do not exist.
- New features in 1.1.0
- Ability to set file as template for boilerplate using -c flag
- Next update
-rtag to restore default boilerplate template
- Future updates
- More comprehensive native support for different express stacks
- mySQL
- postgreSQL
- Database initialization boilerplate code
- Ability to set stack flag once and generate boileplate based on chosen stack every time without needing to pass flag.
- More comprehensive native support for different express stacks
Installation
Run the following command to install as a dev dependency:
npm install @yoirchalk/express-mongoose-cli-tool --save-devUsage
Generating boilerplate code
This package provides a CLI tool for generating boilerplate code for your project. The command syntax is:
generate_bp <type> <name>where:
<type>is the type of boilerplate you want to generate. It can bemodelfor generating a mongoose model with joi validationroutefor generating an express routebothto generate both a route and a model
nameis the name of the model or route you want to generate (e.g 'User', 'Products')
The program is case-insensitive and automatically handles both singular and plural forms.
The program places files in an approprite models/routes folder and will create the folder in root folder if it doesn't exist.
Usage Examples
- generate model
generate_bp model Appartment- generate route
generate_bp route houses- generate both a route and a model
generate_bp both Locations- view help
generate_bp -h
generate_bp --helpSetting file as boilerplate template
Command syntax is
generate_bp <type> -c <file>where
<type>is type of boilerplate to set either;<model>or<route>
<file>is the .js file which contains a constant called template which is set to a template string which contains template boilerplate code
Usage example
generate_pb route -c template.jsand an example of template.js content:
let template = `
const express = require('express')
const mysql = require('mysql2')
app.get('/${pluralName}', (req, res) => {
const query = 'SELECT * FROM ${pluralName}';
db.query(query, (err, ${pluralName}$) => {
if (err) {
console.error('Error fetching ${pluralName}:', err);
res.status(500).json({ error: 'Failed to fetch ${pluralName}' });
return;
}
res.send(${pluralName});
});
});
`Make sure to include necessary import statements.
Constant must end with semicolon ;
Must use let keyword and not const
Contribution and Feedback
Contributions, bug reports and suggestions are very welcome! Here's how you can help improve this package;
Reporting bugs and suggesting features
If you find a bug or have an idea for an improvment please
- open an issue in git hub or
- contact me directly
- be sure to add package name in subject line. No need for @yoirchalk
- if link doesnt work right click and click on 'copy email adress'
Contributing via GitHub
- Visit repository at https://github.com/yoirchalk1995/express-boilerplate-cli-tool
- Fork the repository by clicking fork at top right of page
- Clone youre fork
git clone https://github.com/YOUR_USERNAME/YOUR_FORK.git - Create a new branch for your feature or fix
git checkout -b feature-name - Make changes and commit them
git commit -m "Add new feature" - Push your changes
git push origin feature-name - Submit a push request. I'll review it asap
