NestJS-聊天模块

NestJS-聊天模块

1、模块文件搭建

javascript 复制代码
nest g controller modules/chat --no-spec
nest g module modules/chat --no-spec
nest g service modules/chat --no-spec

生成的文件

javascript 复制代码
chat.controller.ts
chat.module.ts
chat.service.ts

2、模块实体创建

创建聊天消息实体(Entity): 我们需要定义一个数据库sys_chat表来存储消息

👉sql数据

javascript 复制代码
CREATE TABLE sys_chat (
  chatId INT AUTO_INCREMENT PRIMARY KEY,
  senderId VARCHAR(255) NOT NULL,
  receiverId VARCHAR(255) NOT NULL,
  chatType ENUM('single', 'group') NOT NULL,
  content TEXT NOT NULL,
  messageType ENUM('text', 'image', 'audio', 'video', 'file') NOT NULL,
  status ENUM('sent', 'delivered', 'read') DEFAULT 'sent',
  timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  attachments JSON DEFAULT NULL,
  groupId INT DEFAULT NULL,
  groupName VARCHAR(255) DEFAULT NULL,  -- 添加群组名称字段
  isSystem BOOLEAN DEFAULT FALSE,
  isDeleted BOOLEAN DEFAULT FALSE,
  create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,  -- 记录创建时间
  update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP -- 记录更新时间,每次更新时自动更新
);

👉 chat.entity.ts

javascript 复制代码
import { Entity, PrimaryGeneratedColumn, Column, UpdateDateColumn, CreateDateColumn } from 'typeorm';

@Entity('sys_chat')
export class SysChat {
  @PrimaryGeneratedColumn()
  chatId: number;

  @Column({ type: 'varchar', length: 255 })
  senderId: string;

  @Column({ type: 'varchar', length: 255 })
  receiverId: string;

  @Column({ type: 'enum', enum: ['single', 'group'] })
  chatType: 'single' | 'group';

  @Column({ type: 'text' })
  content: string;

  @Column({ type: 'enum', enum: ['text', 'image', 'audio', 'video', 'file'] })
  messageType: 'text' | 'image' | 'audio' | 'video' | 'file';

  @Column({ type: 'enum', enum: ['sent', 'delivered', 'read'], default: 'sent' })
  status: 'sent' | 'delivered' | 'read';

  @CreateDateColumn({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
  timestamp: Date;

  @Column({ type: 'json', nullable: true })
  attachments: any;

  @Column({ type: 'int', nullable: true })
  groupId: number;

  @Column({ type: 'varchar', length: 255, nullable: true })
  groupName: string;

  @Column({ type: 'boolean', default: false })
  isSystem: boolean;

  @Column({ type: 'boolean', default: false })
  isDeleted: boolean;

  @CreateDateColumn({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
  create_time: Date;

  @UpdateDateColumn({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP', onUpdate: 'CURRENT_TIMESTAMP' })
  update_time: Date;
}

👉 引入实体和控制器

chat.module.ts导入需要的部分然后进行导出

javascript 复制代码
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from './user.entity';
import { UserService } from './user.service';
import { UserController } from './user.controller';
import { AuthModule } from '@/auth/auth.module';

@Module({
  imports: [TypeOrmModule.forFeature([User]),AuthModule],  // 导入实体
  controllers: [UserController],               // 注册控制器
  providers: [UserService],                    // 注册服务
  exports: [UserService]                       // 导出服务
})
export class UserModule {}

3、模块功能

增加

👉chat.module.ts
javascript 复制代码
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

import { ChatService } from './chat.service';
import { ChatController } from './chat.controller';
import { SysChat } from './chat.entity';
import { ChatGateway } from './chat.gateway';

@Module({
  imports: [TypeOrmModule.forFeature([SysChat])],
  providers: [ChatService],
  controllers: [ChatController, ChatGateway],
})
export class ChatModule {}
👉 chat.service.ts

这里我们先简单填写一下我们的方法逻辑

javascript 复制代码
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { SysChat } from './chat.entity';
import { CreateChatDto,UpdateChatDto } from './dto/chat.dto';

async create(createChatDto: CreateChatDto): Promise<SysChat> {
  const chat = this.chatRepository.create(createChatDto);
  return this.chatRepository.save(chat);
}
👉chat.controller.ts
javascript 复制代码
// chat.controller.ts

import { Controller, Get, Post, Body, Param, Put, Delete } from '@nestjs/common';
import { ChatService } from './chat.service';
import { CreateChatDto,UpdateChatDto} from './dto/chat.dto';
import { SysChat } from './chat.entity';

@Controller('chat')
export class ChatController {
  constructor(private readonly chatService: ChatService) {}
  // 创建消息
  @Post()
  async create(@Body() createChatDto: CreateChatDto): Promise<SysChat> {
    return this.chatService.create(createChatDto);
  }
}
👉 调用接口

这个时候我们调用接口,返回的信息如下,新增接口就成功了

javascript 复制代码
{
    "chatId": 2,
    "senderUserId": "57",
    "receiverUserId": "user123",
    "chatType": "single",
    "content": "Hello, how are you?",
    "messageType": "text",
    "status": "sent",
    "attachments": null,
    "groupId": null,
    "groupName": null,
    "isSystem": false,
    "isDeleted": false,
    "createTime": "2025-05-13 16:16:13",
    "updateTime": "2025-05-13 16:16:13"
}

查找

👉 chat.service.ts

这里我们先简单填写一下我们的方法逻辑

javascript 复制代码
// 获取所有消息
async findAll(): Promise<SysChat[]> {
  return this.chatRepository.find();
}
👉chat.controller.ts
javascript 复制代码
async findAll(): Promise<SysChat[]> {
  return this.chatService.findAll();
}
👉 调用接口

这个时候我们调用接口,返回的信息如下,查询接口ok

javascript 复制代码
[
    {
        "chatId": 1,
        "senderUserId": "57",
        "receiverUserId": "user123",
        "chatType": "single",
        "content": "Hello, how are you?",
        "messageType": "text",
        "status": "sent",
        "attachments": null,
        "groupId": null,
        "groupName": null,
        "isSystem": false,
        "isDeleted": false,
        "createTime": "2025-05-13 16:15:09",
        "updateTime": "2025-05-13 16:15:09"
    },
    {
        "chatId": 2,
        "senderUserId": "57",
        "receiverUserId": "user123",
        "chatType": "single",
        "content": "Hello, how are you?",
        "messageType": "text",
        "status": "sent",
        "attachments": null,
        "groupId": null,
        "groupName": null,
        "isSystem": false,
        "isDeleted": false,
        "createTime": "2025-05-13 16:16:13",
        "updateTime": "2025-05-13 16:16:13"
    }
]

详情

👉chat.controller.ts
javascript 复制代码
 // 获取单个详情
  @Get('/system/chat/:id')
  async findOne(@Param('id') id: number){
    return this.chatService.findOne(id);
  }
👉 chat.service.ts

这里我们先简单填写一下我们的方法逻辑

javascript 复制代码
// 根据 ID 获取单个消息
  async findOne(chatId: number){
    return this.chatRepository.findOne({ where: { chatId } });
  }
👉 调用接口

这个时候我们调用接口,返回的信息如下,查询接口ok

javascript 复制代码
{
    "chatId": 1,
    "senderUserId": "57",
    "receiverUserId": "user123",
    "chatType": "single",
    "content": "Hello, how are you?",
    "messageType": "text",
    "status": "sent",
    "attachments": null,
    "groupId": null,
    "groupName": null,
    "isSystem": false,
    "isDeleted": false,
    "createTime": "2025-05-13 16:15:09",
    "updateTime": "2025-05-13 16:15:09",
    "senderUserAvatar": null,
    "senderUserName": null
}

更新

👉chat.controller.ts
javascript 复制代码
 @Put('/system/chat')
  async update(
    @Body('id') id: number,
    @Body() updateChatDto, // : UpdateChatDto
  ) {
    return this.chatService.update(id, updateChatDto);
  }
👉 chat.service.ts

这里我们先简单填写一下我们的方法逻辑

javascript 复制代码
async update(chatId: number, updateChatDto: UpdateChatDto) {
    const resdata = await this.chatRepository.findOne({ where: { chatId } });
    if (!resdata) {
      return {
        code: 404,
        message: '数据不存在!',
        data: null,
      };
      //throw new Error('Message not found');
    }
    const updateData = { ...resdata, updateTime: new Date() }; // 合并更新的数据-确保更新时间是最新的
    // Object.assign(resdata, updateChatDto);
    console.log(resdata, 'resdata-----------更新信息');
    const result = await this.chatRepository.save(resdata);
    if (result) {
      return {
        code: 200,
        message: '更新成功',
        // data: updatedUser,
      };
    } else {
      return {
        code: 500,
        message: '更新失败',
      };
    }
    // return this.chatRepository.save(resdata);
  }
👉 调用接口

这个时候我们调用接口,返回的信息如下,查询接口ok

javascript 复制代码
{
    "code": 200,
    "message": "更新成功"
}

删除

删除部分我们一般只是做一个假的删除部分

👉chat.controller.ts
javascript 复制代码
// 删除消息 : Promise<void> 
@Delete('/system/chat/:id')
async remove(@Param('id') id: number){
  return this.chatService.remove(id);
}
👉 chat.service.ts

这里我们先简单填写一下我们的方法逻辑

javascript 复制代码
// 删除消息(软删除) : Promise<void> 
async remove(chatId: number) {
  const chat = await this.chatRepository.findOne({ where: { chatId } });
  if (chat) {
    chat.isDeleted = true;
    const delUpdateData = {...chat,isDeleted: true};
    await this.chatRepository.save(delUpdateData);
    // const delUser = await this.userRepository.delete(id);
    const delData = await this.chatRepository.save(chat);
    if( delData) {
      return {
        code: 200,
        message: '删除成功',
      };
    }else {
      return {
        code: 200,
        message: '删除失败',
      };
    }
  } else {
    return {
      code: 401,
      message: '数据不存在!',
    };
  }
}
👉 调用接口

这个时候我们调用接口,返回的信息如下,查询接口ok

javascript 复制代码
{
    "code": 200,
    "message": "更新成功"
}

4、报错以及处理

👉An invalid controller has been detected

javascript 复制代码
UnknownRequestMappingException [Error]: An invalid controller has been detected. "ChatGateway" does not have the @Controller() 
decorator but it is being listed in the 
"controllers" array of some module.
写法
javascript 复制代码
controllers: [ChatController, ChatGateway],
原因

"ChatGateway"网关应该放入providers模块中

处理方式
javascript 复制代码
controllers: [ChatController, ChatGateway],
=> 
providers: [ChatService, ChatGateway],

问题解决!

相关推荐
To_OC8 小时前
别再串行写 await 了,Promise.all 才是并行请求的正确打开方式
前端·javascript·promise
vipbic9 小时前
中后台越做越乱后,我用插件化把它救回来了
前端·vue.js
Hyyy9 小时前
Computer Use 适合做什么,不适合做什么——一次真实使用后的思考
前端
小和尚同志9 小时前
前端 AI 单元测试思考与落地
前端·人工智能·aigc
invicinble10 小时前
c端系统,其实更像一个信息展示平台
前端
李姆斯11 小时前
管理是否可以被完全量化
前端·产品经理·团队管理
戮漠summer12 小时前
Missing Semester 计算机教育中缺失的一课 Lecture 01 Shell
开发语言·后端·scala
名字还没想好☜12 小时前
Next.js 中间件实战:鉴权、重定向与 A/B 分流
开发语言·前端·javascript·中间件·react·next.js
广州灵眸科技有限公司12 小时前
瑞芯微RV1126B开发板(EASY-EAI-PI2) INI文件操作
java·前端·javascript·网络·人工智能
江华森13 小时前
Web 开发基础:HTTP 与 REST
前端·网络协议·http