7/22 搭建基本框架
-
新建项目
blog-api-nest -
创建帖子
post模块- 创建帖子模块
nest g mo posts - 创建帖子控制器
nest g co posts
- 创建帖子模块
-
安装APIUI 接口文档管理
npm install --save @nestjs/swagger swagger-ui-express -
安装数据库环境
yarn add @typegoose/typegoose mongoose @types/mongoose@types/mongoose ts定义文件ps: 是为了更好支持ts和代码提示 -
安装管道可以验证请求数据规范
yarn add class-validator class-transformer
常用装饰器
| 装饰器 | 说明 |
|---|---|
@ApiTags | 模块api标签说明 |
@ApiOperation | api标签说明 |
@ApiProperty | 接口类型定义描述 |
@Param | url所有参数 |
@prop() | 数据模型 |
@IsNotEmpty | 字段校验 |
常用装mongoose函数
| 函数 | 作用 |
|---|---|
find | 查找所有数据 |
create | 创建一条数据 |
findById | 查找一条数据 |
findByIdAndUpdate | 更新一条数据 |
findByIdAndDelete | 删除一条数据 |