Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility to use extensions and this.prisma. #88

Closed
famouzkk opened this issue Dec 29, 2023 · 2 comments
Closed

Possibility to use extensions and this.prisma. #88

famouzkk opened this issue Dec 29, 2023 · 2 comments

Comments

@famouzkk
Copy link

Is it possible to use extensions and still access the Prisma client using this.prisma. instead of this.prisma.client.?

@shreyassood
Copy link

+1 would be great to have extensions support

@marcjulian
Copy link
Member

@shreyassood Prisma extensions are already supported: https://nestjs-prisma.dev/docs/prisma-client-extensions/

@famouzkk I haven't found a way to make this possible.

The default PrismaService is extended by the PrismaClient

PrismaService

// simplified
import { Inject, Injectable, OnModuleInit, Optional } from '@nestjs/common';
import { Prisma, PrismaClient } from '@prisma/client';
import { PrismaServiceOptions } from './interfaces';
import { PRISMA_SERVICE_OPTIONS } from './prisma.constants';

@Injectable()
export class PrismaService extends PrismaClient {
  constructor(
    @Optional()
    @Inject(PRISMA_SERVICE_OPTIONS)
    private readonly prismaServiceOptions: PrismaServiceOptions = {},
  ) {
    super(prismaServiceOptions.prismaOptions);
  }
}

That allows to access this.prisma.MODEL_NAME directly

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

  users() {
    return this.prisma.user.findMany();
  }
}

However, with Prisma extensions it is not possible to extend the service with the applied extension. Open issue about this: prisma/prisma#18628

My solution is to provide the Prisma Client with the extension via the CustomPrismaModule and exposing it as client property.

CustomPrismaService

@Injectable()
export class CustomPrismaService<Client extends PrismaClientLike> {
  constructor(
    @Inject(CUSTOM_PRISMA_CLIENT)
    public client: Client,
  ) {}
}

This approach requires to access the client this.prisma.client.MODEL_NAME

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

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

If you have a solution, I am always open for ideas or a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants