@mikemajesty/microservice-crud
v7.1.2
Published
Monorepo CLI
Readme
NestJS Microservice CRUD CLI
A powerful CLI tool to generate CRUD modules for NestJS microservices following Clean Architecture principles.
Features
- 🚀 Fast CRUD Generation: Create complete CRUD modules in seconds
- 🏗️ Clean Architecture: Follows Clean Architecture patterns with separation of concerns
- 🔄 Multiple Database Support: PostgreSQL (TypeORM) and MongoDB (Mongoose)
- 📦 Auto-Import: Automatically registers modules in the appropriate files
- 🧪 Test Ready: Generates complete test suites with Jest
- 🔒 Type Safe: Full TypeScript support with proper validation
- 🎯 Multiple Templates: Core, Module, Library, Infrastructure, and CRUD generators
Installation
npm install -g @mikemajesty/microservice-crudOr use directly with npx:
npx @mikemajesty/microservice-crudUsage
Navigate to your NestJS microservice project root directory and run:
microservice-crudOr using npm script:
npm run crudAvailable Templates
The CLI will prompt you to select a template:
- POSTGRES:CRUD - Complete CRUD with PostgreSQL/TypeORM
- MONGO:CRUD - Complete CRUD with MongoDB/Mongoose
- LIB - Library module (shared services)
- INFRA - Infrastructure module (adapters, external services)
- MODULE - Simple module (controller + module only)
- CORE - Core use case (single domain logic)
What Gets Generated
POSTGRES:CRUD / MONGO:CRUD
Creates a complete CRUD module with:
src/
├── core/
│ └── [name]/
│ ├── entity/
│ │ └── [name].ts # Domain entity with validation
│ ├── repository/
│ │ └── [name].ts # Repository interface
│ └── use-cases/
│ ├── [name]-create.ts # Create use case
│ ├── [name]-update.ts # Update use case
│ ├── [name]-delete.ts # Delete use case
│ ├── [name]-list.ts # List with pagination
│ ├── [name]-get-by-id.ts # Get by ID use case
│ └── __tests__/ # Complete test suite
│ ├── [name]-create.spec.ts
│ ├── [name]-update.spec.ts
│ ├── [name]-delete.spec.ts
│ ├── [name]-list.spec.ts
│ └── [name]-get-by-id.spec.ts
├── modules/
│ └── [name]/
│ ├── adapter.ts # Adapter interfaces
│ ├── controller.ts # REST controller with decorators
│ ├── module.ts # NestJS module
│ └── repository.ts # Repository implementation
├── infra/
│ └── database/
│ └── [postgres|mongo]/
│ └── schemas/
│ └── [name].ts # Database schema
└── docs/
└── src/
└── modules/
└── [name]/
├── controller.tsp # TypeSpec API routes
├── model.tsp # TypeSpec models/DTOs
└── exception.tsp # TypeSpec error definitionsFeatures:
- ✅ Complete CRUD operations (Create, Read, Update, Delete, List)
- ✅ Input validation with Zod schemas
- ✅ Pagination support
- ✅ Search and filters
- ✅ Soft delete support
- ✅ Permission-based access control
- ✅ Swagger documentation ready
- ✅ Full test coverage
- ✅ TypeSpec API documentation (auto-generated!)
LIB (Library Module)
Creates a shared library module:
src/libs/[name]/
├── adapter.ts # Service adapter interface
├── index.ts # Public exports
├── module.ts # Library module
└── service.ts # Service implementationUse cases: Event handlers, i18n, metrics, tokens, shared utilities
INFRA (Infrastructure Module)
Creates an infrastructure module:
src/infra/[name]/
├── adapter.ts # Infrastructure adapter interface
├── index.ts # Public exports
├── module.ts # Infrastructure module
└── service.ts # Service implementationUse cases: Cache, database connections, email, HTTP clients, loggers, secrets
MODULE (Simple Module)
Creates a basic module:
src/modules/[name]/
├── controller.ts # Basic controller
└── module.ts # Module definitionUse cases: Health checks, status endpoints, simple routes
CORE (Single Use Case)
Creates a single domain use case:
src/core/[name]/
├── entity/
│ └── [name].ts
├── repository/
│ └── [name].ts
└── use-cases/
└── [name]-rename.tsUse cases: Custom business logic, specific operations
Auto-Import Behavior
The CLI automatically registers generated modules:
- CRUD/MODULE: Registered in
src/app.module.ts - LIB: Registered in
src/libs/module.tswithLibModulesuffix - INFRA: Registered in
src/infra/module.ts - TypeSpec: Auto-imports in
docs/src/main.tsp(for CRUD templates)
No manual imports needed! 🎉
TypeSpec Documentation
When generating CRUD modules (Postgres or Mongo), the CLI automatically creates TypeSpec documentation:
docs/src/modules/[name]/
├── controller.tsp # API routes with all endpoints
├── model.tsp # Entity models and DTOs
└── exception.tsp # Error definitions (400, 404)The import is automatically added to docs/src/main.tsp:
import "./modules/[name]/controller.tsp";Generated TypeSpec includes:
- ✅ All CRUD endpoints (POST, PUT, GET, DELETE)
- ✅ Input/Output models
- ✅ Pagination models
- ✅ Validation error definitions
- ✅ Not found error definitions
- ✅ Bearer authentication
- ✅ API versioning support
Name Validation
Module names are automatically sanitized:
- ✅ Converts to kebab-case
- ✅ Removes special characters
- ✅ Prevents numeric prefixes
- ✅ Handles spaces and underscores
Examples:
User Profile→user-profilemy_module→my-moduleProduct@123→product-123
Generated Code Patterns
Entity
export class ProductEntity extends BaseEntity {
name: string;
constructor(input: ProductEntityInput) {
super();
this.name = input.name;
this.validate();
this.ensureID();
}
validate() {
const validation = ProductEntitySchema.safeParse(this);
if (!validation.success) return validation.error;
}
}Use Case
export class ProductCreateUsecase {
constructor(private readonly repository: IProductRepository) {}
async execute(input: ProductCreateInput): Promise<ProductEntity> {
const entity = new ProductEntity(input);
await this.repository.create(entity);
return entity.toObject();
}
}Controller
@Controller('products')
export class ProductController {
@Post()
@Permission('product:create')
@ApiBody({ type: ProductCreateInput })
async create(@Body() input: ProductCreateInput) {
return await this.createUsecase.execute(input);
}
}Requirements
Your NestJS project must have:
src/core/directorysrc/modules/directorysrc/app.module.tsfile
For library modules: src/libs/module.ts
For infrastructure modules: src/infra/module.ts
Best Practices
- Run from project root: Always execute the CLI from your NestJS project root
- Descriptive names: Use clear, descriptive module names
- Follow conventions: Stick to kebab-case naming
- Review generated code: Always review and customize generated code for your needs
- Run tests: Execute
npm testafter generation to ensure everything works
Testing Generated Code
After generating a module, verify it works:
# Build the project
npm run build
# Run linter
npm run lint
# Run tests
npm testCustomization
All generated templates are in src/templates/:
core/- Core domain templatesmongo/- MongoDB templatespostgres/- PostgreSQL templateslibs/- Library templatesinfra/- Infrastructure templatesmodule/- Simple module templates
Modify these to match your project's patterns.
Troubleshooting
Error: "select nestjs microservice api root"
- Ensure you're running from the correct directory
- Verify
src/core/andsrc/modules/exist
Module not imported automatically
- Check if target module file exists
- Verify import paths are correct
- Module may already be imported
Build errors after generation
- Run
npm run buildto check TypeScript errors - Verify dependencies are installed
- Check for naming conflicts
Contributing
Contributions are welcome! Please submit issues and pull requests on GitHub.
License
MIT
Author
Mike Majesty - @mikemajesty
