Parcourir la source

refactor(prisma): 引入DatasourceService集中管理PrismaClient实例

将各控制器中直接实例化PrismaClient的方式重构为通过依赖注入的DatasourceService获取实例
这样可以避免重复创建数据库连接,提高性能并统一管理数据库连接
同时更新.gitignore文件以忽略更多包管理器的锁定文件
Sakulin il y a 2 mois
Parent
commit
caf33f91d7

+ 5 - 1
.gitignore

@@ -57,4 +57,8 @@ report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
 
 # sb jixin
 package-lock.json
-prisma/db.sqlite
+prisma/db.sqlite
+
+yarn.lock
+pnpm-lock.yaml
+pnpm-workspace.yaml

+ 2 - 1
src/app.module.ts

@@ -5,10 +5,11 @@ import { PostController } from './post/post.controller';
 import { AuthController } from './auth/auth.controller';
 import { AuthService } from './auth/auth.service';
 import { TagController } from './tag/tag.controller';
+import { DatasourceService } from './datasource/datasource.service';
 
 @Module({
   imports: [],
   controllers: [AppController, PostController, AuthController, TagController],
-  providers: [AppService, AuthService],
+  providers: [AppService, AuthService, DatasourceService],
 })
 export class AppModule {}

+ 18 - 0
src/datasource/datasource.service.spec.ts

@@ -0,0 +1,18 @@
+import { Test, TestingModule } from '@nestjs/testing';
+import { DatasourceService } from './datasource.service';
+
+describe('DatasourceService', () => {
+  let service: DatasourceService;
+
+  beforeEach(async () => {
+    const module: TestingModule = await Test.createTestingModule({
+      providers: [DatasourceService],
+    }).compile();
+
+    service = module.get<DatasourceService>(DatasourceService);
+  });
+
+  it('should be defined', () => {
+    expect(service).toBeDefined();
+  });
+});

+ 15 - 0
src/datasource/datasource.service.ts

@@ -0,0 +1,15 @@
+import { Injectable } from '@nestjs/common';
+import { PrismaClient } from '@prisma/client';
+
+@Injectable()
+export class DatasourceService {
+  private readonly datasource: PrismaClient;
+
+  constructor() {
+    this.datasource = new PrismaClient();
+  }
+
+  getPrisma() {
+    return this.datasource;
+  }
+}

+ 10 - 7
src/post/post.controller.ts

@@ -7,8 +7,8 @@ import {
   Body,
   Headers,
 } from '@nestjs/common';
-import { PrismaClient } from '@prisma/client';
 import { AuthService } from 'src/auth/auth.service';
+import { DatasourceService } from 'src/datasource/datasource.service';
 
 interface PostBody {
   title: string;
@@ -18,14 +18,17 @@ interface PostBody {
 
 @Controller('post')
 export class PostController {
-  constructor(private authService: AuthService) {}
+  constructor(
+    private authService: AuthService,
+    private datasource: DatasourceService,
+  ) {}
 
   @Get('/list')
   async getAllPosts(
     @Query('length') take: number = 10,
     @Query('start') skip: number = 0,
   ) {
-    const prisma = new PrismaClient();
+    const prisma = this.datasource.getPrisma();
     return {
       code: 200,
       data: await prisma.post.findMany({
@@ -50,7 +53,7 @@ export class PostController {
 
   @Get('/get/:id')
   async getPostById(@Param('id') id: string) {
-    const prisma = new PrismaClient();
+    const prisma = this.datasource.getPrisma();
     const target = await prisma.post.findUnique({
       where: { id: parseInt(id) },
       select: {
@@ -90,7 +93,7 @@ export class PostController {
         msg: 'Unauthorized',
       };
     }
-    const prisma = new PrismaClient();
+    const prisma = this.datasource.getPrisma();
     return await prisma.post
       .create({
         data: {
@@ -129,7 +132,7 @@ export class PostController {
         msg: 'Unauthorized',
       };
     }
-    const prisma = new PrismaClient();
+    const prisma = this.datasource.getPrisma();
     return await prisma.post
       .update({
         where: { id: parseInt(id) },
@@ -166,7 +169,7 @@ export class PostController {
         msg: 'Unauthorized',
       };
     }
-    const prisma = new PrismaClient();
+    const prisma = this.datasource.getPrisma();
     return await prisma.post
       .delete({
         where: { id: parseInt(id) },

+ 8 - 5
src/tag/tag.controller.ts

@@ -1,14 +1,17 @@
 import { Controller, Get, Param, Headers } from '@nestjs/common';
-import { PrismaClient } from '@prisma/client';
 import { AuthService } from 'src/auth/auth.service';
+import { DatasourceService } from 'src/datasource/datasource.service';
 
 @Controller('tag')
 export class TagController {
-  constructor(private authService: AuthService) {}
+  constructor(
+    private authService: AuthService,
+    private datasource: DatasourceService,
+  ) {}
 
   @Get('/list')
   async getAllTags() {
-    const prisma = new PrismaClient();
+    const prisma = this.datasource.getPrisma();
     return {
       code: 200,
       data: await prisma.tag.findMany({
@@ -23,7 +26,7 @@ export class TagController {
 
   @Get('/get/:id')
   async getTagById(@Param('id') id: string) {
-    const prisma = new PrismaClient();
+    const prisma = this.datasource.getPrisma();
     const target = await prisma.tag.findUnique({
       where: { id: parseInt(id) },
       select: {
@@ -61,7 +64,7 @@ export class TagController {
         msg: 'Unauthorized',
       };
     }
-    const prisma = new PrismaClient();
+    const prisma = this.datasource.getPrisma();
     return await prisma.tag
       .deleteMany({
         where: {