nest中的ORM

在 Nest.js 中执行 SQL 查询通常涉及使用 TypeORM 或 Sequelize 这样的 ORM(对象-关系映射)库。这些库使得在 Nest.js 应用程序中连接和操作 SQL 数据库变得更加简单和直观。

以下是一个使用 TypeORM 在 Nest.js 中执行 SQL 查询的示例代码:

首先,确保你已经在项目中安装了 TypeORM:

在user模块中的entites中;user.entity.ts中有如下代码:

复制代码
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number
  @Column()
  username: string
  @Column()
  password: string
}

在user模块中的service中;user.service.ts中有如下代码:

复制代码
import { Injectable } from '@nestjs/common';
import { UserDto } from './dto/create-user.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { User } from './entities/user.entity';
import { Repository } from 'typeorm';
@Injectable()
export class UserService {
  constructor(
    @InjectRepository(User) private readonly user: Repository<User>) { }
  async login(userDto: UserDto) {
    const userInfo = await this.user.findOne({
      where: {
        username: userDto.username,
        password: userDto.password,
      },
    })
    return userInfo;
  }

  async register(userDto: UserDto) {
    const existingUser = await this.user.findOne({
      where: {
        username: userDto.username,
      },
    });
    if (existingUser) {
      return '用户名已存在';
    }
    const data = new User();
    data.username = userDto.username;
    data.password = userDto.password;
    return this.user.save(data);
  }
}

很明显没有使用sql语句,但是也完成了sql操作;

在另一个项目中使用的是

user模块中的user.model.ts中代码如下:

复制代码
import { Column, Model, Table } from 'sequelize-typescript';

@Table
export class User extends Model<User> {
  @Column
  username: string;
  @Column
  password: string;
  @Column
  avatar: string;
}

在user.service.ts中代码如下:

复制代码
import { Injectable } from '@nestjs/common';
import { CreateUserDto } from './dto/create-user.dto';
import { InjectModel } from '@nestjs/sequelize';
import { User } from './model/user.model';
@Injectable()
export class UserService {
  constructor(@InjectModel(User) private userModel: typeof User,) {

  }
  async create(createUserDto: CreateUserDto) {
    let res = await this.userModel.build({
      ...createUserDto
    })
    await res.save()
    return res
  }
  async findAll() {
    let res = await this.userModel.findAll()
    return res
  }
  async find(createUserDto: CreateUserDto) {
    let res = await this.userModel.findOne({
      where: {
        ...createUserDto
      }
    })
    return res
  }
  async findOne(username: string) {
    let res = await this.userModel.findOne({
      where: {
        username
      }
    })
    return res !== null ? res : null
  }
  async uploadAvatar(username: string, avatar: string) {
    let res = await this.userModel.update({
      avatar
    }, {
      where: {
        username: username
      }
    })
    return {
      code: '200',
      msg: "上传成功",
      data: res
    }
  }
  async hasAvatar(username: string) {
    let res = await this.userModel.findOne({
      where: {
        username: username
      }
    })
    return res.avatar !== null
  }
}
相关推荐
map_3d_vis30 分钟前
百度地图 Hello World:5 分钟用 JSAPI 4.0 显示一张地图
前端·javascript·地图·百度地图
不要试图纠正别人42 分钟前
构建 0 报错,产物体积翻倍:换引擎最容易漏的对账
前端·javascript
恋猫de小郭1 小时前
Android Keep Rules 解读,直接影响 Google Play 上架和曝光
android·前端·flutter
Hilaku1 小时前
面了 30 家公司拿到 2 个 Offer!
前端·javascript·程序员
延晗1 小时前
【从零开始 DeepSeek Harness 插件开发教程】三、从零开发 DeepSeek Harness 插件——完成第一个插件的挂载与运行
前端·人工智能
小飞在努力1 小时前
【实现负载均匀在线判题④】接入 cpp-httplib 实现编译服务 HTTP 网络化
c语言·开发语言·c++·笔记·http
择势2 小时前
iOS 工程环境绝对隔离实践:开发机的"编不出"生产包
前端
某哈压力大2 小时前
Vue的双向绑定与自定义组件的v-model
前端·面试
炸薯条!2 小时前
Python面向对象基础:类与对象、实例方法、魔法方法和属性详解
java·开发语言·python
CappuccinoRose2 小时前
前端开发交互体系 - 汇总篇
开发语言·前端·javascript·css·html·交互