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

问题解决!

相关推荐
咖啡の猫6 分钟前
Vue插件
前端·javascript·vue.js
韩劳模7 分钟前
Canvas、SVG实现不规则区域高亮的方案说明
前端
拾光师15 分钟前
Hadoop安全模式详解
后端
张可爱24 分钟前
20251026-从网页 Console 到 Python 爬虫:一次 B 站字幕自动抓取的实践与复盘
前端·python
阿杰AJie31 分钟前
数据库id生成方案
后端·mysql
咖啡の猫1 小时前
Vue中的自定义事件
前端·javascript·vue.js
仪器工程师1 小时前
报错提示 “unclosed parenthesis”“mismatched quotes” 的解决办法
后端
yangwan1 小时前
Ubunut 22.04 安装 Docker 24.0.x
前端·后端
等风起8811 小时前
Element Plus实现TreeSelect树形选择在不同父节点下子节点有相同id的双向绑定联动
前端·javascript