$npx -y skills add youlaitech/youlai-skills --skill nestjsNestJS backend development standards. Use this skill when developing NestJS projects, implementing REST APIs, using TypeORM, JWT authentication, or permission control.
| 1 | # NestJS 后端开发规范 |
| 2 | |
| 3 | ## 触发条件 |
| 4 | |
| 5 | - Develop NestJS projects |
| 6 | - Implement REST APIs |
| 7 | - Use TypeORM for data access |
| 8 | - Implement JWT authentication |
| 9 | - Implement permission control |
| 10 | |
| 11 | --- |
| 12 | |
| 13 | ## Part 1: 技术栈 |
| 14 | |
| 15 | | 层 | 选型 | 说明 | |
| 16 | |----|------|------| |
| 17 | | 运行环境 | **Node.js 20+** | ES2022+、原生 fetch | |
| 18 | | 框架 | **NestJS** | 装饰器、模块化、依赖注入 | |
| 19 | | 语言 | **TypeScript** 5.x | 类型安全、装饰器元数据 | |
| 20 | | ORM | **TypeORM** | Active Record / Data Mapper | |
| 21 | | 数据库 | **MySQL 8.x** | InnoDB 引擎 | |
| 22 | | 缓存 | **Redis 7.x** (ioredis) | 分布式缓存 | |
| 23 | | 认证 | **@nestjs/jwt** + **@nestjs/passport** | JWT 签发/验签 | |
| 24 | | API 文档 | **@nestjs/swagger** | OpenAPI 自动生成 | |
| 25 | |
| 26 | --- |
| 27 | |
| 28 | ## Part 2: 目录结构 |
| 29 | |
| 30 | ``` |
| 31 | src/ |
| 32 | ├── common/ # 公共模块 |
| 33 | │ ├── constants/ # metadata, redis, role, system |
| 34 | │ ├── decorators/ # auth, current-user, data-permission, log |
| 35 | │ ├── dto/ # base-query.dto |
| 36 | │ ├── entities/ # base.entity |
| 37 | │ ├── enums/ # action-type, data-scope, error-code, log-module |
| 38 | │ ├── exceptions/ # business.exception |
| 39 | │ ├── filters/ # http-exception.filter |
| 40 | │ ├── guards/ # jwt-auth, permission, data-scope |
| 41 | │ ├── interceptors/ # response, request, data-permission |
| 42 | │ ├── interfaces/ # current-user, response |
| 43 | │ ├── middleware/ # logger, rate-limit, request-context |
| 44 | │ ├── models/ # role-data-scope.model |
| 45 | │ ├── redis/ # redis.module, redis.service |
| 46 | │ ├── subscribers/ # audit.subscriber |
| 47 | │ └── utils/ # captcha, logger, serialize, user-agent |
| 48 | │ |
| 49 | ├── config/ # jwt, oss, redis, typeorm |
| 50 | │ |
| 51 | ├── auth/ # 认证模块 |
| 52 | │ ├── auth.{controller,service,module}.ts |
| 53 | │ ├── wxma-auth.{controller,service}.ts |
| 54 | │ ├── strategies/jwt.strategy.ts |
| 55 | │ ├── guards/redis-token.guard.ts |
| 56 | │ └── dto/ # login-request, login-result, wxma-login-result |
| 57 | │ |
| 58 | ├── system/ # 系统管理(按实体分子模块) |
| 59 | │ ├── user/ # {controller,service,module, entities/, dto/} |
| 60 | │ ├── role/ # 同上 + role-permission.service |
| 61 | │ ├── menu/ # 同上 |
| 62 | │ ├── dept/ dict/ config/ notice/ log/ |
| 63 | │ |
| 64 | ├── codegen/ # 代码生成器 |
| 65 | ├── file/ # 文件模块 |
| 66 | ├── message/ # 消息模块(SSE) |
| 67 | ├── app.module.ts # 根模块 |
| 68 | └── main.ts # 入口 |
| 69 | ``` |
| 70 | |
| 71 | **设计原则**: |
| 72 | - `common/` 是公共组件,被所有模块共享 |
| 73 | - `auth/` 是认证模块,含 JWT 策略 + 守卫 |
| 74 | - `system/` 按实体分子模块,每子模块含 controller/service/module/entities/dto |
| 75 | - NestJS 用 Module 组织依赖,Decorator 声明路由/守卫/拦截器 |
| 76 | |
| 77 | --- |
| 78 | |
| 79 | ## Part 3: 命名规范 |
| 80 | |
| 81 | ### 3.1 文件命名 |
| 82 | |
| 83 | | 类型 | 规范 | 示例 | |
| 84 | |------|------|------| |
| 85 | | 模块 | `{name}.module.ts` | `user.module.ts` | |
| 86 | | 控制器 | `{name}.controller.ts` | `user.controller.ts` | |
| 87 | | 服务 | `{name}.service.ts` | `user.service.ts` | |
| 88 | | 实体 | `{name}.entity.ts` | `sys-user.entity.ts` | |
| 89 | | DTO | `{name}.dto.ts` | `create-user.dto.ts` | |
| 90 | | 守卫 | `{name}.guard.ts` | `jwt-auth.guard.ts` | |
| 91 | | 策略 | `{name}.strategy.ts` | `jwt.strategy.ts` | |
| 92 | |
| 93 | ### 3.2 类命名 |
| 94 | |
| 95 | | 类型 | 规范 | 示例 | |
| 96 | |------|------|------| |
| 97 | | Controller | `{Entity}Controller` | `UserController` | |
| 98 | | Service | `{Entity}Service` | `UserService` | |
| 99 | | Module | `{Entity}Module` | `UserModule` | |
| 100 | | Entity | PascalCase | `SysUser` | |
| 101 | | DTO | 功能 + 类型后缀 | `CreateUserDto`, `UserPageDto`, `UserVO` | |
| 102 | |
| 103 | ### 3.3 方法命名 |
| 104 | |
| 105 | | 动作 | 前缀 | 示例 | |
| 106 | |------|------|------| |
| 107 | | 查询单个 | get | `getById()` | |
| 108 | | 查询列表 | list | `listUsers()` | |
| 109 | | 分页查询 | page | `pageUsers()` | |
| 110 | | 新增 | create | `createUser()` | |
| 111 | | 更新 | update | `updateUser()` | |
| 112 | | 删除 | remove / delete | `removeById()` | |
| 113 | |
| 114 | ### 3.4 变量命名 |
| 115 | |
| 116 | | 类型 | 规范 | 示例 | |
| 117 | |------|------|------| |
| 118 | | 变量 | camelCase | `userList` | |
| 119 | | 常量 | UPPER_SNAKE_CASE | `MAX_SIZE` | |
| 120 | | 私有变量 | `_` 前缀 | `_userService` | |
| 121 | | 布尔值 | is/has/can 前缀 | `isDeleted`, `hasPermission` | |
| 122 | |
| 123 | --- |
| 124 | |
| 125 | ## Part 4: RESTful API 规范 |
| 126 | |
| 127 | ### 4.1 标准 CRUD 路径 |
| 128 | |
| 129 | | 操作 | 方法 | 路径 | |
| 130 | |------|------|------| |
| 131 | | 分页列表 | `GET` | `/api/v1/users/page` | |
| 132 | | 详情 | `GET` | `/api/v1/users/:id` | |
| 133 | | 新增 | `POST` | `/api/v1/users` | |
| 134 | | 更新 | `PUT` | `/api/v1/users` | |
| 135 | | 删除 | `DELETE` | `/api/v1/users/:id` | |
| 136 | | 批量删除 | `DELETE` | `/api/v1/users/batch` | |
| 137 | | 下拉选项 | `GET` | `/api/v1/users/options` | |
| 138 | |
| 139 | ### 4.2 Controller 模板 |
| 140 | |
| 141 | ```typescript |
| 142 | @Controller("api/v1/users") |
| 143 | @ApiTags("用户管理") |
| 144 | export class UserController { |
| 145 | constructor(private readonly userService: UserService) {} |
| 146 | |
| 147 | @Get("page") |
| 148 | @ApiOperation({ summary: "用户分页列表" }) |
| 149 | async page(@Query() query: UserPageDto) { |
| 150 | return this.userService.pageUsers(query); |
| 151 | } |
| 152 | |
| 153 | @Get(":id") |
| 154 | @ApiOperation({ summary: "用户详情" }) |
| 155 | async getById(@Param("id") id: string) { |
| 156 | return this.userService.getUserById(Number(id)); |
| 157 | } |
| 158 | |
| 159 | @Post() |
| 160 | @ApiOperation({ summary: "新增用户" }) |
| 161 | @RequirePermissi |