Nest.js从0到1搭建博客系统---CRUD(5)

319 阅读5分钟

前言

本篇文章介绍MySQL(数据库)的建表,查表改表删表的基本操作,即CRUD ,它表示增加(Create)、查询(Retrieve)、更新(Update)、删除(Delete)四个单词的首字母缩写。

ColumnOptions中可用选项列表:

  • type: ColumnType - 列类型。其中之一在上面.

  • name: string - 数据库表中的列名。 默认情况下,列名称是从属性的名称生成的。 你也可以通过指定自己的名称来更改它。

  • length: number - 列类型的长度。 例如,如果要创建varchar(150)类型,请指定列类型和长度选项。

  • width: number - 列类型的显示范围。 仅用于MySQL integer types(opens new window)

  • onUpdate: string - ON UPDATE触发器。 仅用于 MySQL (opens new window).

  • nullable: boolean - 在数据库中使列NULL或NOT NULL。 默认情况下,列是nullable:false。

  • update: boolean - 指示"save"操作是否更新列值。如果为false,则只能在第一次插入对象时编写该值。 默认值为"true"。

  • select: boolean - 定义在进行查询时是否默认隐藏此列。 设置为false时,列数据不会显示标准查询。 默认情况下,列是select:true

  • default: string - 添加数据库级列的DEFAULT值。

  • primary: boolean - 将列标记为主要列。 使用方式和@ PrimaryColumn相同。

  • unique: boolean - 将列标记为唯一列(创建唯一约束)。

  • comment: string - 数据库列备注,并非所有数据库类型都支持。

  • precision: number - 十进制(精确数字)列的精度(仅适用于十进制列),这是为值存储的最大位数。仅用于某些列类型。

  • scale: number - 十进制(精确数字)列的比例(仅适用于十进制列),表示小数点右侧的位数,且不得大于精度。 仅用于某些列类型。

  • zerofill: boolean - 将ZEROFILL属性设置为数字列。 仅在 MySQL 中使用。 如果是true,MySQL 会自动将UNSIGNED属性添加到此列。

  • unsigned: boolean - 将UNSIGNED属性设置为数字列。 仅在 MySQL 中使用。

  • charset: string - 定义列字符集。 并非所有数据库类型都支持。

  • collation: string - 定义列排序规则。

  • enum: string[]|AnyEnum - 在enum列类型中使用,以指定允许的枚举值列表。 你也可以指定数组或指定枚举类。

  • asExpression: string - 生成的列表达式。 仅在MySQL (opens new window)中使用。

  • generatedType: "VIRTUAL"|"STORED" - 生成的列类型。 仅在MySQL (opens new window)中使用。

  • hstoreType: "object"|"string" -返回HSTORE列类型。 以字符串或对象的形式返回值。 仅在Postgres中使用。

  • array: boolean - 用于可以是数组的 postgres 列类型(例如 int [])

  • transformer: { from(value: DatabaseType): EntityType, to(value: EntityType): DatabaseType } - 用于将任意类型EntityType的属性编组为数据库支持的类型DatabaseType。

新增

  • 创建实体user.entity.ts
import { ApiProperty } from '@nestjs/swagger'
import { Entity, Column, PrimaryGeneratedColumn, CreateDateColumn, UpdateDateColumn } from 'typeorm';
@Entity({ name: "sys_user" })
export class User {
    @ApiProperty({ description: '用户主键ID' })
    @PrimaryGeneratedColumn({
        type: 'bigint',
        comment: '用户主键ID'
    })
    id: number;
    @ApiProperty({ description: '用户名业务id' })
    @Column({ type: 'varchar', name: 'user_id', comment: '用户名业务id' })
    userId: string
    @ApiProperty({ description: '用户名' })
    @Column({ type: 'varchar', length: 50, comment: '用户名' })
    username: string;
    @ApiProperty({ description: '昵称' })
    @Column({ type: 'varchar', length: 50, comment: '昵称' })
    nickname: string;
    @ApiProperty({ description: '手机号' })
    @Column({ length: 11, comment: '手机号' })
    phone: string;
    @ApiProperty({ description: '用户密码' })
    @Column({ comment: "用户密码" })
    password: string;
    @ApiProperty({ description: '状态', })
    @Column({ type: 'tinyint', default: 1, comment: '状态:1-有效,0-禁用' })
    status: number
    @ApiProperty({ description: '备注' })
    @Column({ type: 'text', comment: '备注' })
    remark: string;
    @ApiProperty({ description: '创建时间' })
    @CreateDateColumn({ type: 'timestamp', comment: '创建时间' })
    createTime: Date;
    @ApiProperty({ description: '更新时间' })
    @UpdateDateColumn({ type: 'timestamp', comment: '更新时间' })
    updateTime: Date;
}
  • 修改create-user.dto.ts,添加参数验证
/*
 * @Author: vhen
 * @Date: 2023-12-21 17:39:47
 * @LastEditTime: 2023-12-26 01:36:35
 * @Description: 现在的努力是为了小时候吹过的牛逼!
 * @FilePath: \nest-vhen-blog\src\modules\user\dto\create-user.dto.ts
 */
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString, Length, IsIn, IsNumber, MinLength, MaxLength } from 'class-validator'
export class CreateUserDto {
    @ApiProperty({ description: '用户名' })
    @IsNotEmpty()//验证是否为空
    @IsString() //是否为字符串
    @MinLength(1, { message: '用户名至少1个字符' })
    @MaxLength(50, { message: '用户名最多50个字符' })
    username: string;
    @ApiProperty({ description: '昵称' })
    @IsString() //是否为字符串
    @Length(0, 50)
    nickname: string;
    @ApiProperty({ description: '手机号' })
    @IsString() //是否为字符串
    @Length(0, 11)
    phone: string;
    @ApiProperty({ description: '用户密码' })
    @IsString() //是否为字符串
    password: string;
    @ApiProperty({ description: '状态', default: 1 })
    @IsNumber()
    status: number;
    @ApiProperty({ description: '备注' })
    @IsString() //是否为字符串
    remark: string;
}
  • 修改 user.module.ts注入实体
/*
 * @Author: vhen
 * @Date: 2023-12-21 17:39:47
 * @LastEditTime: 2023-12-25 18:03:50
 * @Description: 现在的努力是为了小时候吹过的牛逼!
 * @FilePath: \nest-vhen-blog\src\modules\user\user.module.ts
 * 
 */
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UserService } from './user.service';
import { UserController } from './user.controller';
import { User as UserEntity } from './entities/user.entity'
@Module({
  imports: [
    TypeOrmModule.forFeature([UserEntity]),
  ],
  controllers: [UserController],
  providers: [UserService],
  exports: [UserService],
})
export class UserModule { }

  • 修改 user.service.ts
/*
 * @Author: vhen
 * @Date: 2023-12-21 17:39:47
 * @LastEditTime: 2023-12-26 15:36:08
 * @Description: 现在的努力是为了小时候吹过的牛逼!
 * @FilePath: \nest-vhen-blog\src\modules\user\user.service.ts
 * 
 */
import { Injectable } from '@nestjs/common';
import { Repository } from 'typeorm';
import * as uuid from 'uuid';
import { InjectRepository } from '@nestjs/typeorm';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { User as UserEntity } from './entities/user.entity';
@Injectable()
export class UserService {
  // 通过构造函数注入
  constructor(@InjectRepository(UserEntity) private readonly userRepo: Repository<UserEntity>) { }
  create(createUserDto: CreateUserDto) {
    const data = new UserEntity()
    data.userId = `U${uuid.v4()}`
    data.username = createUserDto.username
    data.nickname = createUserDto.nickname
    data.phone = createUserDto.phone
    data.password = createUserDto.password
    data.status = createUserDto.status
    data.remark = createUserDto.remark
    return this.userRepo.save(data)
  }
}
  • 修改 user.controller.ts
/*
 * @Author: vhen
 * @Date: 2023-12-21 17:39:47
 * @LastEditTime: 2023-12-26 14:44:27
 * @Description: 现在的努力是为了小时候吹过的牛逼!
 * @FilePath: \nest-vhen-blog\src\modules\user\user.controller.ts
 */
import { Controller, Get, Post, Body,Query, Patch, Param, Delete, HttpCode, HttpStatus, } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiParam, ApiBearerAuth, ApiBody, ApiResponse, ApiHeader } from "@nestjs/swagger"
import { UserService } from './user.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { AllowAnon } from '@/common/decorator/allow-anon.decorator'
import { User as UserEntity } from './entities/user.entity';

@Controller({
  path: "user",
  version: '1'
})
@ApiTags("用户管理")
@ApiBearerAuth()
@ApiHeader({
  name: 'authoriation',
  required: true,
  description: '本次请求请带上token',
})
@AllowAnon()
export class UserController {
  constructor(private readonly userService: UserService) { }
  @ApiResponse({ status: 201, description: '创建用户', type: [UserEntity] })
  @Post()
  @HttpCode(HttpStatus.CREATED)
  create(@Body() createUserDto: CreateUserDto) {
    return this.userService.create(createUserDto);
  }
}
  • 执行接口

image.png

image.png

修改

  • user.service.ts
  update(id: number, updateUserDto: UpdateUserDto) {
    delete updateUserDto.password
    this.userRepo.update(id, updateUserDto)
  }
  • user.controller.ts
  @ApiResponse({ status: 200, description: '更新用户', type: UserEntity })
  @Patch(':id')
  update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
    return this.userService.update(+id, updateUserDto);
  }

image.png image.png

查询

  • user.service.ts
async findAll(query: { keyWord: string, page: number, pageSize: number }) {
    const data = this.userRepo.find({
      where: {
        username: Like(`%${query.keyWord}%`)
      },
      order: {
        id: "DESC"
      }
    return data
}
  • user.controller.ts
  @Get("getUserList")
  // @Version('1')
  @ApiOperation({ summary: "获取用户列表", description: "获取用户列表接口" })
  findAll(@Query() query: { keyWord: string, page: number, pageSize: number }) {
    return this.userService.findAll(query);
  }

image.png

删除

  • user.service.ts
 remove(id: number) {
   return this.userRepo.delete(id)
 }
  • user.controller.ts
@ApiOperation({ summary: "删除用户", description: "根据id删除用户" })
@Delete(':id')
remove(@Param('id') id: string) {
    return this.userService.remove(+id);
}

image.png

  • id为1的用户小星被删除了 image.png image.png

githutb

项目地址:nest_vhen_blog