Service 单例 , Singleton, 全局唯一(进程级别)

import {
  Provide,
  Scope,
  ScopeEnum,
  Init,
  Destroy,
  Autoload,
} from "@midwayjs/decorator";

import { PrismaClient } from "@prisma/client";

@Autoload()
@Scope(ScopeEnum.Singleton) // Singleton 单例,全局唯一(进程级别)
@Provide()
export class PrismaService {
  private prisma;

  get db() {
    return this.prisma;
  }
  @Init()
  async connect() {
    // 创建连接
    this.prisma = new PrismaClient();
  }

  @Destroy()
  async close() {
    await this.prisma.$disconnect();
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31