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 authExamples:
1. MVC Architecture
2. Feature Architecture
Service
npx servercn-cli g se authExamples:
1. MVC Architecture
2. Feature Architecture
Route
npx servercn-cli g ro authExamples:
1. MVC Architecture
2. Feature Architecture
Model
npx servercn-cli g mo userExamples:
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 authnpx servercn-cli g dt user --flatnpx servercn-cli g dt user name:string email:email age:numberCreates:
- 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 authnpx servercn-cli g va auth --flatnpx servercn-cli g va user name:string email:email age:numberCreates:
- MVC:
src/validators/auth.validator.ts - Feature:
src/modules/auth/auth.validator.ts
Resource
npx servercn-cli g re postCreates:
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 --helpSummary
npx servercn-cli g co authnpx servercn-cli g se usernpx servercn-cli g mo usernpx servercn-cli g ro authnpx servercn-cli g re postnpx servercn-cli g dt authnpx servercn-cli g va authnpx servercn-cli g --help