@20i/prisma-nestjs 中文文档教程

发布于 3年前 浏览 25 项目主页 更新于 3年前

@20i/prisma-nestjs

Prisma Client 的简单包装器,可以轻松注入 NestJS 应用程序

Peer dependencies

    "@nestjs/common": "^7.6.17",
    "@nestjs/config": "^0.6.3",
    "@nestjs/core": "^7.6.17",
    "@nestjs/jwt": "^7.2.0",
    "@nestjs/passport": "^7.1.5",
    "@nestjs/platform-express": "^7.6.17",
    "@nestjs/swagger": "^4.8.0",
    "prisma": "2.26.0",
    "@prisma/client": "2.26.0",
    "class-transformer": "^0.4.0",
    "class-validator": "^0.13.1",
    "jsonwebtoken": "^8.5.1",
    "jwk-to-pem": "^2.0.5",
    "jwks-rsa": "^2.0.3",
    "passport": "^0.4.1",
    "passport-jwt": "^4.0.0",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^6.6.6",
    "swagger-ui-express": "^4.1.6"

Installation

npm login
npm i @20i/prisma-nestjs
npx prisma generate #???

Usage

Inside a Service

@Injectable()
export class ReportsService {
  constructor(
    private readonly prisma: PrismaService,
  ) {}

  getSomething(id: number) {
    return this.prisma.aThing.findUnique({
        where: {
           id,
        }
    }
  }
}

In your app.module.ts

import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { PrismaModule } from './prisma/prisma.module';
// Other imports

@Module({
  imports: [
    // global modules
    ConfigModule.forRoot({
      isGlobal: true,
    }),
    PrismaModule, // <== here
  ],
  providers: [{ provide: APP_GUARD, useClass: GlobalJwtGuard }], // relies on the AuthModule
  controllers: [AppController],
})
export class AppModule {}

.env file

DATABASE_URL="postgresql://postgres:password@localhost:6432/postgres?schema=public"

@20i/prisma-nestjs

A simple wrapper around the Prisma Client that makes it easy to inject into a NestJS App

Peer dependencies

    "@nestjs/common": "^7.6.17",
    "@nestjs/config": "^0.6.3",
    "@nestjs/core": "^7.6.17",
    "@nestjs/jwt": "^7.2.0",
    "@nestjs/passport": "^7.1.5",
    "@nestjs/platform-express": "^7.6.17",
    "@nestjs/swagger": "^4.8.0",
    "prisma": "2.26.0",
    "@prisma/client": "2.26.0",
    "class-transformer": "^0.4.0",
    "class-validator": "^0.13.1",
    "jsonwebtoken": "^8.5.1",
    "jwk-to-pem": "^2.0.5",
    "jwks-rsa": "^2.0.3",
    "passport": "^0.4.1",
    "passport-jwt": "^4.0.0",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^6.6.6",
    "swagger-ui-express": "^4.1.6"

Installation

npm login
npm i @20i/prisma-nestjs
npx prisma generate #???

Usage

Inside a Service

@Injectable()
export class ReportsService {
  constructor(
    private readonly prisma: PrismaService,
  ) {}

  getSomething(id: number) {
    return this.prisma.aThing.findUnique({
        where: {
           id,
        }
    }
  }
}

In your app.module.ts

import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { PrismaModule } from './prisma/prisma.module';
// Other imports

@Module({
  imports: [
    // global modules
    ConfigModule.forRoot({
      isGlobal: true,
    }),
    PrismaModule, // <== here
  ],
  providers: [{ provide: APP_GUARD, useClass: GlobalJwtGuard }], // relies on the AuthModule
  controllers: [AppController],
})
export class AppModule {}

.env file

DATABASE_URL="postgresql://postgres:password@localhost:6432/postgres?schema=public"
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文