• Nest.js项目小结1


    整体项目结构

            prisma\        (用于管理数据库迁移、定义数据模型和数据填充)

                    migrations\        (日志记录,不用管)

                    schema.prisma        (定义数据模型)

                    seed.ts        (用于填充数据库初始数据的文件)

            src\

                    auth\        (包含处理用户认证的模块代码)

                            dto\        (定义数据传输对象)

                                    register.dto.ts        (数据对象)

                            auth.controller.ts         (处理路由等)

                            auth.module.ts            (模块)

                            auth.service.ts            (逻辑)

                    common\        (包含通用的功能代码)

                            rules\        (自定义验证规则)

                                    is-confirm.rule.ts

                                    is-not-exists.rule.ts

                            validate.ts        (通用数据验证)

                    prisma\

                            prisma.module.ts        (模块)

                            prisma.service.ts        (处理与Prisma数据库交互的服务)

                    app.module.ts        (模块)

                    main.ts        (主入口)

                    transform.inteceptor.ts        (响应拦截器)

    ——————————————————————————————————————

    schema.prisma       

            定义数据库模型

    1. generator client {
    2. provider = "prisma-client-js"
    3. }
    4. datasource db {
    5. provider = "mysql"
    6. url = env("DATABASE_URL")
    7. }
    8. model user {
    9. id Int @id @default(autoincrement()) @db.UnsignedInt
    10. name String @unique
    11. password String
    12. }
    13. model article {
    14. id Int @id @default(autoincrement()) @db.UnsignedInt
    15. title String
    16. content String @db.Text
    17. }

    seed.ts

            填充数据库数据

    1. import { PrismaClient } from "@prisma/client"
    2. import { hash } from "argon2"
    3. import { Random } from "mockjs"
    4. const prisma = new PrismaClient() //注册Prisma
    5. async function run() {
    6. await prisma.user.create({ //往user表里面插入数据
    7. data: {
    8. name: "admin",
    9. password: await hash("admin8888") //转成hash
    10. }
    11. })
    12. for (let i = 0; i < 50; i++) { //填充50组
    13. await prisma.article.create({ //往article表里面插入数据
    14. data: {
    15. title: Random.ctitle(10, 30), //随机题目
    16. content: Random.cparagraph(10, 20), //随机内容
    17. }
    18. })
    19. }
    20. }
    21. run()

    ——————————————————————————————————————

    app.module.ts

    1. import { Module } from '@nestjs/common';
    2. import { AuthModule } from './auth/auth.module'; //引入验证模块
    3. import { PrismaModule } from './prisma/prisma.module'; //引入prisma模块
    4. @Module({
    5. imports: [ AuthModule , PrismaModule ], //插入
    6. })
    7. export class AppModule {}

    ——————————————————————————————————————

    auth

            dto

                    register.dto.ts

    1. import { IsNotEmpty } from "class-validator"; //引入默认不为空验证
    2. import { IsNotExistsRule } from "../../common/rules/is-not-exists.rule"; //引入自定义验证
    3. import { IsConfiemRule } from "../../common/rules/is-confirm.rule"; //引入自定义验证
    4. export default class RegisterDto {
    5. @IsNotEmpty({ message:'用户名不能为空' }) //验证规则
    6. @IsNotExistsRule("user" , { message: "用户已经存在" }) //验证规则
    7. name:string; //类型
    8. @IsNotEmpty({ message:'密码不能为空' })
    9. @IsConfiemRule({ message: "两次密码不一致" })
    10. password:string
    11. @IsNotEmpty({message: "确认密码不能为空"})
    12. password_confirm: string
    13. }

    auth.controller.ts

    1. import { Controller , Body , Post } from '@nestjs/common'
    2. import { AuthService } from './auth.service';
    3. import RegisterDto from './dto/register.dto'; //引入验证规则
    4. @Controller()
    5. export class AuthController{
    6. constructor(private auth: AuthService) {}
    7. @Post('register') //post请求,路由为register
    8. login(@Body() dto: RegisterDto) { //获取body数据 为dto : 验证规则
    9. return this.auth.register(dto) //执行auth的register函数
    10. }
    11. }

    auth.service.ts

    1. import { Injectable } from '@nestjs/common'
    2. import RegisterDto from './dto/register.dto';
    3. import { PrismaService } from './../prisma/prisma.service'; //引入全局配置prisma
    4. import { hash } from 'argon2';
    5. import { JwtService } from '@nestjs/jwt';
    6. @Injectable()
    7. export class AuthService {
    8. constructor(private prisma:PrismaService , private jwt: JwtService){}
    9. async register(dto: RegisterDto){ //验证通过后执行这里
    10. const user = await this.prisma.user.create({ //等候创建
    11. data:{
    12. name: dto.name,
    13. password: await hash(dto.password)
    14. }
    15. })
    16. return this.token(user); //执行token函数
    17. }
    18. private async token({ id , name }){
    19. return {
    20. token: await this.jwt.signAsync({ //执行配置好后的jwt
    21. name ,
    22. sub: id
    23. })
    24. }
    25. }
    26. }

    auth.module.ts

    1. import { Module } from '@nestjs/common'
    2. import { AuthService } from './auth.service'
    3. import { AuthController } from './auth.controller'
    4. import { JwtModule } from '@nestjs/jwt'
    5. import { ConfigModule, ConfigService } from '@nestjs/config'
    6. @Module({
    7. imports: [ //TODO 配置JWT
    8. JwtModule.registerAsync({
    9. imports: [ConfigModule],
    10. inject: [ConfigService],
    11. useFactory: (config: ConfigService) => {
    12. return {
    13. secret: config.get('TOKEN_SECRET'), //密钥在.env里配置
    14. signOptions: { expiresIn: '60d' } //持续时间
    15. }
    16. },
    17. })
    18. ], //END
    19. controllers: [AuthController], //引入控制器
    20. providers: [AuthService] //引入函数
    21. })
    22. export class AuthModule{}

    common

            rules

                    is-confirm.rule.ts

    1. import { PrismaClient } from '@prisma/client'
    2. import { registerDecorator , ValidationArguments , ValidationOptions } from 'class-validator'
    3. //表字段是否唯一
    4. export function IsConfiemRule( //这里定义规则名称
    5. validationOptions?: ValidationOptions
    6. ) {
    7. return function (object: Record<string , any>, propertyName: string) {
    8. registerDecorator({
    9. name: 'IsConfiemRule', //同上
    10. target: object.constructor,
    11. propertyName: propertyName,
    12. constraints: [],
    13. options: validationOptions,
    14. validator: {
    15. async validate(value: string, args: ValidationArguments) {
    16. //TODO
    17. return Boolean(value == args.object[`${args.property}_confirm`]) //true为通过,false为不通过
    18. //END
    19. }
    20. }
    21. })
    22. }
    23. }

                    is-not-exists.ule.ts

    1. import { PrismaClient } from '@prisma/client'
    2. import { registerDecorator , ValidationArguments , ValidationOptions } from 'class-validator'
    3. //表字段是否唯一
    4. export function IsNotExistsRule(
    5. table: string,
    6. validationOptions?: ValidationOptions
    7. ) {
    8. return function (object: Record<string , any>, propertyName: string) {
    9. registerDecorator({
    10. name: 'IsNotExistsRule',
    11. target: object.constructor,
    12. propertyName: propertyName,
    13. constraints: [table],
    14. options: validationOptions,
    15. validator: {
    16. async validate(value: string, args: ValidationArguments) {
    17. //TODO
    18. const prisma = new PrismaClient
    19. const res = await prisma[table].findFirst({
    20. where: {
    21. [args.property]: value
    22. }
    23. })
    24. return !Boolean(res)
    25. //END
    26. }
    27. }
    28. })
    29. }
    30. }
    31. //同上

            validate.ts

    1. import { HttpException, HttpStatus, ValidationPipe } from "@nestjs/common";
    2. import { ValidationError } from "class-validator";
    3. export default class Validate extends ValidationPipe {
    4. protected flattenValidationErrors(validationErrors: ValidationError[]): string[] {
    5. const messages = {}
    6. validationErrors.forEach(error => { //遍历出必要错误返回给前端
    7. messages[error.property] = Object.values(error.constraints)[0];
    8. })
    9. throw new HttpException({ //返回给前端的错误
    10. code: 422,
    11. messages
    12. }, HttpStatus.UNPROCESSABLE_ENTITY);
    13. }
    14. }

    prisma

            prisma.module.ts

    1. import { Global, Module } from '@nestjs/common'
    2. import { PrismaService } from './prisma.service';
    3. @Global() // 全局模块
    4. @Module({
    5. providers: [PrismaService],
    6. exports: [PrismaService]
    7. })
    8. export class PrismaModule{}

            prisma.service.ts

    1. import { PrismaClient } from '@prisma/client'
    2. import { Injectable } from '@nestjs/common'
    3. @Injectable()
    4. export class PrismaService extends PrismaClient{
    5. constructor(){
    6. super({
    7. log:['query']
    8. })
    9. }
    10. }

    transform.inteceptor.ts

    1. import { CallHandler , ExecutionContext , Injectable , NestInterceptor } from '@nestjs/common'
    2. import { map } from 'rxjs/operators'
    3. @Injectable()
    4. export class TransformInterceptor implements NestInterceptor { //修改一下数据格式
    5. intercept(context: ExecutionContext, next: CallHandler) {
    6. return next.handle().pipe(
    7. map((data) => {
    8. return {
    9. data
    10. }
    11. })
    12. )
    13. }
    14. }

    main.ts

    1. import { NestFactory } from '@nestjs/core';
    2. import { AppModule } from './app.module';
    3. import Validate from './common/validate';
    4. import { TransformInterceptor } from './transform.inteceptor';
    5. async function bootstrap() {
    6. const app = await NestFactory.create(AppModule);
    7. app.useGlobalPipes(new Validate()) //返回给前端的错误数据
    8. app.useGlobalInterceptors(new TransformInterceptor()) //返回给前端的正确数据(响应拦截器)
    9. await app.listen(3000); //监听的端口号
    10. }
    11. bootstrap();

  • 相关阅读:
    leetcode 216. 组合总和 III
    外卖系统源码,搭建同城点餐配送服务平台
    机器学习/人工智能的笔试面试题目——CNN相关问题总结
    什么是岗位点检?在设备管理中如何应用岗位点检理论?
    C语言基础篇 —— 4.1 管理内存:栈(stack)、堆(heap)、数据区(.data)
    SpringBoot 如何使用 CORS 进行跨域资源共享
    用Nginx搭建一个具备缓存功能的反向代理服务
    OneFormer: One Transformer to Rule Universal Image Segmentation论文笔记
    excel的frequency函数的用法和实例
    C++之<set和map模拟实现>
  • 原文地址:https://blog.csdn.net/m0_53785610/article/details/138169744