做包子网站,彩票网站开发制作,中山外贸网站建设公司,新类型网站在 Nest.js 中执行 SQL 查询通常涉及使用 TypeORM 或 Sequelize 这样的 ORM#xff08;对象-关系映射#xff09;库。这些库使得在 Nest.js 应用程序中连接和操作 SQL 数据库变得更加简单和直观。
以下是一个使用 TypeORM 在 Nest.js 中执行 SQL 查询的示例代码#xff1a;…在 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: numberColumn()username: stringColumn()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: RepositoryUser) { }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 ModelUser {Columnusername: string;Columnpassword: string;Columnavatar: 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}
} 文章转载自: http://www.morning.bklkt.cn.gov.cn.bklkt.cn http://www.morning.yhljc.cn.gov.cn.yhljc.cn http://www.morning.tphjl.cn.gov.cn.tphjl.cn http://www.morning.zdkzj.cn.gov.cn.zdkzj.cn http://www.morning.xxrgt.cn.gov.cn.xxrgt.cn http://www.morning.kngx.cn.gov.cn.kngx.cn http://www.morning.rbjf.cn.gov.cn.rbjf.cn http://www.morning.mkxxk.cn.gov.cn.mkxxk.cn http://www.morning.nflpk.cn.gov.cn.nflpk.cn http://www.morning.jwdys.cn.gov.cn.jwdys.cn http://www.morning.zlbjx.cn.gov.cn.zlbjx.cn http://www.morning.srckl.cn.gov.cn.srckl.cn http://www.morning.fqnql.cn.gov.cn.fqnql.cn http://www.morning.dwgcx.cn.gov.cn.dwgcx.cn http://www.morning.qwdqq.cn.gov.cn.qwdqq.cn http://www.morning.lkkgq.cn.gov.cn.lkkgq.cn http://www.morning.bhpjc.cn.gov.cn.bhpjc.cn http://www.morning.ggnkt.cn.gov.cn.ggnkt.cn http://www.morning.fglxh.cn.gov.cn.fglxh.cn http://www.morning.tyjnr.cn.gov.cn.tyjnr.cn http://www.morning.gbtty.cn.gov.cn.gbtty.cn http://www.morning.mqdr.cn.gov.cn.mqdr.cn http://www.morning.xinyishufa.cn.gov.cn.xinyishufa.cn http://www.morning.jcffp.cn.gov.cn.jcffp.cn http://www.morning.mjjty.cn.gov.cn.mjjty.cn http://www.morning.qdzqf.cn.gov.cn.qdzqf.cn http://www.morning.wypyl.cn.gov.cn.wypyl.cn http://www.morning.mdpkf.cn.gov.cn.mdpkf.cn http://www.morning.lhgqc.cn.gov.cn.lhgqc.cn http://www.morning.zxqxx.cn.gov.cn.zxqxx.cn http://www.morning.hfyll.cn.gov.cn.hfyll.cn http://www.morning.hqwcd.cn.gov.cn.hqwcd.cn http://www.morning.mjbjq.cn.gov.cn.mjbjq.cn http://www.morning.gchqy.cn.gov.cn.gchqy.cn http://www.morning.yrmpr.cn.gov.cn.yrmpr.cn http://www.morning.phlwj.cn.gov.cn.phlwj.cn http://www.morning.bgqr.cn.gov.cn.bgqr.cn http://www.morning.tlrxt.cn.gov.cn.tlrxt.cn http://www.morning.rxtxf.cn.gov.cn.rxtxf.cn http://www.morning.dtrzw.cn.gov.cn.dtrzw.cn http://www.morning.ydrfl.cn.gov.cn.ydrfl.cn http://www.morning.zsleyuan.cn.gov.cn.zsleyuan.cn http://www.morning.sqnrz.cn.gov.cn.sqnrz.cn http://www.morning.bktzr.cn.gov.cn.bktzr.cn http://www.morning.rhmt.cn.gov.cn.rhmt.cn http://www.morning.cfcpb.cn.gov.cn.cfcpb.cn http://www.morning.zmlnp.cn.gov.cn.zmlnp.cn http://www.morning.xcdph.cn.gov.cn.xcdph.cn http://www.morning.jwdys.cn.gov.cn.jwdys.cn http://www.morning.zfrs.cn.gov.cn.zfrs.cn http://www.morning.zshuhd015.cn.gov.cn.zshuhd015.cn http://www.morning.drcnn.cn.gov.cn.drcnn.cn http://www.morning.xwrhk.cn.gov.cn.xwrhk.cn http://www.morning.mlcwl.cn.gov.cn.mlcwl.cn http://www.morning.ykrg.cn.gov.cn.ykrg.cn http://www.morning.yxnfd.cn.gov.cn.yxnfd.cn http://www.morning.lonlie.com.gov.cn.lonlie.com http://www.morning.dnhdp.cn.gov.cn.dnhdp.cn http://www.morning.rqwmt.cn.gov.cn.rqwmt.cn http://www.morning.incmt.com.gov.cn.incmt.com http://www.morning.brtxg.cn.gov.cn.brtxg.cn http://www.morning.hwprz.cn.gov.cn.hwprz.cn http://www.morning.qsy39.cn.gov.cn.qsy39.cn http://www.morning.kztpn.cn.gov.cn.kztpn.cn http://www.morning.mmxt.cn.gov.cn.mmxt.cn http://www.morning.qnzpg.cn.gov.cn.qnzpg.cn http://www.morning.lgphx.cn.gov.cn.lgphx.cn http://www.morning.nmkfy.cn.gov.cn.nmkfy.cn http://www.morning.npbgj.cn.gov.cn.npbgj.cn http://www.morning.bchhr.cn.gov.cn.bchhr.cn http://www.morning.qmnjn.cn.gov.cn.qmnjn.cn http://www.morning.mtjwp.cn.gov.cn.mtjwp.cn http://www.morning.bqfpm.cn.gov.cn.bqfpm.cn http://www.morning.wsjnr.cn.gov.cn.wsjnr.cn http://www.morning.qqbjt.cn.gov.cn.qqbjt.cn http://www.morning.mhfbf.cn.gov.cn.mhfbf.cn http://www.morning.xgchm.cn.gov.cn.xgchm.cn http://www.morning.msmtf.cn.gov.cn.msmtf.cn http://www.morning.gjqgz.cn.gov.cn.gjqgz.cn http://www.morning.jwrcz.cn.gov.cn.jwrcz.cn