Giter Site home page Giter Site logo

Comments (3)

marcjulian avatar marcjulian commented on June 9, 2024 1

Example to use Prisma Client Extensions.

  1. Install Prisma Client v4.7.0 or later and install [email protected]

  2. Enable clientExtensions preview

datasource db {
  provider = "sqlite"
  url      = "file:dev.db"
}

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["clientExtensions"]
}

model User {
  id    String  @id @default(cuid())
  email String  @unique
  name  String?
}
  1. Create a file for the Prisma extension prisma.extension.ts
import { PrismaClient } from '@prisma/client';

/**
 * TS error: "Inferred type of this node exceeds the maximum length the compiler will serialize" with "declaration": true in tsconfig
 *
 * Change "declaration": false in `tsconfig.json`
 *
 * https://github.com/prisma/prisma/issues/16536#issuecomment-1332055501
 */
export const prismaClient = new PrismaClient().$extends({
  model: {
    user: {
      findByEmail: async (email: string) => {
        return prismaClient.user.findFirstOrThrow({ where: { email } });
      },
    },
  },
});

export type prismaClient = typeof prismaClient;
  1. Change declaration to false in your tsconfig.json - workaround for prisma/prisma#16536 (comment)

The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed.

  1. Use CustomPrismaModule.forRootAsync
import { Module } from '@nestjs/common';
import { CustomPrismaModule } from 'nestjs-prisma';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { prismaClient } from './prisma.extension';

@Module({
  imports: [
    CustomPrismaModule.forRootAsync({
      name: 'PrismaService',
      useFactory: () => {
        return prismaClient;
      },
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
  1. Inject CustomPrismaService into your controller/service
import { Inject, Injectable } from '@nestjs/common';
import { CustomPrismaService } from 'nestjs-prisma';
import { prismaClient } from './prisma.extension';

@Injectable()
export class AppService {
  constructor(
    // ✅ use `prismaClient` from extension for correct type-safety
    @Inject('PrismaService')
    private prismaService: CustomPrismaService<prismaClient>,
  ) {}

  users() {
    return this.prismaService.client.user.findMany();
  }

  user(userId: string) {
    // 🦾 use new `findByEmail`
    return this.prismaService.client.user.findByEmail(userId);
  }
}

from nestjs-prisma.

shoaibsharif avatar shoaibsharif commented on June 9, 2024

Thank you for the example. That helps a lot to understand how to use extension with new dev release version, but is there a way we can use extension in existing PrismaService or anyway to override that?

from nestjs-prisma.

marcjulian avatar marcjulian commented on June 9, 2024

hi @shoaibsharif haven't found a way to use extension with the existing PrismaService as the service is directly extended by the default PrismaClient. For now you need to use CustomPrismaService.

from nestjs-prisma.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.