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],

问题解决!

相关推荐
里欧跑得慢5 分钟前
CSS 模块化架构的演进:BEM、CSS Modules 到 CSS-in-JS 的反思
前端·css·flutter·web·css-in-js
IT_陈寒18 分钟前
Vue的computed属性把我坑惨了,原来我一直用错姿势
前端·人工智能·后端
小灰灰搞电子1 小时前
Rust+Slint 实现温度计源码分享
前端·rust·slint
计算机魔术师1 小时前
面壁智能 OpenBMB 推出 MathForm,面向 Lean 4 数学自动形式化的开源框架、数据集与模型
前端
evans在进步1 小时前
Spring Boot 核心机制详解:可执行 JAR、CORS、静态资源与配置绑定
spring boot·后端·jar
Sayuanni%31 小时前
SpringBoot 从注解到源码:核心知识点总结
java·spring boot·后端
NeilCarmack1 小时前
Deepseek-harness增加桌面版端序列:第 2 讲 · spawn Electron:当前进程如何“交棒“
前端·javascript·electron
上海魁鲸科技有限公司2 小时前
APS高级排产系统到底有什么用?一文讲清功能、选型与落地建议
前端·microsoft·excel
陈随易2 小时前
Bun v1.4 更新总结:把浏览器、图片、定时任务和工程工具都装进一个运行时
前端·后端·程序员
人间凡尔赛3 小时前
2026 后端架构三驾马车:Wasm 容器上 K8s、存算分离与 AI 原生
后端·云原生·架构