Skip to content

Repository files navigation

CRUD Builder CLI

npm version npm downloads MIT License Node.js Version Build Status

A powerful, customizable, and developer-friendly Command Line Interface (CLI) tool designed to automate the scaffolding of Node.js/Express projects and Mongoose-backed CRUD (Create, Read, Update, Delete) components.


🚀 Features

  • 📂 Project Scaffolding: Initialize a complete Express.js boilerplate structure in one command.
  • ⚡ Custom Template Engine: Uses clean template files (templates/) instead of fragile inline code strings.
  • 🗃️ Smart Code Generators: Generates Mongoose models, controller logic, and Express routing files.
  • 🧪 Field-Aware Validation: Pass field schemas through the CLI to automatically generate database models and request body validators.
  • 🛠️ Extensible Architecture: Built on Commander.js for a robust command structure.

📦 Installation

To install the CLI globally via npm:

npm install -g crud-builder-cli

Alternatively, to run/link it locally from source:

  1. Clone the repository:

    git clone https://github.com/senuda-d/CRUD-generator-CLI.git
    cd CRUD-generator-CLI
  2. Install dependencies:

    npm install
  3. Link the package globally:

    npm link

Now you can use the global command crud-builder anywhere on your system!


🛠️ Usage Guide

The CLI provides two main commands: init and generate.

1. Initialize a New Project

To scaffold a new Node/Express application structure:

crud-builder init my-awesome-app

This command creates a new folder my-awesome-app with the following structure:

my-awesome-app/
├── src/
│   ├── app.js             # Starter Express application
│   ├── controllers/       # Folder for controllers
│   ├── models/            # Folder for Mongoose models
│   └── routes/            # Folder for Express routes
├── .gitignore
└── package.json

2. Generate CRUD Components

Generate modular CRUD components for a database entity. You must navigate to your project directory and run:

crud-builder generate <EntityName> [options]

CLI Options & Flags

Flag Description
--model Generates a Mongoose schema model.
--controller Generates a controller with standard CRUD request handlers.
--route Generates REST routes mapping HTTP verbs to controllers.
--crud Shortcut to generate model, controller, and routes (same as using all three flags).
--fields <fields> Comma-separated list of field names and types (e.g. title:string,price:number).

Examples

  • Generate a basic model and controller with validation fields:

    crud-builder generate Task --model --controller --fields "title:string,completed:boolean,dueDate:date"
  • Generate complete CRUD routes, models, and controllers in one go:

    crud-builder generate User --crud --fields "email:string,age:number,active:boolean"

📁 Repository Structure

CRUD-generator-CLI/
├── bin/
│   └── cli.js            # CLI entrypoint
├── commands/
│   ├── generate.js       # Action handler for component generation
│   └── init.js           # Action handler for project initialization
├── generators/
│   ├── controllerGenerator.js
│   ├── modelGenerator.js
│   ├── projectGenerator.js
│   └── routeGenerator.js
├── templates/            # Source templates used by generators
│   ├── app.template.js
│   ├── controller.template.js
│   ├── model.template.js
│   └── route.template.js
├── utils/
│   ├── fileWriter.js
│   ├── nameHelper.js
│   └── templateEngine.js # Custom regex-based replacement helper
└── tests/                # Automated tests
    ├── generators.test.js
    └── templateEngine.test.js

🧪 Running Tests

The project utilizes the native Node.js test runner (introduced in Node v18+), requiring no third-party test runners or heavy testing frameworks.

To execute the test suite:

npm test

This runs both unit tests for the template engine and integration tests for the generators.


🤝 Contributing

Contributions are what make the open-source community an amazing place to learn, inspire, and create.

  1. Fork the Project.
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature).
  3. Commit your changes (git commit -m 'feat: add some AmazingFeature').
  4. Push to the Branch (git push origin feature/AmazingFeature).
  5. Open a Pull Request.

📄 License

Distributed under the MIT License. See LICENSE for more information.