本文旨在说明如何通过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