napi-mvc
v2.0.2
Published
RAD tool for rapid API development - Auto-generate CRUD routes, models, and controllers from MySQL database
Maintainers
Readme
🚀 Napi-MVC - RAD Tool for Express APIs
A powerful RAD (Rapid Application Development) tool that automatically generates CRUD routes, models, and controllers from your MySQL database schema.
✨ Features
- Auto-generate CRUD — REST endpoints automatically created from database tables
- Smart Detection — Automatically detects foreign keys and generates validation
- Swagger Integration — Auto-generated OpenAPI/Swagger documentation
- Role-Based Auth — Built-in JWT authentication with role-based access control
- CLI Tool — Easy command-line interface for generation
- Zero Config — Works out of the box with sensible defaults
📦 Installation
npm install napi-mvcOr install globally to use the CLI anywhere:
npm install -g napi-mvc🚀 Quick Start
1. Use with CLI
# Generate CRUD for a table
napi-mvc generate route users
# Register the route in your app.js
napi-mvc register route users2. Use as a Module
const napiMvc = require('napi-mvc');
// Generate routes programmatically
napiMvc.generateRoute('users', {
host: 'localhost',
user: 'root',
database: 'myapp'
});📝 Usage
Prerequisites
- MySQL database with tables defined
- Express.js application setup
- dotenv configuration
Setup
# Create your table
CREATE TABLE products (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
price DECIMAL(10, 2),
description TEXT,
user_id INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id)
);
# Generate CRUD
napi-mvc generate route product
# Register in your app.js
napi-mvc register route product
# Restart server
npm startWhat Gets Generated
✅ Route (routes/product.routes.js)
- GET
/api/v1/products— List all - GET
/api/v1/products/:id— Get one - POST
/api/v1/products— Create - PUT
/api/v1/products/:id— Update - DELETE
/api/v1/products/:id— Delete - Auto-generated Swagger documentation
✅ Model (models/product.model.js)
findAll()— Get all recordsfindById(id)— Get by IDcreate(data)— Insert new recordupdate(id, data)— Update recorddelete(id)— Delete record- Auto FK Validation — Validates foreign key existence
- Auto User ID — Auto-fills user_id from authenticated user
✅ Controller (controllers/product.controller.js)
getAll(req, res)— Handle GET /getById(req, res)— Handle GET /:idcreate(req, res)— Handle POST /update(req, res)— Handle PUT /:iddelete(req, res)— Handle DELETE /:id- Error handling — Built-in error responses
- Auth passing — Passes req.user.id to model
🔐 Authentication & Authorization
Napi-MVC integrates with JWT authentication:
const authMiddleware = require('./middlewares/auth');
const authorize = require('./middlewares/authorize');
// Apply auth to routes
router.post('/', authMiddleware, authorize('admin'), Controller.create);Supported Roles
user— Regular useradmin— Administrator
📚 Advanced Usage
Exclude Fields from CRUD
The generator automatically excludes these fields:
id— Primary keycreated_at— Auto-timestampupdated_at— Auto-timestampuser_id— Auto-filled from auth*_datefields — Auto-set to current date
Custom Configuration
Create a .napi-mvc.json in your project root:
{
"host": "localhost",
"user": "root",
"password": "secret",
"database": "myapp",
"exclude": ["deleted_at", "internal_id"]
}Use in Tests
const { generateRoute, generateModel } = require('napi-mvc');
describe('Product CRUD', () => {
test('should generate valid model', async () => {
const model = await generateModel('products');
expect(model).toHaveProperty('create');
expect(model).toHaveProperty('findAll');
});
});🛠️ Development
# Clone the repo
git clone https://github.com/yourusername/napi-mvc.git
cd napi-mvc
# Install dependencies
npm install
# Run tests
npm test
# Publish to npm
npm publish📋 Command Reference
# Generate route + model + controller
napi-mvc generate route <tableName>
# Register route in app.js
napi-mvc register route <tableName>
# Show help
napi-mvc help
# Show version
napi-mvc --version🐛 Troubleshooting
Table not found
# Make sure the table exists in MySQL
SHOW TABLES;
# Then try generating again
napi-mvc generate route tablenameModule not found
# Make sure you've registered the route
napi-mvc register route tablename
# Then restart your server
npm startPort already in use
# Kill the process using port 3000
# On Windows:
netstat -ano | findstr :3000
taskkill /PID <PID> /F
# On macOS/Linux:
lsof -ti :3000 | xargs kill -9📄 License
MIT
👤 Author
Your Name [email protected]
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
📞 Support
Made with ❤️ for rapid API development
