Generators
Generators allow you to quickly scaffold boilerplate code for your application.
Instead of manually creating files, you can use the CLI:
npx servercn-cli g <type> <name> [options] Aliases
| Type | Alias |
|---|---|
| Controller | co |
| Service | se |
| Route | ro |
| Model | mo |
| DTO | dt |
| Validator | va |
| Resource | re |
Options
--force: Overwrite existing files--flat: Generate files in a flat structure-h,--help: display help for command
Available Generators
Controller
npx servercn-cli g co auth Examples:
1. MVC Architecture
2. Feature Architecture
Service
npx servercn-cli g se auth Examples:
1. MVC Architecture
2. Feature Architecture
Route
npx servercn-cli g ro auth Examples:
1. MVC Architecture
2. Feature Architecture
Model
npx servercn-cli g mo user Examples:
1. MVC Architecture
2. Feature Architecture
DTO
Generate Data Transfer Objects (DTOs) using zod for validation and type inference.
DTOs are used to validate incoming data (e.g. request body, params, query).
npx servercn-cli g dto <name> [fields...] [options] fields:
Define fields using: key:type
npx servercn-cli g dto auth npx servercn-cli g dt user --flat npx servercn-cli g dt user name:string email:email age:number Creates:
- MVC:
src/dtos/auth.dto.ts - Feature:
src/modules/auth/auth.dto.ts
Validator
Generate Validators using zod for validation and type inference.
npx servercn-cli g va <name> [fields...] [options] fields:
Define fields using: key:type
npx servercn-cli g va auth npx servercn-cli g va auth --flat npx servercn-cli g va user name:string email:email age:number Creates:
- MVC:
src/validators/auth.validator.ts - Feature:
src/modules/auth/auth.validator.ts
Resource
npx servercn-cli g re post Creates:
1. MVC:
-
src/controllers/post.controller.ts -
src/services/post.service.ts -
src/routes/post.route.ts -
src/models/post.model.ts -
src/dtos/post.dto.ts
Example:
2. Feature:
-
src\modules\post\post.controller.ts -
src\modules\post\post.service.ts -
src\modules\post\post.route.ts -
src\modules\post\post.model.ts -
src\modules\post\post.dto.ts
Example:
Help
npx servercn-cli g --help Summary
npx servercn-cli g co auth npx servercn-cli g se user npx servercn-cli g mo user npx servercn-cli g ro auth npx servercn-cli g re post npx servercn-cli g dt auth npx servercn-cli g va auth npx servercn-cli g --help