npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nailyjs.nest.modules/prisma

v1.4.1

Published

Nestjs Prisma Module

Downloads

31

Readme

Nest.js 版 Prisma ☁️

中文 | English

Prisma官方Github

本SDK为Prisma的封装。提供了一种更高级的方式在Nest中使用Prisma: 监听器。

安装 📦

npmyarnpnpm 都支持,推荐使用 pnpm

$ pnpm i --save @nailyjs.nest.modules/prisma prisma @prisma/client

使用 👋

导入模块 🧩

首先,先导入模块并配置。建议在根模块中导入。

正常用法 🚀

import { Module } from '@nestjs/common';
import { PrismaModule } from '@nailyjs.nest.modules/prisma';

@Module({
  imports: [
    // 导入Prisma模块。如果不注册任何监听器,可以这么导入。
    PrismaModule.forRoot(),
  ],
})
export class AppModule {}

如何使用 🍞

@nailyjs.nest.modules/prisma导入PrismaService,里面包含了PrismaClient的所有方法。

import { Injectable } from '@nestjs/common';
import { PrismaService } from '@nailyjs.nest.modules/prisma';

@Injectable()
export class AppService {
  constructor(private readonly prismaService: PrismaService) {}

  async findMany() {
    return this.prismaService.user.findMany();
  }
}

监听器 🎉

PrismaClient我直接作为令牌注入了,所以你也可以直接使用PrismaClient

但是,如果你想要监听PrismaClient的方法,就不能直接使用PrismaClient了,而是要使用PrismaService

所以我建议平常就使用PrismaService,这样你可以在任何时候都可以添加监听器。

import { Injectable } from '@nestjs/common';
import { PrismaService } from '@nailyjs.nest.modules/prisma';
import { PrismaClient } from '@prisma/client';

@Injectable()
export class AppService {
  constructor(
    // 允许直接注入PrismaService
    private readonly prismaService: PrismaService,
    // 可以直接注入PrismaClient。PrismaClient是一个单例。
    private readonly prismaClient: PrismaClient,
  ) {}

  public async findMany() {
    // 这里的`findMany`方法会触发上面注册的监听器。
    this.prismaService.user.findMany();
    // 或者直接使用PrismaClient
    // 但是要注意的是,这里不会触发监听器,因为监听器是在PrismaService中注册的,
    // 要触发监听器,必须使用PrismaService中的方法。
    this.prismaClient.user.findMany();
  }
}

@Injectable()
export class PrismaListener {
  /**
   * 假设您有一个名为`user`的prisma模型,可以在这里注册一个监听器,监听`findMany`事件。
   * BeforeListen顾名思义,就是在`findMany`方法执行之前执行的方法。
   */
  @BeforeListen('user', 'findMany')
  public async beforeFindMany() {
    console.log('before findMany');
  }

  /**
   * AfterListen顾名思义,就是在`findMany`方法执行之后执行的方法。
   */
  @AfterListen('user', 'findMany')
  public async afterFindMany() {
    console.log('after findMany');
  }

  /**
   * 监听器还支持这么写。TS会有非常完善的类型提示(小小地做了一下类型体操
   */
  @BeforeListen('user.findMany')
  public async beforeFindMany() {
    console.log('before findMany');
  }

  /**
   * 1.4.0版本之后支持多个监听器调用同一个方法。
   */
  @AfterListen('user.findMany')
  @BeforeListen('user.create')
  public async afterFindMany() {
    console.log('after findMany & before create');
  }
}

然后,监听器必须要在模块中注册。

import { Module } from '@nestjs/common';
import { PrismaModule } from '@nailyjs.nest.modules/prisma';
import { PrismaListener } from './app.service';

@Module({
  imports: [
    PrismaModule.forRoot({
      // 注册监听器
      listeners: [PrismaListener],
    }),
  ],
})
export class AppModule {}

作者 👨‍💻

Zero

☕️ 捐赠 ☕️

如果你觉得这个项目对你有帮助,你可以请我喝杯咖啡QWQ~

wechat alipay