Nest操作MySQL
2021/8/5 19:09:39
本文主要是介绍Nest操作MySQL,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
本文旨在说明如何通过NestJS实现以ORM方式操作MySQL:
如何安装MySQL不是本文重点,就此跳过。
安装NestJS
npm install -g typescript
npm install -g @nestjs/cli
cd [Project Path]
nest new dashboard
cd dashboard
npm install --save @nestjs/typeorm typeorm mysql
打开项目: code .
初始化模块
nest g module menus
nest g service menus
nest g controller menus
nest g class menus/menu.entity
Entity(实体)
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; @Entity('menu') export class MenuEntity { @PrimaryGeneratedColumn() id: number; @Column({ length: 45 }) firstname: string; @Column({ length: 45 }) familyname: string; @Column({ length: 200 }) email: string; }
Service(服务)
import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { MenuEntity } from './menu.entity'; @Injectable() export class MenusService { constructor( @InjectRepository(MenuEntity) private menusRepository: Repository<MenuEntity>, ) {} async getUsers(user: MenuEntity): Promise<MenuEntity[]> { return await this.menusRepository.find(); } async getUser(_id: number): Promise<MenuEntity[]> { return await this.menusRepository.find({ select: ['firstname', 'familyname', 'email'], where: [{ id: _id }], }); } }
Controller(控制器)
import { Controller, Post, Body, Get, Put, Delete, Param, } from '@nestjs/common'; import { MenusService } from './menus.service'; import { MenuEntity } from './menu.entity'; @Controller('menus') export class MenusController { constructor(private service: MenusService) {} @Get(':id') get(@Param() params) { return this.service.getMenu(params.id); } }
Menu.Module(模块)
import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { MenusService } from './menus.service'; import { MenusController } from './menus.controller'; import { MenuEntity } from './menu.entity'; @Module({ imports: [TypeOrmModule.forFeature([MenuEntity])], providers: [MenusService], controllers: [MenusController], }) export class MenusModule {}
App.Module(根模块)
import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { MenusModule } from './menus/menus.module'; import { TypeOrmModule } from '@nestjs/typeorm'; @Module({ imports: [MenusModule, TypeOrmModule.forRoot()], controllers: [AppController], providers: [AppService], }) export class AppModule {}
ormconfig.json
{ "type": "mysql", "host": "localhost", "port": 3306, "username": "root", "password": "Password01!", "database": "dmp", "entities": ["dist/**/*.entity.js"], "synchronize": true }
注意:此处的entities配置与参考文章不同,应该是版本问题。
参考
https://shaibenshimol.medium.com/nestjs-and-mysql-in-10-minutes-711e02ec1dab
--该文章用的typeorm版本和最新的nestjs略有不兼容,须参考bug fix部分做修改
Bug Fix
https://stackoverflow.com/questions/68317383/typeerror-rxjs-1-lastvaluefrom-is-not-a-function
https://stackoverflow.com/questions/50093144/mysql-8-0-client-does-not-support-authentication-protocol-requested-by-server
https://github.com/nestjs/nest/issues/4283
这篇关于Nest操作MySQL的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-07MySQL读写分离入门:轻松掌握数据库读写分离技术
- 2024-12-07MySQL读写分离入门教程
- 2024-12-07MySQL分库分表入门详解
- 2024-12-07MySQL分库分表入门指南
- 2024-12-07MySQL慢查询入门:快速掌握性能优化技巧
- 2024-12-07MySQL入门:新手必读的简单教程
- 2024-12-07MySQL入门:从零开始学习MySQL数据库
- 2024-12-07MySQL索引入门:新手快速掌握MySQL索引技巧
- 2024-12-06BinLog学习:MySQL数据库BinLog入门教程
- 2024-12-06Binlog学习:MySQL数据库的日志管理入门教程