科技企业网站设计制作,网站制作动态,建设企业网站需要什么呢,河北邯郸初始化项目
安装依赖
npm i -g nest/cli
新建项目
nest new project-name
命令行创建
创建Controller#xff1a;nest g co test 创建Module#xff1a;nest g mo test 创建Service#xff1a;nest g service test
请求创建
123123
接口文档swagger
安装依赖
npm…初始化项目
安装依赖
npm i -g nest/cli
新建项目
nest new project-name
命令行创建
创建Controllernest g co test 创建Modulenest g mo test 创建Servicenest g service test
请求创建
123123
接口文档swagger
安装依赖
npm install nestjs/swagger swagger-ui-express
装饰器
Controller装饰器ApiTags(“Controller标签”) Controller装饰器ApiBearerAuth()用于token鉴权在文档页设置token Api装饰器ApiOperation({ summary: ‘Api摘要’, description: ‘Api描述’ }) 模型字段装饰器ApiProperty({ description: ‘字段描述’ })字段必填 模型字段装饰器ApiPropertyOptional({ description: ‘字段描述’ })字段非必填
注册使用
// main.ts
import { NestFactory } from nestjs/core;
import { AppModule } from ./app.module;
import {DocumentBuilder,SwaggerDocumentOptions,SwaggerModule,
} from nestjs/swagger;const app await NestFactory.create(AppModule, { cors: true });const swaggerOption new DocumentBuilder().setTitle(接口文档) // 文档标题.setDescription(用来展示API信息) // 文档描述.setVersion(1.0) // 文档版本号.addTag() // 标签.addBearerAuth() // token鉴权.build();const document SwaggerModule.createDocument(app, swaggerOption);
// 第一个参数是文档访问路径前缀
SwaggerModule.setup(doc, app, document);await app.listen(3000);
// 访问文档路径http://localhost:3000/doc管道验证
安装依赖
npm install class-validator class-transformer
装饰器
模型字段装饰器IsNotEmpty({ message: “xxx字段为必填项” })
常用方法
import { isBoolean, isNumber, isArray, validate} from class-validator;
import { plainToClass } from class-transformer;
// isBoolean 是否为布尔值
// isNumber 是否为数字
// isArray 是否为数组
// validate 验证配置的验证规则
// plainToClass 将对象转换为Class用以获取验证规则进行验证注册使用
管道使用需要注册 在main.ts中全局注册管道
// main.ts
import { ValidationPipe } from nestjs/common;
app.useGlobalPipes(new ValidationPipe());数据库操作TypeORMMySql
安装依赖
npm i typeorm mysql
数据源
实体
装饰器
实体是由Entity装饰器装饰的模型。
// name对应数据库表名schema对应数据库名name可简写
Entity({name:table_name,schema:schema_name})
Entity({table_name,schema:schema_name})表列是由Column装饰器装饰的实体属性。 主列是由PrimaryColumn装饰器装饰的实体属性。 自动生成的列是由PrimaryGeneratedColumn装饰器装饰的实体属性。
列数据类型
数据类型一般会根据实体属性定义的类型进行推断如number将被转换为integerstring将转换为varcharboolean转换为bool等。也可以自行设置如下
// type可以简写
Column(int)Column({type: int})Colunm(int, {name: field_name, length: 100, unique: true, ...})更多配置。
关系
import { Entity, Column, PrimaryGeneratedColumn, ManyToOne, OneToMany, OneToOne, JoinColumn } from typeorm;Entity()
export class Category {PrimaryGeneratedColumn()id: number;Column()name: string;Column()description: string;OneToOne(type Category)JoinColumn()item: Category;OneToMany(type Category, category category.children)parent: Category;ManyToOne(type Category, category category.parent)children: Category;
}OneToOne一对一 OneToMany一对多总是包含反向关系不能单独出现需要和ManyToOne一起使用关系拥有者为多对一的一侧 ManyToOne多对一 JoinColumn实体键的对应关系仅在关系的一侧使用关系拥有者使用关系的拥有方包含数据库中具有外键的列 TODO多对多比较复杂还需研究 ManyToMany多对多 JoinTable()需要指定这是关系的所有者方
参数1typeCategory返回当前实体想要建立关系的目标实体类 参数2categorycategory.xxx用以指定反向关系当前实体存储在目标实体的xxx属性
Entity Manager 和 Repository
Entity Manager实体管理器可以管理insert, update, delete, load 等任何实体。EntityManager 就像放一个实体存储库的集合的地方。
Repository就像EntityManager一样但其操作仅限于具体实体。